-
Notifications
You must be signed in to change notification settings - Fork 8
/
example.go
144 lines (130 loc) · 2.77 KB
/
example.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
package main
import (
"fmt"
"io/ioutil"
"os"
"sync"
. "github.com/flant/libjq-go"
)
/*
Run it locally if oniguruma and jq are compiled:
CGO_ENABLED=1 \
CGO_CFLAGS="-Ipath-to-jq_lib/include" \
CGO_LDFLAGS="-Lpath-to-oniguruma_lib/lib -Lpath-to-jq_lib/lib" \
go run example.go
1. "bar"
2. kebab-string-here "kebabStringHere"
3. "bar-quux"
3. "baz-baz"
4. "Foo quux"
4. "Foo baz"
5. "bar"
5. "bar"
5. "bar"
*/
func main() {
var res string
var err error
var inputJsons []string
// 1. Run one program with one input.
res, err = Jq().Program(".foo").Run(`{"foo":"bar"}`)
if err != nil {
panic(err)
}
fmt.Printf("1. %s\n", res)
// Should print
// 1. "bar"
// 2. Use directory with jq modules.
prepareJqLib()
res, err = Jq().WithLibPath("./jq_lib").
Program(`include "mylibrary"; .foo|mymethod`).
Run(`{"foo":"kebab-string-here"}`)
fmt.Printf("2. %s %s\n", "kebab-string-here", res)
removeJqLib()
// Should print
// 2. kebab-string-here "kebabStringHere"
// 3. Use program text as a key for a cache.
inputJsons = []string{
`{ "foo":"bar-quux" }`,
`{ "foo":"baz-baz" }`,
// ...
}
for _, data := range inputJsons {
res, err = Jq().Program(".foo").Cached().Run(data)
if err != nil {
panic(err)
}
// Now do something with filter result ...
fmt.Printf("3. %s\n", res)
}
// Should print
// 3. "bar-quux"
// 3. "baz-baz"
// 4. Explicitly precompile jq expression.
inputJsons = []string{
`{ "bar":"Foo quux" }`,
`{ "bar":"Foo baz" }`,
// ...
}
prg, err := Jq().Program(".bar").Precompile()
if err != nil {
panic(err)
}
for _, data := range inputJsons {
res, err = prg.Run(data)
if err != nil {
panic(err)
}
// Now do something with filter result ...
fmt.Printf("4. %s\n", res)
}
// Should print
// 4. "Foo quux"
// 4. "Foo baz"
// 5. It is safe to use Jq() from multiple go-routines.
// Note however that programs are executed synchronously.
wg := sync.WaitGroup{}
wg.Add(3)
go func() {
res, err = Jq().Program(".foo").Run(`{"foo":"bar"}`)
if err != nil {
panic(err)
}
fmt.Printf("5. %s\n", res)
wg.Done()
}()
go func() {
res, err = Jq().Program(".foo").Cached().Run(`{"foo":"bar"}`)
if err != nil {
panic(err)
}
fmt.Printf("5. %s\n", res)
wg.Done()
}()
go func() {
res, err = Jq().Program(".foo").Cached().Run(`{"foo":"bar"}`)
if err != nil {
panic(err)
}
fmt.Printf("5. %s\n", res)
wg.Done()
}()
wg.Wait()
// Should print
// 5. "bar"
// 5. "bar"
// 5. "bar"
}
func prepareJqLib() {
if err := os.MkdirAll("./jq_lib", 0755); err != nil {
panic(err)
}
if err := ioutil.WriteFile("./jq_lib/mylibrary.jq", []byte(`def mymethod: gsub("-(?<a>[a-z])"; .a|ascii_upcase);`), 0644); err != nil {
panic(err)
}
}
func removeJqLib() {
if err := os.RemoveAll("./jq_lib"); err != nil {
panic(err)
}
}