Skip to content

Commit

Permalink
Bump gjson version and add more json tests
Browse files Browse the repository at this point in the history
  • Loading branch information
zix99 committed Jun 7, 2022
1 parent 8a56a1a commit 5360801
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 17 deletions.
6 changes: 3 additions & 3 deletions go.mod
Expand Up @@ -9,7 +9,7 @@ require (
github.com/fsnotify/fsnotify v1.4.9
github.com/kr/pretty v0.1.0 // indirect
github.com/stretchr/testify v1.7.0
github.com/tidwall/gjson v1.3.5
github.com/tidwall/gjson v1.14.1
github.com/urfave/cli v1.22.5
golang.org/x/sys v0.0.0-20211019181941-9d821ace8654
golang.org/x/term v0.0.0-20210503060354-a79de5458b56
Expand All @@ -22,8 +22,8 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/tidwall/match v1.0.1 // indirect
github.com/tidwall/pretty v1.0.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.0 // indirect
golang.org/x/exp/typeparams v0.0.0-20220218215828-6cf2b201936e // indirect
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4 // indirect
golang.org/x/tools v0.1.11-0.20220513221640-090b14e8501f // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Expand Up @@ -30,10 +30,16 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/tidwall/gjson v1.3.5 h1:2oW9FBNu8qt9jy5URgrzsVx/T/KSn3qn/smJQ0crlDQ=
github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls=
github.com/tidwall/gjson v1.14.1 h1:iymTbGkQBhveq21bEvAQ81I0LEBork8BFe1CUZXdyuo=
github.com/tidwall/gjson v1.14.1/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.0.1 h1:PnKP62LPNxHKTwvHHZZzdOAOCtsJTjo6dZLCwpKm5xc=
github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.0.0 h1:HsD+QiTn7sK6flMKIvNmpqz1qrpP3Ps6jOKIKMooyg4=
github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk=
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/urfave/cli v1.22.5 h1:lNq9sAHXK2qfdI8W+GRItjCEkI+2oR4d+MEHy1CKXoU=
github.com/urfave/cli v1.22.5/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
github.com/yuin/goldmark v1.4.1/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
Expand Down
13 changes: 12 additions & 1 deletion pkg/expressions/keyBuilder_test.go
Expand Up @@ -120,8 +120,19 @@ func TestManyStagesOptimize(t *testing.T) {
assert.Equal(t, "value: -1 8", kb.BuildKey(&KeyBuilderContextArray{}))
}

// BenchmarkSimpleFunc-4 5198154 221.5 ns/op 32 B/op 2 allocs/op
func BenchmarkSimpleFunc(b *testing.B) {
k := NewKeyBuilder()
k := NewKeyBuilderEx(false)
k.Funcs(simpleFuncs)
kb, _ := k.Compile("value: {addi {addi 1 2} 2}")
for i := 0; i < b.N; i++ {
kb.BuildKey(&KeyBuilderContextArray{})
}
}

// BenchmarkOptimizedFunc-4 13708504 79.70 ns/op 24 B/op 1 allocs/op
func BenchmarkOptimizedFunc(b *testing.B) {
k := NewKeyBuilderEx(true)
k.Funcs(simpleFuncs)
kb, _ := k.Compile("value: {addi {addi 1 2} 2}")
for i := 0; i < b.N; i++ {
Expand Down
2 changes: 1 addition & 1 deletion pkg/expressions/stdlib/funcsJson.go
Expand Up @@ -8,7 +8,7 @@ import (

func kfJsonQuery(args []KeyBuilderStage) KeyBuilderStage {
if len(args) == 1 {
// Assumes "{0}" is the json blog to extract, so arg[0] is the key
// Assumes "{0}" is the json blob to extract, so arg[0] is the key
return KeyBuilderStage(func(context KeyBuilderContext) string {
json := context.GetMatch(0)
expression := args[0](context)
Expand Down
38 changes: 38 additions & 0 deletions pkg/expressions/stdlib/funcsJson_test.go
@@ -0,0 +1,38 @@
package stdlib

import (
"testing"
)

func TestJson(t *testing.T) {
testExpression(t, mockContext(`{"abc":123}`), `{json {0} abc}`, "123")
}

func TestJsonSingleArg(t *testing.T) {
testExpression(t, mockContext(`{"abc":456}`), `{json abc}`, "456")
}

func TestJsonManyArgs(t *testing.T) {
testExpression(t, mockContext(`{"abc":456}`), `{json {0} abc woops}`, "<ARGN>")
}

func TestJsonComplexObject(t *testing.T) {
testExpression(t, mockContext(`{"abc":{"efg":23}}`), `{json {0} abc.efg}`, "23")
testExpression(t, mockContext(`{"abc":{"efg":23}}`), `{json {0} abc.qef}`, "")
}

func TestJsonNestedArray(t *testing.T) {
testExpression(t, mockContext(`[1,2,3,4]`), `{json 1}`, "2")
testExpression(t, mockContext(`{"a":[1,2,3,4]}`), `{json a.1}`, "2")
testExpression(t, mockContext(`{"a":[{"efg":123},2,3,4]}`), `{json a.0}`, `{"efg":123}`)
testExpression(t, mockContext(`{"a":[{"efg":123},2,3,4]}`), `{json a.0.efg}`, `123`)
}

// BenchmarkJson-4 7041579 169.1 ns/op 0 B/op 0 allocs/op
func BenchmarkJson(b *testing.B) {
kb, _ := NewStdKeyBuilder().Compile("{json abc}")
context := mockContext(`{"abc":123}`)
for i := 0; i < b.N; i++ {
kb.BuildKey(context)
}
}
12 changes: 0 additions & 12 deletions pkg/expressions/stdlib/funcs_test.go
Expand Up @@ -128,18 +128,6 @@ func TestHumanize(t *testing.T) {
"12,345 12,345.1235 <BAD-TYPE> <BAD-TYPE>")
}

func TestJson(t *testing.T) {
testExpression(t, mockContext(`{"abc":123}`), `{json {0} abc}`, "123")
}

func TestJsonSingleArg(t *testing.T) {
testExpression(t, mockContext(`{"abc":456}`), `{json abc}`, "456")
}

func TestJsonManyArgs(t *testing.T) {
testExpression(t, mockContext(`{"abc":456}`), `{json {0} abc woops}`, "<ARGN>")
}

func TestFormat(t *testing.T) {
testExpression(t, mockContext(), `{format "%10s" abc}`, " abc")
}
Expand Down

0 comments on commit 5360801

Please sign in to comment.