-
Notifications
You must be signed in to change notification settings - Fork 39
/
amount.go
100 lines (88 loc) · 2.43 KB
/
amount.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
// 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 (
"encoding/json"
"strings"
"unicode/utf8"
)
// Amount (up to a penny less than $10 billion) {2000}
type Amount struct {
// tag
tag string
// Amount must be right justified with leading zeroes, an implied decimal point and
// no commas (e.g., $12,345.67 becomes 000001234567). Amount can be all zeroes for
// only SUBTYPE CODE 90 messages.
Amount string `json:"amount"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewAmount returns a new Amount
func NewAmount() *Amount {
a := &Amount{
tag: TagAmount,
}
return a
}
// Parse takes the input string and parses the Amount value
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (a *Amount) Parse(record string) error {
if utf8.RuneCountInString(record) != 18 {
return NewTagWrongLengthErr(18, len(record))
}
a.tag = record[:6]
a.Amount = a.parseStringField(record[6:18])
return nil
}
func (a *Amount) UnmarshalJSON(data []byte) error {
type Alias Amount
aux := struct {
*Alias
}{
(*Alias)(a),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
a.tag = TagAmount
return nil
}
// String returns a fixed-width Amount record
func (a *Amount) String() string {
var buf strings.Builder
buf.Grow(18)
buf.WriteString(a.tag)
buf.WriteString(a.AmountField())
return buf.String()
}
// Validate performs WIRE format rule checks on Amount and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (a *Amount) Validate() error {
if err := a.fieldInclusion(); err != nil {
return err
}
if a.tag != TagAmount {
return fieldError("tag", ErrValidTagForType, a.tag)
}
if err := a.isAmountImplied(a.Amount); err != nil {
return fieldError("Amount", err, a.Amount)
}
return nil
}
// fieldInclusion validate mandatory fields. If fields are
// invalid the WIRE will return an error.
func (a *Amount) fieldInclusion() error {
if a.Amount == "" {
return fieldError("Amount", ErrFieldRequired)
}
return nil
}
// AmountField gets a string of entry addenda batch count zero padded
func (a *Amount) AmountField() string {
return a.numericStringField(a.Amount, 12)
}