-
Notifications
You must be signed in to change notification settings - Fork 40
/
file.go
111 lines (95 loc) · 2.89 KB
/
file.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
// 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 wire
import (
"bytes"
"encoding/json"
"fmt"
)
// File contains the structures of a parsed WIRE File.
type File struct {
ID string `json:"id"`
FEDWireMessage FEDWireMessage `json:"fedWireMessage"`
}
// NewFile constructs a file template
func NewFile(opts ...FilePropertyFunc) *File {
f := &File{}
for _, opt := range opts {
opt(f)
}
return f
}
// SetValidation stores ValidateOpts on the FEDWireMessage's validation rules
func (f *File) SetValidation(opts *ValidateOpts) {
if f == nil || opts == nil {
return
}
f.FEDWireMessage.ValidateOptions = opts
}
// GetValidation returns validation rules of FEDWireMessage
func (f *File) GetValidation() *ValidateOpts {
if f == nil || f.FEDWireMessage.ValidateOptions == nil {
return nil
}
return f.FEDWireMessage.ValidateOptions
}
// AddFEDWireMessage appends a FEDWireMessage to the File
func (f *File) AddFEDWireMessage(fwm FEDWireMessage) FEDWireMessage {
f.FEDWireMessage = fwm
return f.FEDWireMessage
}
// Create will tabulate and assemble an WIRE file into a valid state.
//
// Create implementations are free to modify computable fields in a file and should
// call the Validate() function at the end of their execution.
func (f *File) Create() error {
return nil
}
// Validate will never modify the file.
func (f *File) Validate() error {
if err := f.FEDWireMessage.verify(); err != nil {
return err
}
return nil
}
// FileFromJSON attempts to return a *File object assuming the input is valid JSON.
//
// Callers should always check for a nil-error before using the returned file.
//
// The File returned may not be valid and callers should confirm with Validate(). Invalid files may
// be rejected by other Financial Institutions or ACH tools.
func FileFromJSON(bs []byte) (*File, error) {
if len(bs) == 0 {
// return nil, errors.New("no JSON data provided")
return nil, nil
}
file := NewFile()
if err := json.NewDecoder(bytes.NewReader(bs)).Decode(file); err != nil {
return nil, fmt.Errorf("problem reading File: %v", err)
}
return file, nil
}
type FilePropertyFunc func(*File)
// OutgoingFile configures the FedWireMessage ValidationOpts for an outgoing file
func OutgoingFile() FilePropertyFunc {
return func(f *File) {
if f != nil {
if f.FEDWireMessage.ValidateOptions == nil {
f.FEDWireMessage.ValidateOptions = &ValidateOpts{}
}
f.FEDWireMessage.ValidateOptions.AllowMissingSenderSupplied = false
}
}
}
// IncomingFile configures the FedWireMessage ValidationOpts for an incoming file
func IncomingFile() FilePropertyFunc {
return func(f *File) {
if f != nil {
if f.FEDWireMessage.ValidateOptions == nil {
f.FEDWireMessage.ValidateOptions = &ValidateOpts{}
}
f.FEDWireMessage.ValidateOptions.AllowMissingSenderSupplied = true
}
}
}