-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathlocalInstrument.go
152 lines (130 loc) · 4.39 KB
/
localInstrument.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
// Copyright 2020 The Moov Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package wire
import (
"encoding/json"
"strings"
"unicode/utf8"
)
// LocalInstrument is the LocalInstrument of the wire
type LocalInstrument struct {
// tag
tag string
// LocalInstrumentCode is local instrument code
LocalInstrumentCode string `json:"LocalInstrument,omitempty"`
// ProprietaryCode is proprietary code
ProprietaryCode string `json:"proprietaryCode,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewLocalInstrument returns a new LocalInstrument
func NewLocalInstrument() *LocalInstrument {
li := &LocalInstrument{
tag: TagLocalInstrument,
}
return li
}
// Parse takes the input string and parses the LocalInstrument values
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (li *LocalInstrument) Parse(record string) error {
if utf8.RuneCountInString(record) < 6 {
return NewTagMinLengthErr(6, len(record))
}
li.tag = record[:6]
length := 6
value, read, err := li.parseFixedStringField(record[length:], 4)
if err != nil {
return fieldError("LocalInstrumentCode", err)
}
li.LocalInstrumentCode = value
length += read
value, read, err = li.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("ProprietaryCode", err)
}
li.ProprietaryCode = value
length += read
if err := li.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (li *LocalInstrument) UnmarshalJSON(data []byte) error {
type Alias LocalInstrument
aux := struct {
*Alias
}{
(*Alias)(li),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
li.tag = TagLocalInstrument
return nil
}
// String returns a fixed-width LocalInstrument record
func (li *LocalInstrument) String() string {
return li.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a LocalInstrument record formatted according to the FormatOptions
func (li *LocalInstrument) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(45)
buf.WriteString(li.tag)
buf.WriteString(li.LocalInstrumentCodeField())
buf.WriteString(li.FormatProprietaryCode(options) + Delimiter)
if options.VariableLengthFields {
return li.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on LocalInstrument and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (li *LocalInstrument) Validate() error {
if err := li.fieldInclusion(); err != nil {
return err
}
if li.tag != TagLocalInstrument {
return fieldError("tag", ErrValidTagForType, li.tag)
}
if err := li.isLocalInstrumentCode(li.LocalInstrumentCode); err != nil {
return fieldError("LocalInstrumentCode", err, li.LocalInstrumentCode)
}
if err := li.isAlphanumeric(li.ProprietaryCode); err != nil {
return fieldError("ProprietaryCode", err, li.ProprietaryCode)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
// ProprietaryCode is only allowed if LocalInstrument Code is PROP
func (li *LocalInstrument) fieldInclusion() error {
if li.LocalInstrumentCode != ProprietaryLocalInstrumentCode && li.ProprietaryCode != "" {
return fieldError("ProprietaryCode", ErrInvalidProperty, li.ProprietaryCode)
}
return nil
}
// LocalInstrumentCodeField gets a string of LocalInstrumentCode field
func (li *LocalInstrument) LocalInstrumentCodeField() string {
return li.alphaField(li.LocalInstrumentCode, 4)
}
// ProprietaryCodeField gets a string of ProprietaryCode field
func (li *LocalInstrument) ProprietaryCodeField() string {
return li.alphaField(li.ProprietaryCode, 35)
}
// FormatLocalInstrumentCode returns LocalInstrumentCode formatted according to the FormatOptions
func (li *LocalInstrument) FormatLocalInstrumentCode(options FormatOptions) string {
return li.formatAlphaField(li.LocalInstrumentCode, 4, options)
}
// FormatProprietaryCode returns ProprietaryCode formatted according to the FormatOptions
func (li *LocalInstrument) FormatProprietaryCode(options FormatOptions) string {
return li.formatAlphaField(li.ProprietaryCode, 35, options)
}