-
Notifications
You must be signed in to change notification settings - Fork 0
/
object.delimited.go
184 lines (158 loc) · 4.01 KB
/
object.delimited.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
package tox
import (
"errors"
"fmt"
"strings"
"time"
)
type part struct {
name string
dataType string
timeFormat string
arraySize int
arrayObjKey string
}
func parsePart(partString string, idx int) (part, error) {
if len(partString) == 0 {
return part{name: fmt.Sprintf("ignored.field%d", idx), dataType: "string"}, nil
}
lp := strings.IndexByte(partString, '(')
rp := strings.IndexByte(partString, ')')
if lp == -1 {
return part{name: partString, dataType: "string"}, nil
}
if rp == -1 {
return part{name: partString[0:lp]}, errors.New("missing parentheses")
}
if rp < lp {
return part{name: partString[0:lp], dataType: "string"}, errors.New("bad format")
}
if rp-lp == 1 {
return part{name: partString[0:lp], dataType: "string"}, nil
}
argStr := partString[lp+1 : rp]
args := strings.Split(argStr, ",")
ret := part{name: partString[0:lp]}
if len(args) >= 1 {
switch strings.ToLower(args[0]) {
case "string", "int", "float", "bool", "timestamp", "array", "arrayobj":
ret.dataType = strings.ToLower(args[0])
default:
return ret, errors.New("invalid data type")
}
} else {
ret.dataType = "string"
}
if len(args) >= 2 {
switch ret.dataType {
case "timestamp":
ret.timeFormat = args[1]
case "array", "arrayobj":
ret.arraySize = ToInt(args[1])
}
}
if len(args) >= 3 {
switch ret.dataType {
case "arrayobj":
ret.arrayObjKey = ToString(args[2])
}
}
if ret.dataType == "arrayobj" && len(ret.arrayObjKey) == 0 {
return ret, errors.New("arrayobj requires 2 parameters")
}
return ret, nil
}
func NewObjectFromDelimitedString(source string, pattern string, delim string) (Object, error) {
patternParts := strings.Split(pattern, "|")
patternIdx := 0
data := Object{}
var arrayBase string
var arrayCount int
var arrayIndex int
var arrayPartIndex int
var arrayPartCount int
var arrayObjKey string
var arrayData Object
var arrayItems []Object
sourceParts := strings.Split(source, delim)
for _, sourcePart := range sourceParts {
var partData Object
var parsedPart part
patternPart := ""
if patternIdx < len(patternParts) {
patternPart = patternParts[patternIdx]
}
parsedPart, err := parsePart(patternPart, patternIdx)
if err != nil {
return nil, err
}
patternIdx++
if len(arrayBase) != 0 {
partData = arrayData
} else {
partData = data
}
if len(sourcePart) != 0 {
switch parsedPart.dataType {
case "string":
partData.Set(parsedPart.name, sourcePart)
case "int":
partData.Set(parsedPart.name, ToInt(sourcePart))
case "float":
partData.Set(parsedPart.name, ToFloat64(sourcePart))
case "bool":
partData.Set(parsedPart.name, ToBool(sourcePart))
case "timestamp":
tm, err := time.Parse(parsedPart.timeFormat, sourcePart)
if err != nil {
partData.Set(parsedPart.name, "err: "+err.Error())
} else {
partData.Set(parsedPart.name, tm.UTC().Format(time.RFC3339))
}
case "array", "arrayobj":
arrayBase = parsedPart.name
arrayCount = ToInt(sourcePart)
arrayIndex = 0
arrayPartCount = parsedPart.arraySize
arrayPartIndex = -1
arrayData = Object{}
if parsedPart.dataType == "arrayobj" {
arrayObjKey = parsedPart.arrayObjKey
} else {
arrayObjKey = ""
}
}
}
if len(arrayBase) != 0 {
arrayPartIndex++
if arrayPartIndex == arrayPartCount {
// done with array item
arrayItems = append(arrayItems, arrayData)
arrayData = Object{}
arrayPartIndex = 0
arrayIndex++
if arrayIndex == arrayCount {
// done with array
if len(arrayObjKey) != 0 {
subObj := Object{}
for _, item := range arrayItems {
key := item.GetString(arrayObjKey, "")
if len(key) == 0 {
return nil, errors.New("bad array: missing object key")
}
delete(item, arrayObjKey)
subObj.Set(key, item)
}
data.Set(arrayBase, subObj)
} else {
data.Set(arrayBase, arrayItems)
}
arrayBase = ""
} else {
patternIdx -= arrayPartCount
}
}
}
}
return data, nil
}