-
Notifications
You must be signed in to change notification settings - Fork 39
/
reader.go
823 lines (765 loc) · 25.7 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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
// 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 (
"bufio"
"encoding/binary"
"errors"
"fmt"
"io"
"strconv"
"github.com/gdamore/encoding"
)
// ParseError is returned for parsing reader errors.
// The first line is 1.
type ParseError struct {
Line int // Line number where the error occurred
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)
}
func (e *ParseError) Unwrap() error {
return 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
// func used to decode line to desired encoding ie. ASCII,EBCDIC
decodeLine DecodeLineFn
// line is the current line being parsed from the input r
line string
// currentCashLetter is the current CashLetter being parsed
currentCashLetter CashLetter
// 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,
}
}
// addCurrentCashLetter creates the current cash letter for the file being read. A successful
// currentCashLetter will be added to r.File once parsed.
func (r *Reader) addCurrentCashLetter(cashLetter CashLetter) {
r.currentCashLetter = cashLetter
}
// addCurrentBundle creates the CurrentBundle for the file being read. A successful
// currentBundle will be added to r.File once parsed.
func (r *Reader) addCurrentBundle(bundle *Bundle) {
r.currentCashLetter.currentBundle = bundle
}
// addCurrentRoutingNumberSummary creates the CurrentRoutingNumberSummary for the file being read. A successful
// currentRoutingNumberSummary will be added to r.File once parsed.
func (r *Reader) addCurrentRoutingNumberSummary(rns *RoutingNumberSummary) {
r.currentCashLetter.currentRoutingNumberSummary = rns
}
// NewReader returns a new ACH Reader that reads from r.
func NewReader(r io.Reader, opts ...ReaderOption) *Reader {
f := NewFile()
f.Control = FileControl{}
reader := &Reader{
File: *f,
scanner: bufio.NewScanner(r),
decodeLine: Passthrough,
}
for _, opt := range opts {
opt(reader)
}
return reader
}
// DecodeLineFn is used to decode a scanned line into desired encoding.
// Depending on X9 spec, cashletter could be encoded as ASCII or EBCDIC
type DecodeLineFn func(lineIn string) (lineOut string, err error)
// Passthrough will return line as is
func Passthrough(lineIn string) (lineOut string, err error) {
return lineIn, nil
}
// DecodeEBCDIC will decode a line from EBCDIC-0037 to UTF-8
func DecodeEBCDIC(lineIn string) (string, error) {
lineOut, err := encoding.EBCDIC.NewDecoder().String(lineIn)
if err != nil {
return "", fmt.Errorf("error decoding '%X' as EBCDIC: %v\n", lineIn, err)
}
return lineOut, nil
}
// ReaderOption can be used to change default behavior of Reader
type ReaderOption func(*Reader)
// ReadVariableLineLengthOption allows Reader to split imagecashletter files based on encoded line lengths
func ReadVariableLineLengthOption() ReaderOption {
scanVariableLengthLines := func(data []byte, atEOF bool) (advance int, token []byte, err error) {
if atEOF && len(data) == 0 {
return 0, nil, nil
} else if len(data) < 4 && atEOF {
// we ran out of bytes and we're at the end of the file
return 0, nil, io.ErrUnexpectedEOF
} else if len(data) < 4 {
// we need at least the control bytes
return 0, nil, nil
}
// line length can be variable
// use the 4 control bytes at the beginning of a line to determine its length
ctrl := data[0:4]
dataLen := int(binary.BigEndian.Uint32(ctrl))
lineLen := 4 + dataLen
if lineLen <= len(data) {
// return line while accounting for control bytes
return lineLen, data[4:lineLen], nil
} else if lineLen > len(data) && atEOF {
// we need more data, but there is no more data to read
return 0, nil, io.ErrUnexpectedEOF
}
// request more data.
return 0, nil, nil
}
return func(r *Reader) {
r.scanner.Split(scanVariableLengthLines)
}
}
// ReadEbcdicEncodingOption allows Reader to decode scanned lines from EBCDIC to UTF-8
func ReadEbcdicEncodingOption() ReaderOption {
return func(r *Reader) {
r.decodeLine = DecodeEBCDIC
}
}
// BufferSizeOption creates a byte slice of the specified size and uses it as the buffer
// for the Reader's internal scanner. You may need to set this when processing files that
// contain check details exceeding bufio.MaxScanTokenSize (64 kB).
func BufferSizeOption(size int) ReaderOption {
return func(r *Reader) {
r.scanner.Buffer(make([]byte, size), size)
}
}
// Read reads each line of the imagecashletter file and defines which parser to use based
// on the first character of each line. It also enforces imagecashletter formatting 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() {
r.line = r.scanner.Text()
r.lineNum++
lineLength := len(r.line)
if lineLength < 80 {
msg := fmt.Sprintf(msgRecordLength, lineLength)
err := &FileError{FieldName: "RecordLength", Value: strconv.Itoa(lineLength), Msg: msg}
return r.File, r.error(err)
}
if err := r.parseLine(); err != nil {
return r.File, err
}
}
if scanErr := r.scanner.Err(); scanErr != nil {
err := &FileError{FieldName: "LineNumber", Value: strconv.Itoa(r.lineNum), Msg: scanErr.Error()}
return r.File, r.error(err)
}
if (FileHeader{}) == r.File.Header {
// There must be at least one File Header
r.recordName = "FileHeader"
return r.File, r.error(&FileError{Msg: msgFileHeader})
}
if (FileControl{}) == r.File.Control {
// There 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) parseLine() error { //nolint:gocyclo
switch r.line[:2] {
case fileHeaderPos, fileHeaderEbcPos:
if err := r.parseFileHeader(); err != nil {
return err
}
case cashLetterHeaderPos, cashLetterHeaderEbcPos:
if err := r.parseCashLetterHeader(); err != nil {
return err
}
case bundleHeaderPos, bundleHeaderEbcPos:
if err := r.parseBundleHeader(); err != nil {
return err
}
case checkDetailPos, checkDetailEbcPos:
if err := r.parseCheckDetail(); err != nil {
return err
}
case checkDetailAddendumAPos, checkDetailAddendumAEbcPos:
if err := r.parseCheckDetailAddendumA(); err != nil {
return err
}
case checkDetailAddendumBPos, checkDetailAddendumBEbcPos:
if err := r.parseCheckDetailAddendumB(); err != nil {
return err
}
case checkDetailAddendumCPos, checkDetailAddendumCEbcPos:
if err := r.parseCheckDetailAddendumC(); err != nil {
return err
}
case imageViewDetailPos, imageViewDetailEbcPos:
if err := r.parseImageViewDetail(); err != nil {
return err
}
case imageViewDataPos, imageViewDataEbcPos:
if err := r.parseImageViewData(); err != nil {
return err
}
case imageViewAnalysisPos, imageViewAnalysisEbcPos:
if err := r.parseImageViewAnalysis(); err != nil {
return err
}
case returnDetailPos, returnDetailEbcPos:
if err := r.parseReturnDetail(); err != nil {
return err
}
case returnAddendumAPos, returnAddendumAPEbcos:
if err := r.parseReturnDetailAddendumA(); err != nil {
return err
}
case returnAddendumBPos, returnAddendumBEbcPos:
if err := r.parseReturnDetailAddendumB(); err != nil {
return err
}
case returnAddendumCPos, returnAddendumCEbcPos:
if err := r.parseReturnDetailAddendumC(); err != nil {
return err
}
case returnAddendumDPos, returnAddendumDEbcPos:
if err := r.parseReturnDetailAddendumD(); err != nil {
return err
}
case creditPos, creditEbcPos:
if err := r.parseCredit(); err != nil {
return err
}
case creditItemPos, creditItemEbcPos:
if err := r.parseCreditItem(); err != nil {
return err
}
case bundleControlPos, bundleControlEbcPos:
if err := r.parseBundleControl(); err != nil {
return err
}
if r.currentCashLetter.currentBundle == nil {
r.error(&FileError{Msg: msgFileBundleControl})
}
// Add Bundle or ReturnBundle to CashLetter
if r.currentCashLetter.currentBundle != nil {
if err := r.currentCashLetter.currentBundle.Validate(); err != nil {
r.recordName = "Bundles"
return r.error(err)
}
r.currentCashLetter.AddBundle(r.currentCashLetter.currentBundle)
r.currentCashLetter.currentBundle = new(Bundle)
}
case routingNumberSummaryPos, routingNumberSummaryEbcPos:
if err := r.parseRoutingNumberSummary(); err != nil {
return err
}
r.currentCashLetter.AddRoutingNumberSummary(r.currentCashLetter.currentRoutingNumberSummary)
r.currentCashLetter.currentRoutingNumberSummary = new(RoutingNumberSummary)
case cashLetterControlPos, cashLetterControlEbcPos:
// This is needed for validation od CashLetterControl since SettlementDate
// is a conditional field and is only available for certain types of CashLetters.
header := r.currentCashLetter.CashLetterHeader
if header == nil {
return errors.New("missing CashLetterHeader")
}
if err := r.parseCashLetterControl(); err != nil {
return err
}
if err := r.currentCashLetter.Validate(); err != nil {
r.recordName = "CashLetters"
return r.error(err)
}
r.File.AddCashLetter(r.currentCashLetter)
r.currentCashLetter = CashLetter{}
case fileControlPos, fileControlEbcPos:
if err := r.parseFileControl(); err != nil {
return err
}
default:
msg := fmt.Sprintf(msgUnknownRecordType, r.line[:2])
return r.error(&FileError{FieldName: "recordType", Value: r.line[:2], Msg: msg})
}
return nil
}
// parseFileHeader takes the input record string and parses the FileHeader values
func (r *Reader) parseFileHeader() error {
r.recordName = "FileHeader"
if (FileHeader{}) != r.File.Header {
// There can only be one File Header per File
r.error(&FileError{Msg: msgFileHeader})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
r.File.Header.Parse(lineOut)
// Ensure valid FileHeader
if err := r.File.Header.Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseCashLetterHeader takes the input record string and parses the CashLetterHeader values
func (r *Reader) parseCashLetterHeader() error {
r.recordName = "CashLetterHeader"
if r.currentCashLetter.CashLetterHeader != nil {
// CashLetterHeader inside of current cash letter
return r.error(&FileError{Msg: msgFileCashLetterInside})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
clh := NewCashLetterHeader()
clh.Parse(lineOut)
// Ensure we have a valid CashLetterHeader
if err := clh.Validate(); err != nil {
return r.error(err)
}
// Passing CashLetterHeader into NewCashLetter creates a CashLetter
cl := NewCashLetter(clh)
r.addCurrentCashLetter(cl)
return nil
}
// parseBundleHeader takes the input record string and parses the BundleHeader values
func (r *Reader) parseBundleHeader() error {
r.recordName = "BundleHeader"
if r.currentCashLetter.currentBundle != nil {
// BundleHeader inside of current Bundle
if r.currentCashLetter.currentBundle.BundleHeader != nil {
return r.error(&FileError{Msg: msgFileBundleInside})
}
}
// Ensure we have a valid bundle header before building a bundle.
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
bh := NewBundleHeader()
bh.Parse(lineOut)
if err := bh.Validate(); err != nil {
return r.error(err)
}
// Passing BundleHeader into NewBundle creates a Bundle
bundle := NewBundle(bh)
r.addCurrentBundle(bundle)
return nil
}
// parseCheckDetail takes the input record string and parses the CheckDetail values
func (r *Reader) parseCheckDetail() error {
r.recordName = "CheckDetail"
if r.currentCashLetter.currentBundle == nil {
return r.error(&FileError{Msg: msgFileBundleOutside})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
cd := new(CheckDetail)
cd.Parse(lineOut)
// Ensure valid CheckDetail
if err := cd.Validate(); err != nil {
return r.error(err)
}
// Add CheckDetail
if r.currentCashLetter.currentBundle.BundleHeader != nil {
r.currentCashLetter.currentBundle.AddCheckDetail(cd)
}
return nil
}
// parseCheckDetailAddendumA takes the input record string and parses the CheckDetailAddendumA values
func (r *Reader) parseCheckDetailAddendumA() error {
r.recordName = "CheckDetailAddendumA"
if r.currentCashLetter.currentBundle.GetChecks() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "CheckDetailAddendumA", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
cdAddendumA := NewCheckDetailAddendumA()
cdAddendumA.Parse(lineOut)
if err := cdAddendumA.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
// r.currentCashLetter.currentBundle.Checks[entryIndex].CheckDetailAddendumA = cdAddendumA
r.currentCashLetter.currentBundle.Checks[entryIndex].AddCheckDetailAddendumA(cdAddendumA)
return nil
}
// parseCheckDetailAddendumB takes the input record string and parses the CheckDetailAddendumB values
func (r *Reader) parseCheckDetailAddendumB() error {
r.recordName = "CheckDetailAddendumB"
if r.currentCashLetter.currentBundle.GetChecks() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "CheckDetailAddendumB", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
cdAddendumB := NewCheckDetailAddendumB()
cdAddendumB.Parse(lineOut)
if err := cdAddendumB.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
r.currentCashLetter.currentBundle.Checks[entryIndex].AddCheckDetailAddendumB(cdAddendumB)
return nil
}
// parseCheckDetailAddendumC takes the input record string and parses the CheckDetailAddendumC values
func (r *Reader) parseCheckDetailAddendumC() error {
r.recordName = "CheckDetailAddendumC"
if r.currentCashLetter.currentBundle.GetChecks() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "CheckDetailAddendumC", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
cdAddendumC := NewCheckDetailAddendumC()
cdAddendumC.Parse(lineOut)
if err := cdAddendumC.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
r.currentCashLetter.currentBundle.Checks[entryIndex].AddCheckDetailAddendumC(cdAddendumC)
return nil
}
// parseReturnDetail takes the input record string and parses the ReturnDetail values
func (r *Reader) parseReturnDetail() error {
r.recordName = "ReturnDetail"
if r.currentCashLetter.currentBundle == nil {
return r.error(&FileError{Msg: msgFileBundleOutside})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rd := new(ReturnDetail)
rd.Parse(lineOut)
if err := rd.Validate(); err != nil {
return r.error(err)
}
if r.currentCashLetter.currentBundle.BundleHeader != nil {
r.currentCashLetter.currentBundle.AddReturnDetail(rd)
}
return nil
}
// parseReturnDetailAddendumA takes the input record string and parses the ReturnDetailAddendumA values
func (r *Reader) parseReturnDetailAddendumA() error {
r.recordName = "ReturnDetailAddendumA"
if r.currentCashLetter.currentBundle.GetReturns() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ReturnDetailAddendumA", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rdAddendumA := NewReturnDetailAddendumA()
rdAddendumA.Parse(lineOut)
if err := rdAddendumA.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
// r.currentCashLetter.currentBundle.Returns[entryIndex].ReturnDetailAddendumA = rdAddendumA
r.currentCashLetter.currentBundle.Returns[entryIndex].AddReturnDetailAddendumA(rdAddendumA)
return nil
}
// parseReturnDetailAddendumB takes the input record string and parses the ReturnDetailAddendumB values
func (r *Reader) parseReturnDetailAddendumB() error {
r.recordName = "ReturnDetailAddendumB"
if r.currentCashLetter.currentBundle.GetReturns() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ReturnDetailAddendumB", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rdAddendumB := NewReturnDetailAddendumB()
rdAddendumB.Parse(lineOut)
if err := rdAddendumB.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddReturnDetailAddendumB(rdAddendumB)
return nil
}
// parseReturnDetailAddendumC takes the input record string and parses the ReturnDetailAddendumC values
func (r *Reader) parseReturnDetailAddendumC() error {
r.recordName = "ReturnDetailAddendumC"
if r.currentCashLetter.currentBundle.GetReturns() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ReturnDetailAddendumC", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rdAddendumC := NewReturnDetailAddendumC()
rdAddendumC.Parse(lineOut)
if err := rdAddendumC.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddReturnDetailAddendumC(rdAddendumC)
return nil
}
// parseReturnDetail*AddendumD takes the input record string and parses the ReturnDetail*AddendumD values
func (r *Reader) parseReturnDetailAddendumD() error {
r.recordName = "ReturnDetailAddendumD"
if r.currentCashLetter.currentBundle.GetReturns() == nil {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ReturnDetailAddendumD", Msg: msg})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rdAddendumD := NewReturnDetailAddendumD()
rdAddendumD.Parse(lineOut)
if err := rdAddendumD.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddReturnDetailAddendumD(rdAddendumD)
return nil
}
// parseImageViewDetail takes the input record string and parses the ImageViewDetail values
func (r *Reader) parseImageViewDetail() error {
r.recordName = "ImageViewDetail"
if err := r.ImageViewDetail(); err != nil {
return err
}
return nil
}
// ImageViewDetail takes the input record string and parses ImageViewDetail for a check
func (r *Reader) ImageViewDetail() error {
if r.currentCashLetter.currentBundle.GetChecks() != nil {
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
ivDetail := NewImageViewDetail()
ivDetail.Parse(lineOut)
if err := ivDetail.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
r.currentCashLetter.currentBundle.Checks[entryIndex].AddImageViewDetail(ivDetail)
} else if r.currentCashLetter.currentBundle.GetReturns() != nil {
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
ivDetail := NewImageViewDetail()
ivDetail.Parse(lineOut)
if err := ivDetail.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddImageViewDetail(ivDetail)
} else {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ImageViewDetail", Msg: msg})
}
return nil
}
// parseImageViewData takes the input record string and parses the ImageViewData values
func (r *Reader) parseImageViewData() error {
r.recordName = "ImageViewData"
if err := r.ImageViewData(); err != nil {
return err
}
return nil
}
// ImageViewData takes the input record string and parses ImageViewData for a check
func (r *Reader) ImageViewData() error {
if r.currentCashLetter.currentBundle.GetChecks() != nil {
ivData := NewImageViewData()
ivData.ParseAndDecode(r.line, r.decodeLine)
if err := ivData.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
r.currentCashLetter.currentBundle.Checks[entryIndex].AddImageViewData(ivData)
} else if r.currentCashLetter.currentBundle.GetReturns() != nil {
ivData := NewImageViewData()
ivData.ParseAndDecode(r.line, r.decodeLine)
if err := ivData.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddImageViewData(ivData)
} else {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ImageViewData", Msg: msg})
}
return nil
}
// parseImageViewAnalysis takes the input record string and parses ImageViewAnalysis values
func (r *Reader) parseImageViewAnalysis() error {
r.recordName = "ImageViewAnalysis"
if err := r.ImageViewAnalysis(); err != nil {
return err
}
return nil
}
// ImageViewAnalysis takes the input record string and parses ImageViewAnalysis for a check
func (r *Reader) ImageViewAnalysis() error {
if r.currentCashLetter.currentBundle.GetChecks() != nil {
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
ivAnalysis := NewImageViewAnalysis()
ivAnalysis.Parse(lineOut)
if err := ivAnalysis.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetChecks()) - 1
r.currentCashLetter.currentBundle.Checks[entryIndex].AddImageViewAnalysis(ivAnalysis)
} else if r.currentCashLetter.currentBundle.GetReturns() != nil {
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
ivAnalysis := NewImageViewAnalysis()
ivAnalysis.Parse(lineOut)
if err := ivAnalysis.Validate(); err != nil {
return r.error(err)
}
entryIndex := len(r.currentCashLetter.currentBundle.GetReturns()) - 1
r.currentCashLetter.currentBundle.Returns[entryIndex].AddImageViewAnalysis(ivAnalysis)
} else {
msg := fmt.Sprint(msgFileBundleOutside)
return r.error(&FileError{FieldName: "ImageViewAnalysis", Msg: msg})
}
return nil
}
// parseCredit takes the input record string and parses the Credit values
func (r *Reader) parseCredit() error {
// Current implementation has the credit letter outside the bundle but within the cash letter
r.recordName = "Credit"
if r.currentCashLetter.CashLetterHeader == nil {
return r.error(&FileError{Msg: msgFileCredit})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
cr := new(Credit)
cr.Parse(lineOut)
if err := cr.Validate(); err != nil {
return r.error(err)
}
r.currentCashLetter.AddCredit(cr)
return nil
}
// parseCreditItem takes the input record string and parses the CreditItem values
func (r *Reader) parseCreditItem() error {
// Current implementation has the credit letter outside the bundle but within the cash letter
r.recordName = "CreditItem"
if r.currentCashLetter.CashLetterHeader == nil {
return r.error(&FileError{Msg: msgFileCreditItem})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
ci := new(CreditItem)
ci.Parse(lineOut)
if err := ci.Validate(); err != nil {
return r.error(err)
}
r.currentCashLetter.AddCreditItem(ci)
return nil
}
// parseBundleControl takes the input record string and parses the BundleControl values
func (r *Reader) parseBundleControl() error {
r.recordName = "BundleControl"
if r.currentCashLetter.currentBundle == nil || r.currentCashLetter.currentBundle.BundleControl == nil {
return r.error(&FileError{Msg: msgFileBundleControl})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
r.currentCashLetter.currentBundle.GetControl().Parse(lineOut)
if err := r.currentCashLetter.currentBundle.GetControl().Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseRoutingNumberSummary takes the input record string and parses the RoutingNumberSummary values
func (r *Reader) parseRoutingNumberSummary() error {
r.recordName = "RoutingNumberSummary"
if r.currentCashLetter.CashLetterHeader == nil {
return r.error(&FileError{Msg: msgFileRoutingNumberSummary})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
rns := NewRoutingNumberSummary()
rns.Parse(lineOut)
if err := rns.Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseCashLetterControl takes the input record string and parses the CashLetterControl values
func (r *Reader) parseCashLetterControl() error {
r.recordName = "CashLetterControl"
if r.currentCashLetter.CashLetterHeader == nil {
// CashLetterControl without a current CashLetter
return r.error(&FileError{Msg: msgFileCashLetterControl})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
r.currentCashLetter.GetControl().Parse(lineOut)
// Ensure valid CashLetterControl
if err := r.currentCashLetter.GetControl().Validate(); err != nil {
return r.error(err)
}
return nil
}
// parseFileControl takes the input record string and parses the FileControl 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})
}
lineOut, err := r.decodeLine(r.line)
if err != nil {
return err
}
r.File.Control.Parse(lineOut)
// Ensure valid FileControl
if err := r.File.Control.Validate(); err != nil {
return r.error(err)
}
return nil
}