-
Notifications
You must be signed in to change notification settings - Fork 40
/
accountDebitedDrawdown.go
240 lines (208 loc) · 7.89 KB
/
accountDebitedDrawdown.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
// 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"
)
// AccountDebitedDrawdown is the account which is debited in a drawdown
type AccountDebitedDrawdown struct {
// tag
tag string
// Identification Code * `D` - Debit
IdentificationCode string `json:"identificationCode"`
// Identifier
Identifier string `json:"identifier"`
// Name
Name string `json:"name"`
Address Address `json:"address,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewAccountDebitedDrawdown returns a new AccountDebitedDrawdown
func NewAccountDebitedDrawdown() *AccountDebitedDrawdown {
debitDD := &AccountDebitedDrawdown{
tag: TagAccountDebitedDrawdown,
}
return debitDD
}
// Parse takes the input string and parses the AccountDebitedDrawdown 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 (debitDD *AccountDebitedDrawdown) Parse(record string) error {
if utf8.RuneCountInString(record) < 9 {
return NewTagMinLengthErr(9, len(record))
}
debitDD.tag = record[:6]
debitDD.IdentificationCode = record[6:7]
length := 7
value, read, err := debitDD.parseVariableStringField(record[length:], 34)
if err != nil {
return fieldError("Identifier", err)
}
debitDD.Identifier = value
length += read
value, read, err = debitDD.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("Name", err)
}
debitDD.Name = value
length += read
value, read, err = debitDD.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("AddressLineOne", err)
}
debitDD.Address.AddressLineOne = value
length += read
value, read, err = debitDD.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("AddressLineTwo", err)
}
debitDD.Address.AddressLineTwo = value
length += read
value, read, err = debitDD.parseVariableStringField(record[length:], 35)
if err != nil {
return fieldError("AddressLineThree", err)
}
debitDD.Address.AddressLineThree = value
length += read
if err := debitDD.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (debitDD *AccountDebitedDrawdown) UnmarshalJSON(data []byte) error {
type Alias AccountDebitedDrawdown
aux := struct {
*Alias
}{
(*Alias)(debitDD),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
debitDD.tag = TagAccountDebitedDrawdown
return nil
}
// String returns a fixed-width AccountDebitedDrawdown record
func (debitDD *AccountDebitedDrawdown) String() string {
return debitDD.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns an AccountDebitedDrawdown record formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(181)
buf.WriteString(debitDD.tag)
buf.WriteString(debitDD.IdentificationCodeField())
buf.WriteString(debitDD.FormatIdentifier(options) + Delimiter)
buf.WriteString(debitDD.FormatName(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineOne(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineTwo(options) + Delimiter)
buf.WriteString(debitDD.FormatAddressLineThree(options) + Delimiter)
if options.VariableLengthFields {
return debitDD.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on AccountDebitedDrawdown and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (debitDD *AccountDebitedDrawdown) Validate() error {
if err := debitDD.fieldInclusion(); err != nil {
return err
}
if debitDD.tag != TagAccountDebitedDrawdown {
return fieldError("tag", ErrValidTagForType, debitDD.tag)
}
if err := debitDD.isIdentificationCode(debitDD.IdentificationCode); err != nil {
return fieldError("IdentificationCode", err, debitDD.IdentificationCode)
}
// Can only be these Identification Codes
switch debitDD.IdentificationCode {
case
DemandDepositAccountNumber:
default:
return fieldError("IdentificationCode", ErrIdentificationCode, debitDD.IdentificationCode)
}
if err := debitDD.isAlphanumeric(debitDD.Identifier); err != nil {
return fieldError("Identifier", err, debitDD.Identifier)
}
if err := debitDD.isAlphanumeric(debitDD.Name); err != nil {
return fieldError("Name", err, debitDD.Name)
}
if err := debitDD.isAlphanumeric(debitDD.Address.AddressLineOne); err != nil {
return fieldError("AddressLineOne", err, debitDD.Address.AddressLineOne)
}
if err := debitDD.isAlphanumeric(debitDD.Address.AddressLineTwo); err != nil {
return fieldError("AddressLineTwo", err, debitDD.Address.AddressLineTwo)
}
if err := debitDD.isAlphanumeric(debitDD.Address.AddressLineThree); err != nil {
return fieldError("AddressLineThree", err, debitDD.Address.AddressLineThree)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (debitDD *AccountDebitedDrawdown) fieldInclusion() error {
if debitDD.IdentificationCode == "" {
return fieldError("IdentificationCode", ErrFieldRequired)
}
if debitDD.Identifier == "" {
return fieldError("Identifier", ErrFieldRequired)
}
if debitDD.Name == "" {
return fieldError("Name", ErrFieldRequired)
}
return nil
}
// IdentificationCodeField gets a string of the IdentificationCode field
func (debitDD *AccountDebitedDrawdown) IdentificationCodeField() string {
return debitDD.alphaField(debitDD.IdentificationCode, 1)
}
// IdentifierField gets a string of the Identifier field
func (debitDD *AccountDebitedDrawdown) IdentifierField() string {
return debitDD.alphaField(debitDD.Identifier, 34)
}
// NameField gets a string of the Name field
func (debitDD *AccountDebitedDrawdown) NameField() string {
return debitDD.alphaField(debitDD.Name, 35)
}
// AddressLineOneField gets a string of Address.AddressLineOne field
func (debitDD *AccountDebitedDrawdown) AddressLineOneField() string {
return debitDD.alphaField(debitDD.Address.AddressLineOne, 35)
}
// AddressLineTwoField gets a string of Address.AddressLineTwo field
func (debitDD *AccountDebitedDrawdown) AddressLineTwoField() string {
return debitDD.alphaField(debitDD.Address.AddressLineTwo, 35)
}
// AddressLineThreeField gets a string of Address.AddressLineThree field
func (debitDD *AccountDebitedDrawdown) AddressLineThreeField() string {
return debitDD.alphaField(debitDD.Address.AddressLineThree, 35)
}
// FormatIdentifier returns Identifier formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatIdentifier(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Identifier, 34, options)
}
// FormatName returns Name formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatName(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Name, 35, options)
}
// FormatAddressLineOne returns Address.AddressLineOne formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineOne(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineOne, 35, options)
}
// FormatAddressLineTwo returns Address.AddressLineTwo formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineTwo(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineTwo, 35, options)
}
// FormatAddressLineThree returns Address.AddressLineThree formatted according to the FormatOptions
func (debitDD *AccountDebitedDrawdown) FormatAddressLineThree(options FormatOptions) string {
return debitDD.formatAlphaField(debitDD.Address.AddressLineThree, 35, options)
}