-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathserviceMessage.go
383 lines (330 loc) · 11.2 KB
/
serviceMessage.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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
// 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"
)
// ServiceMessage is the ServiceMessage of the wire
type ServiceMessage struct {
// tag
tag string
// LineOne
LineOne string `json:"lineOne,omitempty"`
// LineTwo
LineTwo string `json:"lineTwo,omitempty"`
// LineThree
LineThree string `json:"lineThree,omitempty"`
// LineFour
LineFour string `json:"lineFour,omitempty"`
// LineFive
LineFive string `json:"lineFive,omitempty"`
// LineSix
LineSix string `json:"lineSix,omitempty"`
// LineSeven
LineSeven string `json:"lineSeven,omitempty"`
// LineEight
LineEight string `json:"lineEight,omitempty"`
// LineNine
LineNine string `json:"lineNine,omitempty"`
// LineTen
LineTen string `json:"lineTen,omitempty"`
// LineEleven
LineEleven string `json:"lineEleven,omitempty"`
// LineTwelve
LineTwelve string `json:"lineTwelve,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewServiceMessage returns a new ServiceMessage
func NewServiceMessage() *ServiceMessage {
sm := &ServiceMessage{
tag: TagServiceMessage,
}
return sm
}
// Parse takes the input string and parses the ServiceMessage 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 (sm *ServiceMessage) Parse(record string) error {
if utf8.RuneCountInString(record) < 8 {
return NewTagMinLengthErr(8, len(record))
}
sm.tag = record[:6]
length := 6
value, read, err := sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineOne", err)
}
sm.LineOne = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineTwo", err)
}
sm.LineTwo = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineThree", err)
}
sm.LineThree = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineFour", err)
}
sm.LineFour = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineFive", err)
}
sm.LineFive = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineSix", err)
}
sm.LineSix = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineSeven", err)
}
sm.LineSeven = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineEight", err)
}
sm.LineEight = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineNine", err)
}
sm.LineNine = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineTen", err)
}
sm.LineTen = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineEleven", err)
}
sm.LineEleven = value
length += read
value, read, err = sm.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("LineTwelve", err)
}
sm.LineTwelve = value
length += read
if err := sm.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (sm *ServiceMessage) UnmarshalJSON(data []byte) error {
type Alias ServiceMessage
aux := struct {
*Alias
}{
(*Alias)(sm),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
sm.tag = TagServiceMessage
return nil
}
// String returns a fixed-width ServiceMessage record
func (sm *ServiceMessage) String() string {
return sm.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a ServiceMessage record formatted according to the FormatOptions
func (sm *ServiceMessage) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(426)
buf.WriteString(sm.tag)
buf.WriteString(sm.FormatLineOne(options) + Delimiter)
buf.WriteString(sm.FormatLineTwo(options) + Delimiter)
buf.WriteString(sm.FormatLineThree(options) + Delimiter)
buf.WriteString(sm.FormatLineFour(options) + Delimiter)
buf.WriteString(sm.FormatLineFive(options) + Delimiter)
buf.WriteString(sm.FormatLineSix(options) + Delimiter)
buf.WriteString(sm.FormatLineSeven(options) + Delimiter)
buf.WriteString(sm.FormatLineEight(options) + Delimiter)
buf.WriteString(sm.FormatLineNine(options) + Delimiter)
buf.WriteString(sm.FormatLineTen(options) + Delimiter)
buf.WriteString(sm.FormatLineEleven(options) + Delimiter)
buf.WriteString(sm.FormatLineTwelve(options) + Delimiter)
if options.VariableLengthFields {
return sm.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on ServiceMessage and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (sm *ServiceMessage) Validate() error {
if err := sm.fieldInclusion(); err != nil {
return err
}
if sm.tag != TagServiceMessage {
return fieldError("tag", ErrValidTagForType, sm.tag)
}
if err := sm.isAlphanumeric(sm.LineOne); err != nil {
return fieldError("LineOne", err, sm.LineOne)
}
if err := sm.isAlphanumeric(sm.LineTwo); err != nil {
return fieldError("LineTwo", err, sm.LineTwo)
}
if err := sm.isAlphanumeric(sm.LineThree); err != nil {
return fieldError("LineThree", err, sm.LineThree)
}
if err := sm.isAlphanumeric(sm.LineFour); err != nil {
return fieldError("LineFour", err, sm.LineFour)
}
if err := sm.isAlphanumeric(sm.LineFive); err != nil {
return fieldError("LineFive", err, sm.LineFive)
}
if err := sm.isAlphanumeric(sm.LineSix); err != nil {
return fieldError("LineSix", err, sm.LineSix)
}
if err := sm.isAlphanumeric(sm.LineSeven); err != nil {
return fieldError("LineSeven", err, sm.LineSeven)
}
if err := sm.isAlphanumeric(sm.LineEight); err != nil {
return fieldError("LineEight", err, sm.LineEight)
}
if err := sm.isAlphanumeric(sm.LineNine); err != nil {
return fieldError("LineNine", err, sm.LineNine)
}
if err := sm.isAlphanumeric(sm.LineTen); err != nil {
return fieldError("LineTen", err, sm.LineTen)
}
if err := sm.isAlphanumeric(sm.LineEleven); err != nil {
return fieldError("LineEleven", err, sm.LineEleven)
}
if err := sm.isAlphanumeric(sm.LineTwelve); err != nil {
return fieldError("LineTwelve", err, sm.LineTwelve)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (sm *ServiceMessage) fieldInclusion() error {
// If ServiceMessage is defined, LineOne is required
if sm.LineOne == "" {
return fieldError("LineOne", ErrFieldRequired)
}
return nil
}
// LineOneField gets a string of the LineOne field
func (sm *ServiceMessage) LineOneField() string {
return sm.alphaField(sm.LineOne, 35)
}
// LineTwoField gets a string of the LineTwo field
func (sm *ServiceMessage) LineTwoField() string {
return sm.alphaField(sm.LineTwo, 35)
}
// LineThreeField gets a string of the LineThree field
func (sm *ServiceMessage) LineThreeField() string {
return sm.alphaField(sm.LineThree, 35)
}
// LineFourField gets a string of the LineFour field
func (sm *ServiceMessage) LineFourField() string {
return sm.alphaField(sm.LineFour, 35)
}
// LineFiveField gets a string of the LineFive field
func (sm *ServiceMessage) LineFiveField() string {
return sm.alphaField(sm.LineFive, 35)
}
// LineSixField gets a string of the LineSix field
func (sm *ServiceMessage) LineSixField() string {
return sm.alphaField(sm.LineSix, 35)
}
// LineSevenField gets a string of the LineSeven field
func (sm *ServiceMessage) LineSevenField() string {
return sm.alphaField(sm.LineSeven, 35)
}
// LineEightField gets a string of the LineEight field
func (sm *ServiceMessage) LineEightField() string {
return sm.alphaField(sm.LineEight, 35)
}
// LineNineField gets a string of the LineNine field
func (sm *ServiceMessage) LineNineField() string {
return sm.alphaField(sm.LineNine, 35)
}
// LineTenField gets a string of the LineTen field
func (sm *ServiceMessage) LineTenField() string {
return sm.alphaField(sm.LineTen, 35)
}
// LineElevenField gets a string of the LineEleven field
func (sm *ServiceMessage) LineElevenField() string {
return sm.alphaField(sm.LineEleven, 35)
}
// LineTwelveField gets a string of the LineTwelve field
func (sm *ServiceMessage) LineTwelveField() string {
return sm.alphaField(sm.LineTwelve, 35)
}
// FormatLineOne returns LineOne formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineOne(options FormatOptions) string {
return sm.formatAlphaField(sm.LineOne, 35, options)
}
// FormatLineTwo returns LineTwo formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineTwo(options FormatOptions) string {
return sm.formatAlphaField(sm.LineTwo, 35, options)
}
// FormatLineThree returns LineThree formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineThree(options FormatOptions) string {
return sm.formatAlphaField(sm.LineThree, 35, options)
}
// FormatLineFour returns LineFour formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineFour(options FormatOptions) string {
return sm.formatAlphaField(sm.LineFour, 35, options)
}
// FormatLineFive returns LineFive formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineFive(options FormatOptions) string {
return sm.formatAlphaField(sm.LineFive, 35, options)
}
// FormatLineSix returns LineSix formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineSix(options FormatOptions) string {
return sm.formatAlphaField(sm.LineSix, 35, options)
}
// FormatLineSeven returns LineSeven formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineSeven(options FormatOptions) string {
return sm.formatAlphaField(sm.LineSeven, 35, options)
}
// FormatLineEight returns LineEight formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineEight(options FormatOptions) string {
return sm.formatAlphaField(sm.LineEight, 35, options)
}
// FormatLineNine returns LineNine formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineNine(options FormatOptions) string {
return sm.formatAlphaField(sm.LineNine, 35, options)
}
// FormatLineTen returns LineTen formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineTen(options FormatOptions) string {
return sm.formatAlphaField(sm.LineTen, 35, options)
}
// FormatLineEleven returns LineEleven formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineEleven(options FormatOptions) string {
return sm.formatAlphaField(sm.LineEleven, 35, options)
}
// FormatLineTwelve returns LineTwelve formatted according to the FormatOptions
func (sm *ServiceMessage) FormatLineTwelve(options FormatOptions) string {
return sm.formatAlphaField(sm.LineTwelve, 35, options)
}