-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathwriter.go
336 lines (300 loc) · 8.77 KB
/
writer.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
// 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"
"github.com/gdamore/encoding"
)
// Writer writes an ImageCashLetter/X9 File to an encoded format.
//
// Callers should use NewWriter to create a new instance and apply WriterOptions
// as needed to properly encode files for their usecase.
type Writer struct {
w *bufio.Writer
lineNum int // current line being written
VariableLineLength bool
EbcdicEncoding bool
}
// NewWriter returns a new Writer that writes to w.
func NewWriter(w io.Writer, opts ...WriterOption) *Writer {
writer := &Writer{
w: bufio.NewWriter(w),
}
for _, opt := range opts {
opt(writer)
}
return writer
}
// WriterOption allows Writer to be configured to write in different formats
type WriterOption func(w *Writer)
// WriteVariableLineLengthOption allows Writer to write control bytes ahead of record to describe how long the line is
// Follows DSTU microformat as defined https://www.frbservices.org/assets/financial-services/check/setup/frb-x937-standards-reference.pdf
func WriteVariableLineLengthOption() WriterOption {
return func(w *Writer) {
w.VariableLineLength = true
}
}
// WriteEbcdicEncodingOption allows Writer to write file in EBCDIC
// Follows DSTU microformat as defined https://www.frbservices.org/assets/financial-services/check/setup/frb-x937-standards-reference.pdf
func WriteEbcdicEncodingOption() WriterOption {
return func(w *Writer) {
w.EbcdicEncoding = true
}
}
func (w *Writer) writeLine(record FileRecord) error {
line := record.String()
lineLength := len(line)
if w.VariableLineLength {
ctrl := make([]byte, 4)
if lineLength < 0 || !validSizeInt(lineLength) {
return errors.New("writeLine: variable line length can't be negative or overflow")
}
binary.BigEndian.PutUint32(ctrl, uint32(lineLength)) //nolint:gosec
if _, err := w.w.Write(ctrl); err != nil {
return err
}
}
if w.EbcdicEncoding {
if ivData, ok := record.(*ImageViewData); ok {
// need to encode everything other than binary image into EBCDIC
encoded, err := encoding.EBCDIC.NewEncoder().String(ivData.toString(false))
if err != nil {
return err
}
if _, err := w.w.WriteString(encoded); err != nil {
return err
}
if _, err := w.w.Write(ivData.ImageData); err != nil {
return err
}
} else {
// no binary data in record, encode entire line
encoded, err := encoding.EBCDIC.NewEncoder().String(line)
if err != nil {
return err
}
if _, err := w.w.WriteString(encoded); err != nil {
return err
}
}
} else {
// ASCII encoding by default
if _, err := w.w.WriteString(line); err != nil {
return err
}
}
if !w.VariableLineLength {
if _, err := w.w.WriteString("\n"); err != nil {
return err
}
}
w.lineNum++
return nil
}
// Writer writes a single imagecashletter.file record to w
func (w *Writer) Write(file *File) error {
if file == nil {
return ErrNilFile
}
if err := file.Validate(); err != nil {
return err
}
w.lineNum = 0
// Iterate over all records in the file
if err := w.writeLine(&file.Header); err != nil {
return err
}
if err := w.writeCashLetter(file); err != nil {
return err
}
if err := w.writeLine(&file.Control); err != nil {
return err
}
return w.w.Flush()
}
// Flush writes any buffered data to the underlying io.Writer.
// To check if an error occurred during the Flush, call Error.
func (w *Writer) Flush() {
w.w.Flush()
}
// writeCashLetter writes a CashLetter to a file
func (w *Writer) writeCashLetter(file *File) error {
for _, cl := range file.CashLetters {
if err := w.writeLine(cl.GetHeader()); err != nil {
return err
}
for _, ci := range cl.GetCreditItems() {
if err := w.writeLine(ci); err != nil {
return err
}
}
for _, credit := range cl.GetCredits() {
if err := w.writeLine(credit); err != nil {
return err
}
}
if err := w.writeBundle(cl); err != nil {
return err
}
for _, rns := range cl.GetRoutingNumberSummary() {
if err := w.writeLine(rns); err != nil {
return err
}
}
if err := w.writeLine(cl.GetControl()); err != nil {
return err
}
}
return nil
}
// writeBundle writes a Bundle to a CashLetter
func (w *Writer) writeBundle(cl CashLetter) error {
for _, b := range cl.GetBundles() {
if err := w.writeLine(b.GetHeader()); err != nil {
return err
}
if len(b.Checks) > 0 {
if err := w.writeCheckDetail(b); err != nil {
return err
}
}
if len(b.Returns) > 0 {
if err := w.writeReturnDetail(b); err != nil {
return err
}
}
if err := w.writeLine(b.GetControl()); err != nil {
return err
}
}
return nil
}
// writeCheckDetail writes a CheckDetail to a Bundle
func (w *Writer) writeCheckDetail(b *Bundle) error {
for _, cd := range b.GetChecks() {
if err := w.writeLine(cd); err != nil {
return err
}
// Write CheckDetailsAddendum (A, B, C)
if err := w.writeCheckDetailAddendum(cd); err != nil {
return err
}
if err := w.writeCheckImageView(cd); err != nil {
return err
}
}
return nil
}
// writeCheckDetailAddendum writes a CheckDetailAddendum (A, B, C) to a CheckDetail
func (w *Writer) writeCheckDetailAddendum(cd *CheckDetail) error {
addendumA := cd.GetCheckDetailAddendumA()
for i := range addendumA {
if err := w.writeLine(&addendumA[i]); err != nil {
return err
}
}
addendumB := cd.GetCheckDetailAddendumB()
for i := range addendumB {
if err := w.writeLine(&addendumB[i]); err != nil {
return err
}
}
addendumC := cd.GetCheckDetailAddendumC()
for i := range addendumC {
if err := w.writeLine(&addendumC[i]); err != nil {
return err
}
}
return nil
}
// writeReturnDetail writes a ReturnDetail to a ReturnBundle
func (w *Writer) writeReturnDetail(b *Bundle) error {
for _, rd := range b.GetReturns() {
if err := w.writeLine(rd); err != nil {
return err
}
// Write ReturnDetailAddendum (A, B, C, D)
if err := w.writeReturnDetailAddendum(rd); err != nil {
return err
}
if err := w.writeReturnImageView(rd); err != nil {
return err
}
}
return nil
}
// writeReturnDetailAddendum writes a ReturnDetailAddendum (A, B, C, D) to a ReturnDetail
func (w *Writer) writeReturnDetailAddendum(rd *ReturnDetail) error {
addendumA := rd.GetReturnDetailAddendumA()
for i := range addendumA {
if err := w.writeLine(&addendumA[i]); err != nil {
return err
}
}
addendumB := rd.GetReturnDetailAddendumB()
for i := range addendumB {
if err := w.writeLine(&addendumB[i]); err != nil {
return err
}
}
addendumC := rd.GetReturnDetailAddendumC()
for i := range addendumC {
if err := w.writeLine(&addendumC[i]); err != nil {
return err
}
}
addendumD := rd.GetReturnDetailAddendumD()
for i := range addendumD {
if err := w.writeLine(&addendumD[i]); err != nil {
return err
}
}
return nil
}
// writeCheckImageView writes ImageViews (Detail, Data, Analysis) to a CheckDetail
func (w *Writer) writeCheckImageView(cd *CheckDetail) error {
return w.writeImageView(cd.GetImageViewDetail(), cd.GetImageViewData(), cd.GetImageViewAnalysis())
}
// writeReturnImageView writes ImageViews (Detail, Data, Analysis) to a ReturnDetail
func (w *Writer) writeReturnImageView(rd *ReturnDetail) error {
return w.writeImageView(rd.GetImageViewDetail(), rd.GetImageViewData(), rd.GetImageViewAnalysis())
}
// writeImageView writes ImageViews (Detail, Data, Analysis) in the order expected by the FRB
func (w *Writer) writeImageView(ivDetail []ImageViewDetail, ivData []ImageViewData, ivAnalysis []ImageViewAnalysis) error {
// TODO: Add validator to ensure that each imageViewDetail has a corresponding imageViewData and imageViewAnalysis
// for now enforce that all images have data and analysis or no images have data and analysis
if len(ivData) > 0 && len(ivData) != len(ivDetail) {
// should be same number of imageViewData as imageViewDetail
msg := fmt.Sprintf(msgBundleImageDetailCount, len(ivData))
return &BundleError{FieldName: "ImageViewData", Msg: msg}
}
if len(ivAnalysis) > 0 && len(ivAnalysis) != len(ivDetail) {
// should same number of imageViewAnalysis and imageViewDetail
msg := fmt.Sprintf(msgBundleImageDetailCount, len(ivAnalysis))
return &BundleError{FieldName: "ImageViewAnalysis", Msg: msg}
}
// FRB asks that imageViewDetail should immediately be followed by its corresponding data and analysis
for i := range ivDetail {
if err := w.writeLine(&ivDetail[i]); err != nil {
return err
}
if len(ivData) > 0 && len(ivData) >= i-1 {
ivData := ivData[i]
if err := w.writeLine(&ivData); err != nil {
return err
}
}
if len(ivAnalysis) > 0 && len(ivAnalysis) >= i-1 {
ivAnalysis := ivAnalysis[i]
if err := w.writeLine(&ivAnalysis); err != nil {
return err
}
}
}
return nil
}