Skip to content

Commit da7fdcf

Browse files
authored
Update Go & add examples (#90)
1 parent 1a32809 commit da7fdcf

File tree

11 files changed

+1166
-1001
lines changed

11 files changed

+1166
-1001
lines changed

.github/workflows/go.yml

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,19 +11,19 @@ jobs:
1111
build:
1212
strategy:
1313
matrix:
14-
go-version: [1.18.x, 1.19.x, 1.20.x]
14+
go-version: [1.22.x, 1.23.x, 1.24.x]
1515
os: [ubuntu-latest, macos-latest, windows-latest]
1616
env:
1717
CGO_ENABLED: 0
1818
runs-on: ${{ matrix.os }}
1919
steps:
2020
- name: Set up Go
21-
uses: actions/setup-go@v2
21+
uses: actions/setup-go@v5
2222
with:
2323
go-version: ${{ matrix.go-version }}
2424

2525
- name: Checkout code
26-
uses: actions/checkout@v2
26+
uses: actions/checkout@v4
2727

2828
- name: Test
2929
run: go test ./...
@@ -42,12 +42,12 @@ jobs:
4242
runs-on: ubuntu-latest
4343
steps:
4444
- name: Set up Go
45-
uses: actions/setup-go@v2
45+
uses: actions/setup-go@v5
4646
with:
47-
go-version: 1.20.x
47+
go-version: stable
4848

4949
- name: Checkout code
50-
uses: actions/checkout@v2
50+
uses: actions/checkout@v4
5151

5252
- name: fmt
5353
run: diff <(gofmt -d .) <(printf "")

.github/workflows/vulncheck.yml

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ jobs:
1414
runs-on: ubuntu-latest
1515
strategy:
1616
matrix:
17-
go-version: [ 1.20.x ]
17+
go-version: [ stable ]
1818
steps:
1919
- name: Check out code into the Go module directory
20-
uses: actions/checkout@v3
21-
- uses: actions/setup-go@v3
20+
uses: actions/checkout@v4
21+
- uses: actions/setup-go@v5
2222
with:
2323
go-version: ${{ matrix.go-version }}
2424
check-latest: true

benchmarks/go.mod

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
module github.com/minio/simdjson-go/benchmarks
22

3-
go 1.17
3+
go 1.22
44

55
require (
66
github.com/buger/jsonparser v1.1.1
77
github.com/json-iterator/go v1.1.12
8-
github.com/klauspost/compress v1.14.2
8+
github.com/klauspost/compress v1.18.0
99
github.com/minio/simdjson-go v0.0.0-00010101000000-000000000000
1010
)
1111

1212
replace github.com/minio/simdjson-go => ../
1313

1414
require (
15-
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
15+
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
1616
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
1717
github.com/modern-go/reflect2 v1.0.2 // indirect
18+
golang.org/x/sys v0.30.0 // indirect
1819
)

benchmarks/go.sum

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,20 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
44
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
55
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
66
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
7-
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
87
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
98
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
10-
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
11-
github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi28pRw=
12-
github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
13-
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
14-
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
9+
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
10+
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
11+
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
12+
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
1513
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
1614
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
17-
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
1815
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
1916
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
2017
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
2118
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
2219
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
2320
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
2421
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
22+
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
23+
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=

examples/find/start2.go

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/minio/simdjson-go"
8+
)
9+
10+
func main() {
11+
// Parse JSON:
12+
pj, err := simdjson.Parse([]byte(`{
13+
"key": "val",
14+
"list": [{
15+
"key1": "{\"key2\": \"val1\"}"
16+
}]
17+
}`), nil)
18+
if err != nil {
19+
log.Fatal(err)
20+
}
21+
22+
iter := pj.Iter()
23+
list, err := iter.FindElement(nil, "list")
24+
if err != nil {
25+
log.Fatal(err)
26+
}
27+
arr, err := list.Iter.Array(nil)
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
arr.ForEach(func(i simdjson.Iter) {
32+
if i.Type() != simdjson.TypeObject {
33+
return
34+
}
35+
elem, err := i.FindElement(nil, `key1`)
36+
if err != nil {
37+
return
38+
}
39+
fmt.Println(elem.Iter.String())
40+
})
41+
// Output:
42+
// {"key2": "val1"} <nil>
43+
}

examples/find_recursive.go

+91
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/minio/simdjson-go"
8+
)
9+
10+
func main() {
11+
// Parse JSON and print all "scores" as single item.
12+
pj, err := simdjson.Parse([]byte(`{
13+
"items": [
14+
{
15+
"name": "jim",
16+
"scores": [
17+
{
18+
"game":"golf",
19+
"scores": ["one","two"]
20+
},
21+
{
22+
"game":"badminton",
23+
"scores":["zero",1,"six"]
24+
},
25+
[
26+
{
27+
"game": "nested for some reason?",
28+
"scores": ["five"]
29+
}
30+
]
31+
]
32+
}
33+
]
34+
}`), nil)
35+
if err != nil {
36+
log.Fatal(err)
37+
}
38+
39+
// Iterate each top level element.
40+
//traverseKeys := map[string]struct{}{"items": {}}
41+
var parse func(i simdjson.Iter) error
42+
parse = func(i simdjson.Iter) error {
43+
switch i.Type() {
44+
case simdjson.TypeArray:
45+
array, err := i.Array(nil)
46+
if err != nil {
47+
return err
48+
}
49+
array.ForEach(func(i simdjson.Iter) {
50+
parse(i)
51+
})
52+
case simdjson.TypeObject:
53+
obj, err := i.Object(nil)
54+
if err != nil {
55+
return err
56+
}
57+
scores := obj.FindKey("scores", nil)
58+
if scores == nil || scores.Type != simdjson.TypeArray {
59+
return obj.ForEach(func(_ []byte, i simdjson.Iter) {
60+
parse(i)
61+
}, nil)
62+
}
63+
array, err := scores.Iter.Array(nil)
64+
if err != nil {
65+
return err
66+
}
67+
array.ForEach(func(i simdjson.Iter) {
68+
switch i.Type() {
69+
case simdjson.TypeString, simdjson.TypeInt:
70+
s, _ := i.StringCvt()
71+
fmt.Println("Found score:", s)
72+
case simdjson.TypeObject, simdjson.TypeArray:
73+
parse(i)
74+
}
75+
})
76+
default:
77+
fmt.Println("Ignoring", i.Type())
78+
}
79+
80+
return nil
81+
}
82+
_ = pj.ForEach(parse)
83+
84+
// Output:
85+
//Found score: one
86+
//Found score: two
87+
//Found score: zero
88+
//Found score: 1
89+
//Found score: six
90+
//Found score: five
91+
}

0 commit comments

Comments
 (0)