-
Notifications
You must be signed in to change notification settings - Fork 1
/
selector.go
44 lines (37 loc) · 848 Bytes
/
selector.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
package jqingo
import (
"encoding/json"
"fmt"
"strings"
)
func getSelectedJSON(selector, input string) (selectedJSON string, err error) {
var f interface{}
splitSelctor := strings.Split(selector, ".")
if err = json.Unmarshal([]byte(input), &f); err != nil {
return
}
for i, s := range splitSelctor {
if i != 0 {
fmt.Printf("Selector: %s", s)
f = applySelector(f, s)
}
}
selectedJSON = interface2String(f)
return
}
func applySelector(input interface{}, selector string) (output interface{}) {
// if selector is blank string it means '.' is the selector
if selector != "" {
fmt.Printf("apply %s", selector)
m := input.(map[string]interface{})
output = m[selector]
return
}
output = input
return
}
func interface2String(f interface{}) (output string) {
o, _ := json.Marshal(f)
output = string(o)
return
}