Debian Codegraph @

helper.go

raw symbols ◀ ▶ SCIP
1 Debian patch touch this file
1 package gojsonq
2
3 import (
4 "errors"
5 "fmt"
6 "reflect"
7 "sort"
8 "strconv"
9 "strings"
10 )
11
12 func abs(i int) int {
13 if i < 0 {
14 i = -1 * i
15 }
16 return i
17 }
18
19 func isIndex(in string) bool {
20 return strings.HasPrefix(in, "[") && strings.HasSuffix(in, "]")
21 }
22
23 func getIndex(in string) (int, error) {
24 if !isIndex(in) {
25 return -1, fmt.Errorf("invalid index")
26 }
27 is := strings.TrimLeft(in, "[")
28 is = strings.TrimRight(is, "]")
29 oint, err := strconv.Atoi(is)
30 if err != nil {
31 return -1, err
32 }
33 return oint, nil
34 }
35
36 func toString(v interface{}) string {
37 return fmt.Sprintf("%v", v)
38 }
39
40 // toFloat64 converts interface{} value to float64 if value is numeric else return false
41 func toFloat64(v interface{}) (float64, bool) {
42 var f float64
43 flag := true
44 // as Go convert the json Numeric value to float64
45 switch u := v.(type) {
46 case int:
47 f = float64(u)
48 case int8:
49 f = float64(u)
50 case int16:
51 f = float64(u)
52 case int32:
53 f = float64(u)
54 case int64:
55 f = float64(u)
56 case float32:
57 f = float64(u)
58 case float64:
59 f = u
60 default:
61 flag = false
62 }
63 return f, flag
64 }
65
66 // sortList sorts a list of interfaces
67 func sortList(list []interface{}, asc bool) []interface{} {
68 var ss []string
69 var ff []float64
70 var result []interface{}
71 for _, v := range list {
72 // sort elements for string
73 if sv, ok := v.(string); ok {
74 ss = append(ss, sv)
75 }
76 // sort elements for float64
77 if fv, ok := v.(float64); ok {
78 ff = append(ff, fv)
79 }
80 }
81
82 if len(ss) > 0 {
83 if asc {
84 sort.Strings(ss)
85 } else {
86 sort.Sort(sort.Reverse(sort.StringSlice(ss)))
87 }
88 for _, v := range ss {
89 result = append(result, v)
90 }
91 }
92 if len(ff) > 0 {
93 if asc {
94 sort.Float64s(ff)
95 } else {
96 sort.Sort(sort.Reverse(sort.Float64Slice(ff)))
97 }
98 for _, v := range ff {
99 result = append(result, v)
100 }
101 }
102 return result
103 }
104
105 type sortMap struct {
106 data interface{}
107 key string
108 desc bool
109 separator string
110 errs []error
111 }
112
113 // Sort sorts the slice of maps
114 func (s *sortMap) Sort(data interface{}) {
115 s.data = data
•116 sort.Stable(s)
117 }
118
119 // Len satisfies the sort.Interface
120 func (s *sortMap) Len() int {
121 return reflect.ValueOf(s.data).Len()
122 }
123
124 // Swap satisfies the sort.Interface
125 func (s *sortMap) Swap(i, j int) {
126 if i > j {
127 i, j = j, i
128 }
129 list := reflect.ValueOf(s.data)
130 tmp := list.Index(i).Interface()
131 list.Index(i).Set(list.Index(j))
132 list.Index(j).Set(reflect.ValueOf(tmp))
133 }
134
135 // TODO: need improvement
136 // Less satisfies the sort.Interface
137 // This will work for string/float64 only
138 func (s *sortMap) Less(i, j int) (res bool) {
139 list := reflect.ValueOf(s.data)
140 x := list.Index(i).Interface()
141 y := list.Index(j).Interface()
142
143 // compare nested values
144 if strings.Contains(s.key, s.separator) {
145 xv, errX := getNestedValue(x, s.key, s.separator)
146 if errX != nil {
147 s.errs = append(s.errs, errX)
148 }
149 yv, errY := getNestedValue(y, s.key, s.separator)
150 if errY != nil {
151 s.errs = append(s.errs, errY)
152 }
153 res = s.compare(xv, yv)
154 }
155
156 xv, okX := x.(map[string]interface{})
157 if !okX {
158 return
159 }
160 yv := y.(map[string]interface{})
161 if mvx, ok := xv[s.key]; ok {
162 mvy := yv[s.key]
163 res = s.compare(mvx, mvy)
164 }
165
166 return
167 }
168
169 // compare compare two values
170 func (s *sortMap) compare(x, y interface{}) (res bool) {
171 if mfv, ok := x.(float64); ok {
172 if mvy, oky := y.(float64); oky {
173 if s.desc {
174 return mfv > mvy
175 }
176 res = mfv < mvy
177 }
178 }
179
180 if mfv, ok := x.(string); ok {
181 if mvy, oky := y.(string); oky {
182 if s.desc {
183 return mfv > mvy
184 }
185 res = mfv < mvy
186 }
187 }
188
189 return
190 }
191
192 // getNestedValue fetch nested value from node
193 func getNestedValue(input interface{}, node, separator string) (interface{}, error) {
194 pp := strings.Split(node, separator)
195 for _, n := range pp {
196 if isIndex(n) {
197 // find slice/array
198 if arr, ok := input.([]interface{}); ok {
199 indx, err := getIndex(n)
200 if err != nil {
201 return input, err
202 }
203 arrLen := len(arr)
204 if arrLen == 0 ||
205 indx > arrLen-1 {
206 return empty, errors.New("empty array")
207 }
208 input = arr[indx]
209 }
210 } else {
211 // find in map
212 validNode := false
213 if mp, ok := input.(map[string]interface{}); ok {
214 input, ok = mp[n]
215 validNode = ok
216 }
217
218 // find in group data
219 if mp, ok := input.(map[string][]interface{}); ok {
220 input, ok = mp[n]
221 validNode = ok
222 }
223
224 if !validNode {
225 return empty, fmt.Errorf("invalid node name %s", n)
226 }
227 }
228 }
229
230 return input, nil
231 }
232
233 // makeAlias provide syntactic suger. when provide Property name as "user.name as userName"
234 // it return userName as output and pure node name like: "user.name".
235 // If "user.name" does not use "as" clause then it'll return "user.name", "user.name"
236 func makeAlias(in, separator string) (string, string) {
237 const alias = " as "
238 in = strings.Replace(in, " As ", alias, -1)
239 in = strings.Replace(in, " AS ", alias, -1)
240
241 if strings.Contains(in, alias) {
242 ss := strings.Split(in, alias)
243 return strings.TrimSpace(ss[0]), strings.TrimSpace(ss[1])
244 }
245
246 if strings.Contains(in, separator) {
247 ss := strings.Split(in, separator)
248 return in, ss[len(ss)-1]
249 }
250
251 return in, in
252 }
253
254 // length return length of strings/array/map
255 func length(v interface{}) (int, error) {
256 switch val := v.(type) {
257 case string:
258 return len(val), nil
259 case []interface{}:
260 return len(val), nil
261 case map[string]interface{}:
262 return len(val), nil
263 default:
264 return -1, errors.New("invalid type for length")
265 }
266 }