-
Notifications
You must be signed in to change notification settings - Fork 4
/
get.go
203 lines (176 loc) · 4.09 KB
/
get.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
package main
import (
"fmt"
"strconv"
"strings"
"github.com/Jeffail/gabs/v2"
"github.com/urfave/cli/v2"
)
var cmdGet cli.Command
var cmdContains cli.Command
type getOptions struct {
json *gabs.Container
path string
delimiter string
}
func init() {
cmdGet = cli.Command{
Name: "get",
Usage: "extract an path from a json file",
Action: actionGet,
Flags: []cli.Flag{
&flagFile,
&flagPath,
&flagDelimiter,
&flagPretty,
},
}
}
func actionGet(c *cli.Context) error {
j, err := readInput(c.String("file"))
if err != nil {
return err
}
options := getOptions{
json: j,
path: c.String("path"),
delimiter: getDelimiter(c.String("delimiter")),
}
j, err = get(options)
if err != nil {
return err
}
switch j.Data().(type) {
case string:
fmt.Printf("%s", j.Data())
default:
if pretty {
fmt.Println(j.StringIndent("", " "))
} else {
fmt.Println(j.String())
}
}
return nil
}
// get retrieves a path from a JSON structure.
// The path is specified in dotted notation:
// {"foo":{"bar":{"baz":"xyz"}}} = foo.bar.baz
// {"foo":{"bar":["a","b","c"]}} = foo.bar.2
// {"foo":{"bar":{"baz":"xyz"}}} = foo.bar.baz=xyz
// {"foo":{"bar":{"baz":"xyz"}}} = foo.*
// {"foo":{"bar":[{"a":"b"},{"c":"d"}]}} = foo.bar.*.a
func get(options getOptions) (*gabs.Container, error) {
var err error
var value string
j := options.json
pathPieces := strings.Split(options.path, options.delimiter)
for i := 0; i < len(pathPieces); i++ {
p := pathPieces[i]
// Check if a value was specified
kv := strings.Split(p, "=")
if len(kv) > 1 {
p = kv[0]
if len(kv) > 2 {
value = strings.Join(kv[1:], "=")
} else {
value = kv[1]
}
}
debug.Printf("Path piece: %+v", p)
debug.Printf("Path value: %+v", value)
if _, ok := j.Data().([]interface{}); ok {
debug.Printf("%+v is an array", j)
if p == "*" {
debug.Printf("glob used")
children := j.Children()
for _, c := range children {
debug.Printf("Child: %+v", c)
newPath := strings.Join(pathPieces[i+1:], ".")
debug.Printf("New path: %+v", newPath)
newOptions := getOptions{
json: c,
path: newPath,
delimiter: options.delimiter,
}
if j, err := get(newOptions); err != nil {
continue
} else {
return j, nil
}
}
} else {
j, err = checkArray(j, p)
if err != nil {
return nil, err
}
}
} else {
j = j.Path(p)
}
// if a value was given, see if the returned value matches
// if the returned value is an array, check and see if the value exists in the array
if value != "" {
j, err = compareValues(j, value)
if err != nil {
return j, err
}
}
}
if j.Data() == nil {
return nil, fmt.Errorf("No match found.")
}
return j, nil
}
// if the path piece is a number:
// check and see if it can be used as an array index
// if the current path is not an array, use it as a key
func checkArray(j *gabs.Container, pathPiece string) (*gabs.Container, error) {
i, err := strconv.Atoi(pathPiece)
if err != nil {
return nil, fmt.Errorf("Non-numerical index.")
}
if i < 0 {
return nil, fmt.Errorf("Array index out of bounds.")
}
if i > len(j.Data().([]interface{})) {
return nil, fmt.Errorf("Array index out of bounds.")
}
return j.Index(i), nil
}
func compareValues(j *gabs.Container, value string) (*gabs.Container, error) {
debug.Printf("[compareValues] j = %+v, v = %+v\n", j, value)
var err error
switch j.Data().(type) {
case []interface{}:
if value == "[]" {
return j, nil
}
array := j.Data().([]interface{})
for i, _ := range array {
_, err = compareValues(j.Index(i), value)
if err == nil {
return j.Index(i), nil
}
}
case map[string]interface{}:
if value == "{}" {
return j, nil
}
case string:
if value == j.Data().(string) {
return j, nil
}
default:
if valueFloat64, err := strconv.ParseFloat(value, 64); err == nil {
if jFloat64, ok := j.Data().(float64); ok {
if valueFloat64 == jFloat64 {
return j, nil
}
}
}
if value == j.String() {
return j, nil
}
}
return nil, fmt.Errorf("No match found.")
}