-
Notifications
You must be signed in to change notification settings - Fork 39
/
exchangeRate.go
117 lines (99 loc) · 3.13 KB
/
exchangeRate.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
// 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"
)
// ExchangeRate is the ExchangeRate of the wire
type ExchangeRate struct {
// tag
tag string
// ExchangeRate is the exchange rate
// Must contain at least one numeric character and only one decimal comma marker (e.g., an exchange rate of 1.2345 should be entered as 1,2345).
ExchangeRate string `json:"exchangeRate,omitempty"`
// validator is composed for data validation
validator
// converters is composed for WIRE to GoLang Converters
converters
}
// NewExchangeRate returns a new ExchangeRate
func NewExchangeRate() *ExchangeRate {
eRate := &ExchangeRate{
tag: TagExchangeRate,
}
return eRate
}
// Parse takes the input string and parses the ExchangeRate values
//
// Parse provides no guarantee about all fields being filled in. Callers should make a Validate() call to confirm
// successful parsing and data validity.
func (eRate *ExchangeRate) Parse(record string) error {
if utf8.RuneCountInString(record) < 6 {
return NewTagMinLengthErr(6, len(record))
}
eRate.tag = record[:6]
length := 6
value, read, err := eRate.parseVariableStringField(record[length:], 12)
if err != nil {
return fieldError("ExchangeRate", err)
}
eRate.ExchangeRate = value
length += read
if err := eRate.verifyDataWithReadLength(record, length); err != nil {
return NewTagMaxLengthErr(err)
}
return nil
}
func (eRate *ExchangeRate) UnmarshalJSON(data []byte) error {
type Alias ExchangeRate
aux := struct {
*Alias
}{
(*Alias)(eRate),
}
if err := json.Unmarshal(data, &aux); err != nil {
return err
}
eRate.tag = TagExchangeRate
return nil
}
// String returns a fixed-width ExchangeRate record
func (eRate *ExchangeRate) String() string {
return eRate.Format(FormatOptions{
VariableLengthFields: false,
})
}
// Format returns a ExchangeRate record formatted according to the FormatOptions
func (eRate *ExchangeRate) Format(options FormatOptions) string {
var buf strings.Builder
buf.Grow(18)
buf.WriteString(eRate.tag)
buf.WriteString(eRate.FormatExchangeRate(options) + Delimiter)
if options.VariableLengthFields {
return eRate.stripDelimiters(buf.String())
} else {
return buf.String()
}
}
// Validate performs WIRE format rule checks on ExchangeRate and returns an error if not Validated
// The first error encountered is returned and stops that parsing.
func (eRate *ExchangeRate) Validate() error {
if eRate.tag != TagExchangeRate {
return fieldError("tag", ErrValidTagForType, eRate.tag)
}
if err := eRate.isAmount(eRate.ExchangeRate); err != nil {
return fieldError("ExchangeRate", err, eRate.ExchangeRate)
}
return nil
}
// ExchangeRateField gets a string of the ExchangeRate field
func (eRate *ExchangeRate) ExchangeRateField() string {
return eRate.alphaField(eRate.ExchangeRate, 12)
}
// FormatExchangeRate returns ExchangeRate formatted according to the FormatOptions
func (eRate *ExchangeRate) FormatExchangeRate(options FormatOptions) string {
return eRate.formatAlphaField(eRate.ExchangeRate, 12, options)
}