-
Notifications
You must be signed in to change notification settings - Fork 40
/
routingNumberSummary.go
157 lines (140 loc) · 4.8 KB
/
routingNumberSummary.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
// 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 imagecashletter
import (
"encoding/json"
"fmt"
"strings"
"unicode/utf8"
)
// RoutingNumberSummary Record
type RoutingNumberSummary struct {
// ID is a client defined string used as a reference to this record.
ID string `json:"id"`
// RecordType defines the type of record.
recordType string
// CashLetterRoutingNumber
CashLetterRoutingNumber string `json:"cashLetterRoutingNumber"`
// RoutingNumberTotalAmount
RoutingNumberTotalAmount int `json:"routingNumberTotalAmount"`
// RoutingNumberItemCount
RoutingNumberItemCount int `json:"routingNumberItemCount"`
// UserField identifies a field used at the discretion of users of the standard.
UserField string `json:"userField"`
// reserved is a field reserved for future use. Reserved should be blank.
reserved string
// validator is composed for imagecashletter data validation
validator
// converters is composed for imagecashletter to golang Converters
converters
}
// NewRoutingNumberSummary returns a new RoutingNumberSummary with default values for non exported fields
func NewRoutingNumberSummary() *RoutingNumberSummary {
rns := &RoutingNumberSummary{}
rns.setRecordType()
return rns
}
func (rns *RoutingNumberSummary) setRecordType() {
if rns == nil {
return
}
rns.recordType = "85"
}
// Parse takes the input record string and parses the ImageViewDetail values
func (rns *RoutingNumberSummary) Parse(record string) {
if utf8.RuneCountInString(record) < 55 {
return // line too short
}
// Character position 1-2, Always "85"
rns.setRecordType()
// 03-11
rns.CashLetterRoutingNumber = rns.parseStringField(record[2:11])
// 12-25
rns.RoutingNumberTotalAmount = rns.parseNumField(record[11:25])
// 26-31
rns.RoutingNumberItemCount = rns.parseNumField(record[26:31])
// 32-55
rns.UserField = rns.parseStringField(record[31:55])
// 56-80
rns.reserved = " "
}
func (rns *RoutingNumberSummary) UnmarshalJSON(data []byte) error {
type Alias RoutingNumberSummary
aux := struct {
*Alias
}{
(*Alias)(rns),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
rns.setRecordType()
return nil
}
// String writes the ImageViewDetail struct to a string.
func (rns *RoutingNumberSummary) String() string {
if rns == nil {
return ""
}
var buf strings.Builder
buf.Grow(80)
buf.WriteString(rns.recordType)
buf.WriteString(rns.CashLetterRoutingNumberField())
buf.WriteString(rns.RoutingNumberTotalAmountField())
buf.WriteString(rns.RoutingNumberItemCountField())
buf.WriteString(rns.UserFieldField())
buf.WriteString(rns.reservedField())
return buf.String()
}
// Validate performs imagecashletter format rule checks on the record and returns an error if not Validated
// The first error encountered is returned and stops the parsing.
func (rns *RoutingNumberSummary) Validate() error {
if err := rns.fieldInclusion(); err != nil {
return err
}
if rns.recordType != "85" {
msg := fmt.Sprintf(msgRecordType, 85)
return &FieldError{FieldName: "recordType", Value: rns.recordType, Msg: msg}
}
if err := rns.isAlphanumericSpecial(rns.UserField); err != nil {
return &FieldError{FieldName: "UserField",
Value: rns.UserField, Msg: err.Error()}
}
return nil
}
// fieldInclusion validate mandatory fields are not default values. If fields are
// invalid the Electronic Exchange will be returned.
func (rns *RoutingNumberSummary) fieldInclusion() error {
if rns.recordType == "" {
return &FieldError{FieldName: "recordType",
Value: rns.recordType,
Msg: msgFieldInclusion + ", did you use RoutingNumberSummary()?"}
}
if rns.CashLetterRoutingNumber == "" {
return &FieldError{FieldName: "CashLetterRoutingNumber",
Value: rns.CashLetterRoutingNumber,
Msg: msgFieldInclusion + ", did you use RoutingNumberSummary()?"}
}
return nil
}
// CashLetterRoutingNumberField gets the CashLetterRoutingNumber routing number field
func (rns *RoutingNumberSummary) CashLetterRoutingNumberField() string {
return rns.stringField(rns.CashLetterRoutingNumber, 9)
}
// RoutingNumberTotalAmountField gets a string of RoutingNumberTotalAmount zero padded
func (rns *RoutingNumberSummary) RoutingNumberTotalAmountField() string {
return rns.numericField(rns.RoutingNumberTotalAmount, 14)
}
// RoutingNumberItemCountField gets a string of RoutingNumberItemCount zero padded
func (rns *RoutingNumberSummary) RoutingNumberItemCountField() string {
return rns.numericField(rns.RoutingNumberItemCount, 6)
}
// UserFieldField gets the UserField field
func (rns *RoutingNumberSummary) UserFieldField() string {
return rns.alphaField(rns.UserField, 24)
}
// reservedField gets the reserved field
func (rns *RoutingNumberSummary) reservedField() string {
return rns.alphaField(rns.reserved, 25)
}