-
Notifications
You must be signed in to change notification settings - Fork 0
/
struct.go
81 lines (76 loc) · 1.67 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
package neo4j
import (
"encoding/json"
"fmt"
)
func mapToMapParam(item map[string]interface{}, existingParams map[string]interface{}, prefix ...string) (result, params map[string]interface{}) {
var prf string
if prefix != nil && len(prefix) > 0 {
prf = prefix[0]
}
result = map[string]interface{}{}
params = map[string]interface{}{}
for k, v := range item {
k2 := prf + k
if existingParams != nil {
tempK := k2
ind := 1
for {
if _, keyExists := existingParams[tempK]; keyExists {
ind++
tempK = fmt.Sprintf("%s_%d", k2, ind)
} else {
break
}
}
k2 = tempK
}
if mapItem, ok := v.(map[string]interface{}); ok {
nestedResult, nestedParams := mapToMapParam(mapItem, params, prefix...)
for rk, rv := range nestedResult {
result[fmt.Sprintf("%s$%s", k, rk)] = rv
}
for pk, pv := range nestedParams {
params[pk] = pv
}
} else {
result[k] = "$" + k2
params[k2] = v
}
}
return
}
func structToMap(item interface{}) (result map[string]interface{}, err error) {
if item == nil {
return
}
bytes, err := json.Marshal(item)
if err != nil {
return
}
result = map[string]interface{}{}
err = json.Unmarshal(bytes, &result)
if err != nil {
return
}
return
}
func structToMapParam(item interface{}, prefix ...string) (result, params map[string]interface{}) {
params = map[string]interface{}{}
if item == nil {
return
}
result, err := structToMap(item)
if err != nil {
return
}
result, params = mapToMapParam(result, nil, prefix...)
return
}
func arrayToMap(keys []string, values []any) map[string]any {
var data = map[string]any{}
for i, key := range keys {
data[key] = values[i]
}
return data
}