Skip to content

Update Go & add examples #90

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Mar 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions .github/workflows/go.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,19 @@ jobs:
build:
strategy:
matrix:
go-version: [1.18.x, 1.19.x, 1.20.x]
go-version: [1.22.x, 1.23.x, 1.24.x]
os: [ubuntu-latest, macos-latest, windows-latest]
env:
CGO_ENABLED: 0
runs-on: ${{ matrix.os }}
steps:
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: Test
run: go test ./...
Expand All @@ -42,12 +42,12 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Set up Go
uses: actions/setup-go@v2
uses: actions/setup-go@v5
with:
go-version: 1.20.x
go-version: stable

- name: Checkout code
uses: actions/checkout@v2
uses: actions/checkout@v4

- name: fmt
run: diff <(gofmt -d .) <(printf "")
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/vulncheck.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
go-version: [ 1.20.x ]
go-version: [ stable ]
steps:
- name: Check out code into the Go module directory
uses: actions/checkout@v3
- uses: actions/setup-go@v3
uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
go-version: ${{ matrix.go-version }}
check-latest: true
Expand Down
7 changes: 4 additions & 3 deletions benchmarks/go.mod
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
module github.com/minio/simdjson-go/benchmarks

go 1.17
go 1.22

require (
github.com/buger/jsonparser v1.1.1
github.com/json-iterator/go v1.1.12
github.com/klauspost/compress v1.14.2
github.com/klauspost/compress v1.18.0
github.com/minio/simdjson-go v0.0.0-00010101000000-000000000000
)

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

require (
github.com/klauspost/cpuid/v2 v2.0.9 // indirect
github.com/klauspost/cpuid/v2 v2.2.10 // indirect
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 // indirect
github.com/modern-go/reflect2 v1.0.2 // indirect
golang.org/x/sys v0.30.0 // indirect
)
13 changes: 6 additions & 7 deletions benchmarks/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.9/go.mod h1:KdQUCv79m/52Kvf8AW2vK1V8akMuk1QjK/uOdHXbAo4=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
github.com/klauspost/compress v1.13.6/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/compress v1.14.2 h1:S0OHlFk/Gbon/yauFJ4FfJJF5V0fc5HbBTJazi28pRw=
github.com/klauspost/compress v1.14.2/go.mod h1:/3/Vjq9QcHkK5uEr5lBEmyoZ1iFhe47etQ6QUkpK6sk=
github.com/klauspost/cpuid/v2 v2.0.9 h1:lgaqFMSdTdQYdZ04uHyN2d/eKdOMyi2YLSvlQIBFYa4=
github.com/klauspost/cpuid/v2 v2.0.9/go.mod h1:FInQzS24/EEf25PyTYn52gqo7WaD8xa0213Md/qVLRg=
github.com/klauspost/compress v1.18.0 h1:c/Cqfb0r+Yi+JtIEq73FWXVkRonBlf0CRNYc8Zttxdo=
github.com/klauspost/compress v1.18.0/go.mod h1:2Pp+KzxcywXVXMr50+X0Q/Lsb43OQHYWRCY2AiWywWQ=
github.com/klauspost/cpuid/v2 v2.2.10 h1:tBs3QSyvjDyFTq3uoc/9xFpCuOsJQFNPiAhYdw2skhE=
github.com/klauspost/cpuid/v2 v2.2.10/go.mod h1:hqwkgyIinND0mEev00jJYCxPNVRVXFQeu1XKlok6oO0=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421 h1:ZqeYNhU3OHLH3mGKHDcjJRFFRrJa6eAM5H+CtDdOsPc=
github.com/modern-go/concurrent v0.0.0-20180228061459-e0a39a4cb421/go.mod h1:6dJC0mAP4ikYIbvyc7fijjWJddQyLn8Ig3JB5CqoB9Q=
github.com/modern-go/reflect2 v0.0.0-20180701023420-4b7aa43c6742/go.mod h1:bx2lNnkwVCuqBIxFjflWJWanXIb3RllmbCylyMrvgv0=
github.com/modern-go/reflect2 v1.0.2 h1:xBagoLtFs94CBntxluKeaWgTMpvLxC4ur3nMaC9Gz0M=
github.com/modern-go/reflect2 v1.0.2/go.mod h1:yWuevngMOJpCy52FWWMvUC8ws7m/LJsjYzDa0/r8luk=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0 h1:TivCn/peBQ7UY8ooIcPgZFpTNSz0Q2U6UrFlUfqbe0Q=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
golang.org/x/sys v0.30.0 h1:QjkSwP/36a20jFYWkSue1YwXzLmsV5Gfq7Eiy72C1uc=
golang.org/x/sys v0.30.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
43 changes: 43 additions & 0 deletions examples/find/start2.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package main

import (
"fmt"
"log"

"github.com/minio/simdjson-go"
)

func main() {
// Parse JSON:
pj, err := simdjson.Parse([]byte(`{
"key": "val",
"list": [{
"key1": "{\"key2\": \"val1\"}"
}]
}`), nil)
if err != nil {
log.Fatal(err)
}

iter := pj.Iter()
list, err := iter.FindElement(nil, "list")
if err != nil {
log.Fatal(err)
}
arr, err := list.Iter.Array(nil)
if err != nil {
log.Fatal(err)
}
arr.ForEach(func(i simdjson.Iter) {
if i.Type() != simdjson.TypeObject {
return
}
elem, err := i.FindElement(nil, `key1`)
if err != nil {
return
}
fmt.Println(elem.Iter.String())
})
// Output:
// {"key2": "val1"} <nil>
}
91 changes: 91 additions & 0 deletions examples/find_recursive.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
package main

import (
"fmt"
"log"

"github.com/minio/simdjson-go"
)

func main() {
// Parse JSON and print all "scores" as single item.
pj, err := simdjson.Parse([]byte(`{
"items": [
{
"name": "jim",
"scores": [
{
"game":"golf",
"scores": ["one","two"]
},
{
"game":"badminton",
"scores":["zero",1,"six"]
},
[
{
"game": "nested for some reason?",
"scores": ["five"]
}
]
]
}
]
}`), nil)
if err != nil {
log.Fatal(err)
}

// Iterate each top level element.
//traverseKeys := map[string]struct{}{"items": {}}
var parse func(i simdjson.Iter) error
parse = func(i simdjson.Iter) error {
switch i.Type() {
case simdjson.TypeArray:
array, err := i.Array(nil)
if err != nil {
return err
}
array.ForEach(func(i simdjson.Iter) {
parse(i)
})
case simdjson.TypeObject:
obj, err := i.Object(nil)
if err != nil {
return err
}
scores := obj.FindKey("scores", nil)
if scores == nil || scores.Type != simdjson.TypeArray {
return obj.ForEach(func(_ []byte, i simdjson.Iter) {
parse(i)
}, nil)
}
array, err := scores.Iter.Array(nil)
if err != nil {
return err
}
array.ForEach(func(i simdjson.Iter) {
switch i.Type() {
case simdjson.TypeString, simdjson.TypeInt:
s, _ := i.StringCvt()
fmt.Println("Found score:", s)
case simdjson.TypeObject, simdjson.TypeArray:
parse(i)
}
})
default:
fmt.Println("Ignoring", i.Type())
}

return nil
}
_ = pj.ForEach(parse)

// Output:
//Found score: one
//Found score: two
//Found score: zero
//Found score: 1
//Found score: six
//Found score: five
}
Loading
Loading