-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.go
More file actions
180 lines (170 loc) · 4.06 KB
/
functions.go
File metadata and controls
180 lines (170 loc) · 4.06 KB
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
package main
import (
"errors"
"fmt"
"strconv"
"strings"
)
var (
errCantFindMap = fmt.Errorf("invalid map address")
errInvalidIndex = fmt.Errorf("invalid index")
errNonMapInput = fmt.Errorf("invalid input (not map or string)")
)
type typeError string
func (e typeError) Error() string { return "wrong data type expected " + string(e) }
func BreakupArray(input string) []interface{} {
// handle a bug when no input
if input == "" {
return []interface{}{}
}
if strings.HasPrefix(input, "[") && strings.HasSuffix(input, "]") {
input = strings.TrimPrefix(input, "[")
input = strings.TrimSuffix(input, "]")
}
parts := strings.Split(input, "],[")
if len(parts) < 2 {
parts = strings.Split(input, "][")
}
if len(parts) < 2 {
parts = strings.Split(input, ",")
}
output := make([]interface{}, len(parts))
for i, _ := range parts {
parts[i] = strings.TrimSpace(parts[i])
if strings.HasPrefix(parts[i], "\"") && strings.HasSuffix(parts[i], "\"") {
output[i] = strings.Trim(parts[i], "\"")
} else {
number, err := strconv.Atoi(parts[i])
if err == nil {
output[i] = number
} else {
output[i] = parts[i]
}
}
}
return output
}
func GetInt(data interface{}, target []interface{}) (int, error) {
tempItem, err := GetItem(data, target)
if err != nil {
return 0, fmt.Errorf("bad item - %v", err)
}
switch item := tempItem.(type) {
case int:
return item, nil
case float64:
return int(item), nil
case string:
f, err := strconv.ParseFloat(item, 64)
if err != nil {
return 0, fmt.Errorf("got non-float item - %s", tempItem)
}
return int(f), nil
default:
return 0, fmt.Errorf("got unknown typed item - %s", tempItem)
}
}
func GetItem(data interface{}, target []interface{}) (interface{}, error) {
if targetInt, ok := target[0].(int); ok {
if dataSafe, ok := data.([]interface{}); !ok {
return nil, errors.New("got array address for non-array")
} else if targetInt < 0 || targetInt > len(dataSafe)-1 {
return nil, errors.New("non-existant array position")
} else {
if len(target) > 1 {
return GetItem(dataSafe[targetInt], target[1:])
} else {
return dataSafe[targetInt], nil
}
}
} else if targetString, ok := target[0].(string); ok {
if dataSafe, ok := data.(map[string]interface{}); !ok {
return nil, errors.New("got map address for non-map")
} else if value, ok := dataSafe[targetString]; !ok {
return nil, errors.New("non-existant map position")
} else {
if len(target) > 1 {
return GetItem(value, target[1:])
} else {
return value, nil
}
}
} else {
return nil, fmt.Errorf("bad address - %s", target)
}
}
func SetItem(data interface{}, t []interface{}, val interface{}) (err error) {
if len(t) < 1 {
return errNonMapInput
}
// t[1:] := t[1:]
switch d := data.(type) {
case map[string]interface{}:
tt, ok := t[0].(string)
if !ok {
return typeError("string")
}
if len(t[1:]) > 0 {
nextData, ok := d[tt]
if !ok {
return errCantFindMap
}
return SetItem(nextData, t[1:], val)
}
d[tt] = val
return
case []interface{}:
tt, ok := t[0].(int)
if !ok {
return typeError("int")
}
if tt < 0 || tt >= len(d) {
return errInvalidIndex
}
if len(t[1:]) > 0 {
return SetItem(d[tt], t[1:], val)
}
d[tt] = val
return
default:
return errNonMapInput
}
return
}
// func SetItem(data interface{}, t []interface{}, val interface{}) (err error) {
// if len(t) < 1 {
// return errNonMapInput
// }
// switch d := data.(type) {
// case map[string]interface{}:
// tt, ok := t[0].(string)
// if !ok {
// return typeError("string")
// }
// if len(t[1:]) > 0 {
// nextData, ok := d[tt]
// if !ok {
// return errCantFindMap
// }
// return SetItem(nextData, t[1:], val)
// }
// d[tt] = val
// return
// case []interface{}:
// tt, ok := t[0].(int)
// if !ok {
// return typeError("int")
// }
// if len(t[1:]) > 0 {
// if tt < 0 || tt >= len(d) {
// return errInvalidIndex
// }
// return SetItem(d[tt], t[:1], val)
// }
// d[tt] = val
// return
// default:
// return errNonMapInput
// }
// return
// }