-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathtypeSubType.go
112 lines (100 loc) · 2.86 KB
/
typeSubType.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
// 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"
)
// TypeSubType {1510}
type TypeSubType struct {
// tag
tag string
// TypeCode
TypeCode string `json:"typeCode"`
// SubTypeCode
SubTypeCode string `json:"subTypeCode"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewTypeSubType returns a new TypeSubType
func NewTypeSubType() *TypeSubType {
tst := &TypeSubType{
tag: TagTypeSubType,
}
return tst
}
// Parse takes the input string and parses the TypeSubType 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 (tst *TypeSubType) Parse(record string) error {
if utf8.RuneCountInString(record) != 10 {
return NewTagWrongLengthErr(10, utf8.RuneCountInString(record))
}
tst.tag = tst.parseStringField(record[:6])
tst.TypeCode = tst.parseStringField(record[6:8])
tst.SubTypeCode = tst.parseStringField(record[8:10])
return nil
}
func (tst *TypeSubType) UnmarshalJSON(data []byte) error {
type Alias TypeSubType
aux := struct {
*Alias
}{
(*Alias)(tst),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
tst.tag = TagTypeSubType
return nil
}
// String writes TypeSubType
func (tst *TypeSubType) String() string {
var buf strings.Builder
buf.Grow(10)
buf.WriteString(tst.tag)
buf.WriteString(tst.TypeCodeField())
buf.WriteString(tst.SubTypeCodeField())
return buf.String()
}
// Validate performs WIRE format rule checks on TypeSubType and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (tst *TypeSubType) Validate() error {
if err := tst.fieldInclusion(); err != nil {
return err
}
if tst.tag != TagTypeSubType {
return fieldError("tag", ErrValidTagForType, tst.tag)
}
if err := tst.isTypeCode(tst.TypeCode); err != nil {
return fieldError("TypeCode", err, tst.TypeCode)
}
if err := tst.isSubTypeCode(tst.SubTypeCode); err != nil {
return fieldError("SubTypeCode", err, tst.SubTypeCode)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (tst *TypeSubType) fieldInclusion() error {
if tst.TypeCode == "" {
return fieldError("TypeCode", ErrFieldRequired)
}
if tst.SubTypeCode == "" {
return fieldError("SubTypeCode", ErrFieldRequired)
}
return nil
}
// TypeCodeField gets a string of the TypeCode field
func (tst *TypeSubType) TypeCodeField() string {
return tst.alphaField(tst.TypeCode, 2)
}
// SubTypeCodeField gets a string of the SubTypeCode field
func (tst *TypeSubType) SubTypeCodeField() string {
return tst.alphaField(tst.SubTypeCode, 2)
}