forked from moov-io/ach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader.go
298 lines (269 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
293
294
295
296
297
298
// 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"
"errors"
"fmt"
"io"
)
const (
// RecordLength character count of each line representing a letter in a file
RecordLength = 94
)
// 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 {
return fmt.Sprintf("LineNum:%d, RecordName:%s : %s \n", e.Line, e.Record, e.Err)
}
// These are the errors that can be returned in Parse.Error
// additional errors can occur in the Record
var (
ErrFileRead = errors.New("File could not be read")
ErrRecordLen = errors.New("Wrong number of characters in record expected 94")
ErrBatchHeader = errors.New("None or More than one Batch Headers exist in Batch.")
ErrBatchControl = errors.New("No terminating batch control record found in file for previous batch")
ErrUnknownRecordType = errors.New("Unhandled Record Type")
ErrFileHeader = errors.New("None or more than one File Headers exists")
ErrFileControl = errors.New("None or more than one File Control exists")
ErrEntryOutside = errors.New("Entry Detail record outside of a batch")
ErrAddendaOutside = errors.New("Entry Addenda without a preceding Entry Detail")
ErrAddendaNoIndicator = errors.New("Addenda without Entry Detail Addenda Inicator")
)
// currently support SEC codes
const (
ppd = "PPD"
)
// 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 *Batch
// 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,
}
}
// NewReader returns a new Reader that reads from r.
func NewReader(r io.Reader) *Reader {
return &Reader{
scanner: bufio.NewScanner(r),
currentBatch: NewBatch(),
}
}
// 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)
// handle the situation where there are no line breaks
if r.lineNum == 1 && lineLength > RecordLength && lineLength%RecordLength == 0 {
if err := r.processFixedWidthFile(&line); err != nil {
return r.File, r.error(err)
}
break
}
// Only 94 ASCII characters to a line
if lineLength != RecordLength {
return r.File, r.error(ErrRecordLen)
}
r.line = line
if err := r.parseLine(); err != nil {
return r.File, r.error(err)
}
}
if err := r.scanner.Err(); err != nil {
return r.File, r.error(ErrFileRead)
}
if (FileHeader{}) == r.File.Header {
// Their must be at least one File Header
return r.File, r.error(ErrFileHeader)
}
if (FileControl{}) == r.File.Control {
// Their must be at least one File Control
return r.File, r.error(ErrFileControl)
}
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 headerPos:
if err := r.parseFileHeader(); err != nil {
return err
}
case batchPos:
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 err
}
r.File.AddBatch(r.currentBatch)
r.currentBatch = new(Batch)
case fileControlPos:
if r.line[:2] == "99" {
// final blocking padding
break
}
if err := r.parseFileControl(); err != nil {
return err
}
if err := r.File.Validate(); err != nil {
return err
}
default:
return r.error(ErrUnknownRecordType)
}
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
return ErrFileHeader
}
r.File.Header.Parse(r.line)
if err := r.File.Header.Validate(); err != nil {
return 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.Header.ServiceClassCode != 0 {
// Ensure we have an empty Batch
return ErrBatchHeader
}
r.currentBatch.Header.Parse(r.line)
if err := r.currentBatch.Header.Validate(); err != nil {
return err
}
return nil
}
// parseEntryDetail takes the input record string and parses the EntryDetailRecord values
func (r *Reader) parseEntryDetail() error {
r.recordName = "EntryDetail"
if r.currentBatch.Header.ServiceClassCode == 0 {
return ErrEntryOutside
}
sec := r.currentBatch.Header.StandardEntryClassCode
if sec == ppd {
ed := new(EntryDetail)
ed.Parse(r.line)
if err := ed.Validate(); err != nil {
return err
}
r.currentBatch.AddEntryDetail(ed)
} else {
return errors.New("Support for EntryDetail of SEC(standard entry class): " +
r.currentBatch.Header.StandardEntryClassCode + ", has not been implemented")
}
return nil
}
// parseAddendaRecord takes the input record string and parses the AddendaRecord values
func (r *Reader) parseAddenda() error {
r.recordName = "Addenda"
if len(r.currentBatch.Entries) == 0 {
return ErrAddendaOutside
}
entryIndex := len(r.currentBatch.Entries) - 1
entry := r.currentBatch.Entries[entryIndex]
sec := r.currentBatch.Header.StandardEntryClassCode
if sec == ppd {
if entry.AddendaRecordIndicator == 1 {
addenda := Addenda{}
addenda.Parse(r.line)
if err := addenda.Validate(); err != nil {
return err
}
r.currentBatch.Entries[entryIndex].AddAddenda(addenda)
} else {
return ErrAddendaNoIndicator
}
}
// Currently Dead code until Additional SEC codes are supported by BatchHeader
/*
else {
return errors.New("Support for Addenda records for SEC(Standard Entry Class): " +
r.currentBatch.Header.StandardEntryClassCode + ", has not been implemented")
}
*/
return nil
}
// parseBatchControl takes the input record string and parses the BatchControlRecord values
func (r *Reader) parseBatchControl() error {
r.recordName = "BatchControl"
//fmt.Printf("control: %+v \n", r.currentBatch.Control)
r.currentBatch.Control.Parse(r.line)
if err := r.currentBatch.Control.Validate(); err != nil {
return 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 {
// Their can only be one File Control per File exit
return ErrFileControl
}
r.File.Control.Parse(r.line)
if err := r.File.Control.Validate(); err != nil {
return err
}
return nil
}