-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstruct.go
274 lines (246 loc) · 5.55 KB
/
struct.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
package gotemplate
import (
"encoding/json"
"fmt"
"reflect"
"strconv"
"strings"
"github.com/shoobyban/mxj"
"github.com/spf13/cast"
)
func jsonEncode(v interface{}) (string, error) {
b, err := json.Marshal(v)
return string(b), err
}
func xmlEncode(v interface{}, args ...string) (string, error) {
if str, ok := v.(string); ok {
return "<![CDATA[" + str + "]]>", nil
}
if fl, ok := v.(float64); ok {
return strconv.FormatFloat(fl, 'f', 4, 64), nil
}
if fl, ok := v.(int64); ok {
return strconv.FormatInt(fl, 64), nil
}
if fl, ok := v.(int); ok {
return strconv.Itoa(fl), nil
}
if arr, ok := v.([]interface{}); ok {
itemtag := "item"
roottag := "items"
if len(args) > 0 {
itemtag = args[0]
}
if len(args) > 1 {
roottag = args[1]
}
mv := mxj.Map(map[string]interface{}{itemtag: arr})
mxj.XMLEscapeChars(true)
b, err := mv.XmlIndent("", " ", roottag)
return string(b), err
}
mv := mxj.Map(v.(map[string]interface{}))
mxj.XMLEscapeChars(true)
b, err := mv.Xml()
return string(b), err
}
func xmlArray(v interface{}, roottag, itemtag string) (string, error) {
mv := mxj.Map(map[string]interface{}{itemtag: v.([]interface{})})
mxj.XMLEscapeChars(true)
b, err := mv.XmlIndent("", " ", roottag)
return "<?xml version=\"1.0\"?>\n" + string(b), err
}
func decode(s, format string) (interface{}, error) {
p := NewParser()
res, err := p.ParseStruct(strings.NewReader(s), format)
if err != nil {
return nil, fmt.Errorf("Unable to parse json '%s': %v", s, err)
}
return res, nil
}
func jsonDecode(s string) (interface{}, error) {
return decode(s, "json")
}
func xmlDecode(s string) (interface{}, error) {
return decode(s, "xml")
}
// jsonEscape escapes a variable (mostly string) for using inside a JSON as string
func jsonEscape(i interface{}) string {
b, err := json.Marshal(i)
if err != nil {
panic(err)
}
s := string(b)
return s[1 : len(s)-1]
}
func asJSON(s interface{}) string {
jsonBytes, err := json.MarshalIndent(s, "", " ")
if err != nil {
panic(err)
}
return string(jsonBytes)
}
func pathValue(keys []string, s interface{}, f string) (v interface{}) {
var key string
var nextkeys []string
if len(keys) == 0 {
if f == "" {
return s
}
key = ""
nextkeys = keys
} else {
key = keys[0]
nextkeys = keys[1:]
}
filter := ""
var (
i int64
ok bool
)
var err error
if key != "" && key[:1] == "[" && key[len(key)-1:] == "]" {
key, filter = "", key[1:len(key)-1]
}
switch s.(type) {
case map[string]interface{}:
if key == "" {
m := map[string]interface{}{}
found := true
if f != "" {
found = false
fparts := strings.Split(f, "=")
for k, item := range s.(map[string]interface{}) {
if k == fparts[0] && item == fparts[1] {
found = true
}
}
}
if found {
for k, item := range s.(map[string]interface{}) {
m[k] = pathValue(nextkeys, item, filter)
}
}
if len(m) > 0 {
v = m
}
} else if v, ok = s.(map[string]interface{})[key]; !ok {
err = fmt.Errorf("Key not present. [Key:%s]", key)
}
case []interface{}:
array := s.([]interface{})
a := []interface{}{}
if f != "" {
return a
}
if key == "" {
for _, item := range array {
pv := pathValue(nextkeys, item, filter)
if pv != nil {
a = append(a, pv)
}
}
if len(a) == 1 {
v = a[0]
} else if len(a) > 0 {
v = a
}
} else if i, err = strconv.ParseInt(key, 10, 64); err == nil {
if int(i) < len(array) {
v = array[i]
} else {
err = fmt.Errorf("Index out of bounds. [Index:%d] [Array:%v]", i, array)
}
}
}
return pathValue(nextkeys, v, "")
}
func explode(s, sep string) []string {
return strings.Split(s, sep)
}
func inArray(needle interface{}, haystack interface{}) bool {
switch reflect.TypeOf(haystack).Kind() {
case reflect.Slice:
s := reflect.ValueOf(haystack)
for i := 0; i < s.Len(); i++ {
if reflect.DeepEqual(needle, s.Index(i).Interface()) == true {
return true
}
}
}
return false
}
func isSet(a interface{}, key interface{}) bool {
av := reflect.ValueOf(a)
kv := reflect.ValueOf(key)
switch av.Kind() {
case reflect.Array, reflect.Chan, reflect.Slice:
k, err := cast.ToIntE(key)
if err != nil {
return false
}
if av.Len() > k {
return true
}
case reflect.Map:
if kv.Type() == av.Type().Key() {
return av.MapIndex(kv).IsValid()
}
default:
return false
}
return false
}
func mapto(item, mapvals, separators string) string {
maps := strings.Split(mapvals, separators[:1])
mapping := map[string]string{}
for _, v := range maps {
vv := strings.Split(v, separators[1:])
if len(vv) < 2 {
return ""
}
mapping[vv[0]] = vv[1]
}
if ret, ok := mapping[item]; ok {
return ret
}
if ret, ok := mapping["*"]; ok {
return ret
}
return item
}
func createMap() map[string]interface{} {
return map[string]interface{}{}
}
func setItem(m map[string]interface{}, a string, b interface{}) map[string]interface{} {
m[a] = b
return m
}
func mkSlice(args ...interface{}) []interface{} {
return args
}
func decimalFmt(format string, number interface{}) string {
num := emptyStr(number)
f, _ := strconv.ParseFloat(num, 64)
i := strings.Split(format, ",")
s := fmt.Sprintf(fmt.Sprintf("%%%s.%sf", i[0], i[1]), f)
if i[1] != "0" {
s = strings.TrimRight(s, "0")
if s[len(s)-1:] == "." {
s += "0"
}
}
return s
}
func last(x int, a interface{}) bool {
return x == reflect.ValueOf(a).Len()-1
}
func filterPath(s interface{}, p string) interface{} {
return pathValue(strings.Split(p, "."), s, "")
}
func toAbs(float float64) float64 {
if float < 0 {
float = float * -1
}
return float
}