forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreader.go
292 lines (268 loc) · 7.95 KB
/
reader.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
// Copyright 2016 The ACH Authors
// Use of this source code is governed by an Apache License
// license that can be found in the LICENSE file.
package ach
import (
"bufio"
"fmt"
"io"
"strconv"
)
// ParseError is returned for parsing reader errors.
// The first line is 1.
type ParseError struct {
Line int // Line number where the error accurd
Record string // Name of the record type being parsed
Err error // The actual error
}
func (e *ParseError) Error() string {
if e.Record == "" {
return fmt.Sprintf("line:%d %T %s", e.Line, e.Err, e.Err)
}
return fmt.Sprintf("line:%d record:%s %T %s", e.Line, e.Record, e.Err, e.Err)
}
// Reader reads records from a ACH-encoded file.
type Reader struct {
// r handles the IO.Reader sent to be parser.
scanner *bufio.Scanner
// file is ach.file model being built as r is parsed.
File File
// line is the current line being parsed from the input r
line string
// currentBatch is the current Batch entries being parsed
currentBatch Batcher
// line number of the file being parsed
lineNum int
// recordName holds the current record name being parsed.
recordName string
}
// error creates a new ParseError based on err.
func (r *Reader) error(err error) error {
return &ParseError{
Line: r.lineNum,
Record: r.recordName,
Err: err,
}
}
// addCurrentBatch creates the current batch type for the file being read. A successful
// current batch will be added to r.File once parsed.
func (r *Reader) addCurrentBatch(batch Batcher) {
r.currentBatch = batch
}
// NewReader returns a new ACH Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
scanner: bufio.NewScanner(r),
}
}
// Read reads each line of the ACH file and defines which parser to use based
// on the first character of each line. It also enforces ACH formating rules and returns
// the appropriate error if issues are found.
func (r *Reader) Read() (File, error) {
r.lineNum = 0
// read through the entire file
for r.scanner.Scan() {
line := r.scanner.Text()
r.lineNum++
lineLength := len(line)
switch {
case r.lineNum == 1 && lineLength > RecordLength && lineLength%RecordLength == 0:
if err := r.processFixedWidthFile(&line); err != nil {
return r.File, err
}
case lineLength != RecordLength:
msg := fmt.Sprintf(msgRecordLength, lineLength)
err := &FileError{FieldName: "RecordLength", Value: strconv.Itoa(lineLength), Msg: msg}
return r.File, r.error(err)
default:
r.line = line
if err := r.parseLine(); err != nil {
return r.File, err
}
}
}
if (FileHeader{}) == r.File.Header {
// Their must be at least one File Header
r.recordName = "FileHeader"
return r.File, r.error(&FileError{Msg: msgFileHeader})
}
if (FileControl{}) == r.File.Control {
// Their must be at least one File Control
r.recordName = "FileControl"
return r.File, r.error(&FileError{Msg: msgFileControl})
}
return r.File, nil
}
func (r *Reader) processFixedWidthFile(line *string) error {
// it should be safe to parse this byte by byte since ACH files are ascii only
record := ""
for i, c := range *line {
record = record + string(c)
if i > 0 && (i+1)%RecordLength == 0 {
r.line = record
if err := r.parseLine(); err != nil {
return err
}
record = ""
}
}
return nil
}
func (r *Reader) parseLine() error {
switch r.line[:1] {
case fileHeaderPos:
if err := r.parseFileHeader(); err != nil {
return err
}
case batchHeaderPos:
if err := r.parseBatchHeader(); err != nil {
return err
}
case entryDetailPos:
if err := r.parseEntryDetail(); err != nil {
return err
}
case entryAddendaPos:
if err := r.parseAddenda(); err != nil {
return err
}
case batchControlPos:
if err := r.parseBatchControl(); err != nil {
return err
}
if err := r.currentBatch.Validate(); err != nil {
r.recordName = "Batches"
return r.error(err)
}
r.File.AddBatch(r.currentBatch)
r.currentBatch = nil
case fileControlPos:
if r.line[:2] == "99" {
// final blocking padding
break
}
if err := r.parseFileControl(); err != nil {
return err
}
default:
msg := fmt.Sprintf(msgUnknownRecordType, r.line[:1])
return r.error(&FileError{FieldName: "recordType", Value: r.line[:1], Msg: msg})
}
return nil
}
// parseFileHeader takes the input record string and parses the FileHeaderRecord values
func (r *Reader) parseFileHeader() error {
r.recordName = "FileHeader"
if (FileHeader{}) != r.File.Header {
// Their can only be one File Header per File exit
r.error(&FileError{Msg: msgFileHeader})
}
r.File.Header.Parse(r.line)
if err := r.File.Header.Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseBatchHeader takes the input record string and parses the FileHeaderRecord values
func (r *Reader) parseBatchHeader() error {
r.recordName = "BatchHeader"
if r.currentBatch != nil {
// batch header inside of current batch
return r.error(&FileError{Msg: msgFileBatchInside})
}
// Ensure we have a valid batch header before building a batch.
bh := NewBatchHeader()
bh.Parse(r.line)
if err := bh.Validate(); err != nil {
return r.error(err)
}
// Passing SEC type into NewBatch creates a Batcher of SEC code type.
batch, err := NewBatch(BatchParam{
StandardEntryClass: bh.StandardEntryClassCode})
if err != nil {
return r.error(err)
}
batch.SetHeader(bh)
r.addCurrentBatch(batch)
return nil
}
// parseEntryDetail takes the input record string and parses the EntryDetailRecord values
func (r *Reader) parseEntryDetail() error {
r.recordName = "EntryDetail"
if r.currentBatch == nil {
return r.error(&FileError{Msg: msgFileBatchOutside})
}
ed := new(EntryDetail)
ed.Parse(r.line)
if err := ed.Validate(); err != nil {
return r.error(err)
}
r.currentBatch.AddEntry(ed)
return nil
}
// parseAddendaRecord takes the input record string and parses the AddendaRecord values
func (r *Reader) parseAddenda() error {
r.recordName = "Addenda"
if r.currentBatch == nil {
msg := fmt.Sprintf(msgFileBatchOutside)
return r.error(&FileError{FieldName: "Addenda", Msg: msg})
}
if len(r.currentBatch.GetEntries()) == 0 {
return r.error(&FileError{FieldName: "Addenda", Msg: msgFileBatchOutside})
}
entryIndex := len(r.currentBatch.GetEntries()) - 1
entry := r.currentBatch.GetEntries()[entryIndex]
switch sec := r.currentBatch.GetHeader().StandardEntryClassCode; sec {
default:
if entry.AddendaRecordIndicator == 1 {
addenda := Addenda{}
addenda.Parse(r.line)
if err := addenda.Validate(); err != nil {
return r.error(err)
}
r.currentBatch.GetEntries()[entryIndex].AddAddenda(addenda)
} else {
msg := fmt.Sprintf(msgBatchAddendaIndicator)
return r.error(&FileError{FieldName: "AddendaRecordIndicator", Msg: msg})
}
case web, ccd, cor, ppd: // only care for returns
if entry.AddendaRecordIndicator == 1 {
returnAddenda := ReturnAddenda{}
returnAddenda.Parse(r.line)
if err := returnAddenda.Validate(); err != nil {
return r.error(err)
}
r.currentBatch.GetEntries()[entryIndex].AddReturnAddenda(returnAddenda)
} else {
msg := fmt.Sprintf(msgBatchAddendaIndicator)
return r.error(&FileError{FieldName: "AddendaRecordIndicator", Msg: msg})
}
}
return nil
}
// parseBatchControl takes the input record string and parses the BatchControlRecord values
func (r *Reader) parseBatchControl() error {
r.recordName = "BatchControl"
if r.currentBatch == nil {
// batch Control without a current batch
return r.error(&FileError{Msg: msgFileBatchOutside})
}
r.currentBatch.GetControl().Parse(r.line)
if err := r.currentBatch.GetControl().Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseFileControl takes the input record string and parses the FileControlRecord values
func (r *Reader) parseFileControl() error {
r.recordName = "FileControl"
if (FileControl{}) != r.File.Control {
// Can be only one file control per file
return r.error(&FileError{Msg: msgFileControl})
}
r.File.Control.Parse(r.line)
if err := r.File.Control.Validate(); err != nil {
return r.error(err)
}
return nil
}