forked from klyr/go-jq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjq.go
209 lines (181 loc) · 4.42 KB
/
jq.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
204
205
206
207
208
209
package jq
// #cgo LDFLAGS: -ljq
// #include <jq.h>
// #include <jv.h>
import "C"
import (
"errors"
"fmt"
"reflect"
)
type JQ struct {
program string
state *C.jq_state
lastValue C.jv
prevRefCnt int
}
func NewJQ(program string) (*JQ, error) {
state := C.jq_init()
jq := &JQ{program, state, C.jv_invalid(), 0}
if err := jq.compile(program); err != nil {
jq.Close()
return nil, err
}
return jq, nil
}
func (jq *JQ) Handle(value interface{}) {
jq.start(goToJv(value))
}
func (jq *JQ) HandleJson(text string) error {
jv, err := parseJson(text)
if err == nil {
jq.start(jv)
return nil
} else {
return err
}
}
func (jq *JQ) Next() bool {
// jq.ValueJson() is self execute internally (jv_dump_term in the jv_dump_string)
if jq.prevRefCnt == jq.jv_get_refcnt() {
freeJv(jq.lastValue)
}
jq.lastValue = jq.next()
jq.prevRefCnt = jq.jv_get_refcnt()
return isValid(jq.lastValue)
}
func (jq *JQ) Value() interface{} {
return jvToGo(jq.lastValue)
}
func (jq *JQ) ValueJson() string {
return dumpJson(jq.lastValue)
}
func (jq *JQ) Close() {
jq.teardown()
freeJv(jq.lastValue)
}
// JQ APIs
func (jq *JQ) compile(program string) error {
if rc := C.jq_compile(jq.state, C.CString(program)); rc == 0 {
return errors.New("Unable to compile jq filter")
} else {
return nil
}
}
func (jq *JQ) start(jv C.jv) {
C.jq_start(jq.state, jv, 0)
}
func (jq *JQ) next() C.jv {
return C.jq_next(jq.state)
}
func (jq *JQ) teardown() {
C.jq_teardown(&jq.state)
}
func (jq *JQ) jv_get_refcnt() int {
return (int)(C.jv_get_refcnt(jq.lastValue))
}
// JSON values
func parseJson(value string) (C.jv, error) {
v := C.jv_parse(C.CString(value))
if C.jv_is_valid(v) == 0 {
return C.jv_null(), errors.New("Invalid JSON")
}
return v, nil
}
func dumpJson(jv C.jv) string {
jv = C.jv_copy(jv)
strJv := C.jv_dump_string(jv, 0)
result := C.jv_string_value(strJv)
freeJv(strJv)
return C.GoString(result)
}
func refcount(jv C.jv) int {
return int(C.jv_get_refcnt(jv))
}
func goToJv(v interface{}) C.jv {
if v == nil {
return C.jv_null()
}
value := reflect.Indirect(reflect.ValueOf(v))
switch value.Type().Kind() {
case reflect.Bool:
if value.Bool() {
return C.jv_true()
} else {
return C.jv_false()
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return C.jv_number(C.double(value.Int()))
// TODO reflect.Uintptr?
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return C.jv_number(C.double(value.Uint()))
case reflect.Float32, reflect.Float64:
return C.jv_number(C.double(value.Float()))
case reflect.String:
return C.jv_string(C.CString(value.String()))
case reflect.Array, reflect.Slice:
n := value.Len()
arr := C.jv_array_sized(C.int(n))
for i := 0; i < n; i++ {
item := goToJv(value.Index(i).Interface())
arr = C.jv_array_set(C.jv_copy(arr), C.int(i), item)
}
return arr
case reflect.Map:
// TODO assert key is string?
object := C.jv_object()
for _, k := range value.MapKeys() {
key := goToJv(k.Interface())
mapValue := goToJv(value.MapIndex(k).Interface())
object = C.jv_object_set(object, key, mapValue)
}
return object
}
msg := fmt.Sprintf("unknown type for: %v", value.Interface())
return C.jv_invalid_with_msg(C.jv_string(C.CString(msg)))
}
func jvToGo(value C.jv) interface{} {
switch C.jv_get_kind(value) {
case C.JV_KIND_INVALID:
return errors.New("invalid")
case C.JV_KIND_NULL:
return nil
case C.JV_KIND_FALSE:
return false
case C.JV_KIND_TRUE:
return true
case C.JV_KIND_NUMBER:
number := C.jv_number_value(value)
if C.jv_is_integer(value) == 0 {
return float64(number)
} else {
return int(number)
}
case C.JV_KIND_STRING:
return C.GoString(C.jv_string_value(value))
case C.JV_KIND_ARRAY:
length := C.jv_array_length(C.jv_copy(value))
arr := make([]interface{}, length)
for i := range arr {
arr[i] = jvToGo(C.jv_array_get(C.jv_copy(value), C.int(i)))
}
return arr
case C.JV_KIND_OBJECT:
result := make(map[string]interface{})
var k, v C.jv
for jv_i := C.jv_object_iter(value); C.jv_object_iter_valid(value, jv_i) != 0; jv_i = C.jv_object_iter_next(value, jv_i) {
k = C.jv_object_iter_key(value, jv_i)
v = C.jv_object_iter_value(value, jv_i)
result[C.GoString(C.jv_string_value(k))] = jvToGo(v)
}
return result
default:
return errors.New("unknown type")
}
}
func freeJv(jv C.jv) {
C.jv_free(jv)
}
func isValid(jv C.jv) bool {
return C.jv_is_valid(jv) != 0
}