-
Notifications
You must be signed in to change notification settings - Fork 40
/
converters.go
204 lines (165 loc) · 4.11 KB
/
converters.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
// 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 (
"fmt"
"strconv"
"strings"
)
const (
Delimiter = "*"
)
// converters handles golang to WIRE type Converters
type converters struct{}
func (c *converters) parseNumField(r string) (s int) {
s, _ = strconv.Atoi(strings.TrimSpace(r))
return s
}
func (c *converters) parseStringField(r string) (s string) {
s = strings.TrimSpace(r)
return s
}
func (c *converters) parseAlphaField(r string, max uint) string {
ln := uint(len(r))
if ln > max {
return r[ln-max:]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
r += strings.Repeat(" ", int(rem)) //nolint:gosec
}
return r
}
// numericStringField right-justified zero filled
func (c *converters) numericStringField(s string, max uint) string {
ln := uint(len(s))
if ln > max {
return s[ln-max:]
}
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s = strings.Repeat("0", int(rem)) + s //nolint:gosec
}
return s
}
// alphaField returns the input formatted as a fixed-width alphanumeric string.
// If the length of s exceeds max, s will be truncated to max.
func (c *converters) alphaField(s string, max uint) string {
return c.formatAlphaField(s, max, FormatOptions{})
}
// formatAlphaField returns the input formatted according to the FormatOptions.
// If the length of s exceeds max, s will be truncated to max. If options.VariableLengthFields
// is set, any trailing whitespace is stripped
func (c *converters) formatAlphaField(s string, max uint, options FormatOptions) string {
ln := uint(len(s))
if ln > max {
return s[:max]
}
if !options.VariableLengthFields {
rem := max - ln
if !validSizeUint(rem) {
return ""
} else {
s += strings.Repeat(" ", int(rem)) //nolint:gosec
}
}
return s
}
// parseFixedStringField will use to parse for mandatory fixed length elements
// - Has fixed length
// - Not permitted Delimiter
func (c *converters) parseFixedStringField(r string, maxLen int) (got string, size int, err error) {
max := func(x, y int) int {
if x > y {
return x
}
return y
}
// Omit field?
if len(r) == 0 {
return
}
endIndex := -1
delimiterIndex := -1
if index := strings.Index(r, Delimiter); index > -1 {
delimiterIndex = index
}
if index := strings.Index(r, "{"); index > -1 {
endIndex = index
}
size = max(endIndex, delimiterIndex)
if size == -1 {
size = len(r)
}
if size > maxLen {
size = maxLen
} else if size < maxLen {
size = 0
err = ErrValidLength
return
}
got = strings.TrimSpace(r[:size])
return
}
// parseVariableStringField will use to parse for mandatory variable length/optional fixed length/optional variable length elements
// - Has variable length
// - Always required delimiter
func (c *converters) parseVariableStringField(r string, maxLen int) (got string, size int, err error) {
// Omit field?
if len(r) == 0 {
return
}
if index := strings.Index(r, Delimiter); index > -1 {
size = index
} else {
size = 0
err = ErrRequireDelimiter
return
}
if got = strings.TrimSpace(r[:size]); got == Delimiter {
got = ""
}
if len(got) > maxLen {
got = got[:maxLen]
}
// skip delimiter
size++
return
}
// get first option from options
func (c *converters) parseFirstOption(options []bool) bool {
firstOption := false
if len(options) > 0 {
firstOption = options[0]
}
return firstOption
}
// strip delimiters
func (c *converters) stripDelimiters(data string) string {
index := len(data)
for i := len(data) - 1; i > 5; i-- {
inspectLetter1 := string(data[i])
inspectLetter2 := string(data[i-1])
if inspectLetter1 != Delimiter || inspectLetter2 != Delimiter || i == 6 {
index = i + 1
break
}
}
return data[:index]
}
// verify input data with read length
func (c *converters) verifyDataWithReadLength(data string, expected int) error {
n := len(data) // utf8.RuneCountInString(data)
if n == expected {
return nil
}
if n > expected && data[expected:] == Delimiter {
return nil
}
return fmt.Errorf("found data of %d length but expected %d", n, expected)
}