-
Notifications
You must be signed in to change notification settings - Fork 34
/
helper.go
120 lines (107 loc) · 2.37 KB
/
helper.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
package simdb
import (
"os"
"strings"
"fmt"
"strconv"
"errors"
)
func createDirIfNotExist(dir string) error {
if _, err := os.Stat(dir); err == nil {
return nil
}
err := os.Mkdir(dir, 0755)
return err
}
func mergeToExisting(array []interface{}, entity interface{}) ([]interface{}, error) {
array = append(array, entity)
return array, nil
}
// getNestedValue fetch nested value from node
func getNestedValue(input interface{}, node string) (interface{}, error) {
pp := strings.Split(node, ".")
for _, n := range pp {
if isIndex(n) {
// find slice/array
if arr, ok := input.([]interface{}); ok {
indx, err := getIndex(n)
if err != nil {
return input, err
}
arrLen := len(arr)
if arrLen == 0 ||
indx > arrLen-1 {
return empty, errors.New("empty array")
}
input = arr[indx]
}
} else {
// find in map
validNode := false
if mp, ok := input.(map[string]interface{}); ok {
input, ok = mp[n]
validNode = ok
}
// find in group data
if mp, ok := input.(map[string][]interface{}); ok {
input, ok = mp[n]
validNode = ok
}
if !validNode {
return empty, fmt.Errorf("invalid node name %s", n)
}
}
}
return input, nil
}
func getIndex(in string) (int, error) {
if !isIndex(in) {
return -1, fmt.Errorf("invalid index")
}
is := strings.TrimLeft(in, "[")
is = strings.TrimRight(is, "]")
oint, err := strconv.Atoi(is)
if err != nil {
return -1, err
}
return oint, nil
}
func isIndex(in string) bool {
return strings.HasPrefix(in, "[") && strings.HasSuffix(in, "]")
}
// toFloat64 converts interface{} value to float64 if value is numeric else return false
func toFloat64(v interface{}) (float64, bool) {
var f float64
flag := true
// as Go convert the json Numeric value to float64
switch u := v.(type) {
case int:
f = float64(u)
case int8:
f = float64(u)
case int16:
f = float64(u)
case int32:
f = float64(u)
case int64:
f = float64(u)
case float32:
f = float64(u)
case float64:
f = u
default:
flag = false
}
return f, flag
}
// length return length of strings/array/map
func length(v interface{}) (int, error) {
if val, ok := v.(string); ok {
return len(val), nil
} else if val, ok := v.([]interface{}); ok {
return len(val), nil
} else if val, ok := v.(map[string]interface{}); ok {
return len(val), nil
}
return -1, errors.New("invalid type for length")
}