-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathffptag.go
179 lines (153 loc) · 5 KB
/
ffptag.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
package flatfile
import (
"strconv"
"strings"
"github.com/pkg/errors"
)
type flatfileTag struct {
col int
length int
occurs int
override string
condCol int
condLen int
condVal string
condChk bool
}
var parseFuncMap = map[string]func(string, *flatfileTag) error{
"col": parseColumnOption,
"column": parseColumnOption,
"len": parseLengthOption,
"length": parseLengthOption,
"occ": parseOccursOption,
"occurs": parseOccursOption,
"ovr": parseOverrideOption,
"override": parseOverrideOption,
"cond": parseConditionOption,
"condition": parseConditionOption,
}
//condition=1-10-TENLETTERS
//validOptions is a list of the keys loaded from parseFuncMap. This is used purely to display options to user
var validOptions []string
func init() {
for key := range parseFuncMap {
validOptions = append(validOptions, key)
}
}
//parseFlatfileTag parses an ffp struct tag on a field
//Tags are expected to be in the form:
// col,len,occurs
// where col is an int > 0
// len is an int
func parseFlatfileTag(fieldTag string, ffpTag *flatfileTag) error {
var err error
//split tag by comma to get column and length data
params := strings.Split(fieldTag, ",")
//column and length parameters must be provided
if len(params) < 2 {
return errors.Errorf("flatfile.parseFlatfileTag: Not enough ffp tag params provided.\nColumn and length parameters must be provided.\nMust be in form `flatfile:\"col,len\"`")
}
for idx, param := range params {
//check whether or not tag is using named options
if strings.Contains(param, "=") {
options := strings.Split(param, "=")
if len(options) != 2 {
return errors.Errorf("flatfile.parseFlatfileTag: Invalid formatting of named option '%v'\nNamed options should be in the form option=value\nValid options:%v", options, validOptions)
}
if funcVal, exists := parseFuncMap[options[0]]; exists {
err = funcVal(options[1], ffpTag)
} else {
return errors.Errorf("flatfile.parseFlatfileTag: Invalid tag parameter %s\nValid options: %v", options[0], validOptions)
}
} else {
//assume user is using positional options
switch idx {
case 0:
err = parseColumnOption(param, ffpTag)
case 1:
err = parseLengthOption(param, ffpTag)
case 2:
err = parseOccursOption(param, ffpTag)
case 3:
err = parseOverrideOption(param, ffpTag)
case 4:
err = parseConditionOption(param, ffpTag)
}
}
if err != nil {
return errors.Wrapf(err, "flatfile.parseFlatfileTag: Error parsing tag option %s", param)
}
}
if ffpTag.length == 0 || ffpTag.col == 0 {
return errors.New("flatfile.parseFlatfileTag: Column or length option not provided")
}
return nil
}
func parseColumnOption(param string, ffpTag *flatfileTag) error {
col, colerr := strconv.Atoi(param)
if colerr != nil {
return errors.Wrapf(colerr, "flatfile.parseColumnOption: Error parsing tag column parameter %s", param)
}
if col < 1 {
return errors.Errorf("flatfile.parseColumnOption: Out of range error. Column parameter cannot be less than 1. Please note column is 1-indexed not zero")
}
ffpTag.col = col
return nil
}
func parseLengthOption(param string, ffpTag *flatfileTag) error {
length, lenerr := strconv.Atoi(param)
if lenerr != nil {
return errors.Wrapf(lenerr, "flatfile.parseLengthOption: Error parsing tag length parameter %s", param)
}
if length < 1 {
return errors.Errorf("flatfile.parseLengthOption: Out of range error. Length parameter cannot be less than 1")
}
ffpTag.length = length
return nil
}
func parseOccursOption(param string, ffpTag *flatfileTag) error {
occurs, occerr := strconv.Atoi(param)
if occerr != nil {
return errors.Wrapf(occerr, "flatfile.parseOccursOption: Error parsing tag occurs parameter %s", param)
}
if occurs < 2 {
return errors.Errorf("flatfile.parseOccursOption: Out of range error. Occurs parameter cannot be less than 2")
}
ffpTag.occurs = occurs
return nil
}
func parseOverrideOption(param string, ffpTag *flatfileTag) error {
if isValidOverride(param) {
ffpTag.override = param
return nil
}
return errors.Errorf("flatfile.parseOverrideOption: Invalid override %s", param)
}
func isValidOverride(override string) bool {
switch override {
case "byte",
"rune":
return true
}
return false
}
func parseConditionOption(param string, ffpTag *flatfileTag) error {
condParams := strings.Split(param, "-")
if len(condParams) != 3 {
return errors.Errorf("flatfile.parseConditionOption: Expected 3 tag condition parameters but got %d options: %s", len(condParams), param)
}
condCol, colerr := strconv.Atoi(condParams[0])
if colerr != nil {
return errors.Wrapf(colerr, "flatfile.parseConditionOption: Error parsing tag condition col parameter %s", param)
}
condLen, lenerr := strconv.Atoi(condParams[1])
if lenerr != nil {
return errors.Wrapf(lenerr, "flatfile.parseConditionOption: Error parsing tag condition len parameter %s", param)
}
condVal := condParams[2]
ffpTag.condCol = condCol
ffpTag.condLen = condLen
ffpTag.condVal = condVal
ffpTag.condChk = true
return nil
}