diff --git a/go.mod b/go.mod index 7cf94e0..0763e47 100644 --- a/go.mod +++ b/go.mod @@ -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 @@ -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 diff --git a/go.sum b/go.sum index 01ac701..a9ad765 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/pkg/expressions/keyBuilder_test.go b/pkg/expressions/keyBuilder_test.go index dcf1c2a..d78b2bd 100644 --- a/pkg/expressions/keyBuilder_test.go +++ b/pkg/expressions/keyBuilder_test.go @@ -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++ { diff --git a/pkg/expressions/stdlib/funcsJson.go b/pkg/expressions/stdlib/funcsJson.go index f8a0deb..dc9d782 100644 --- a/pkg/expressions/stdlib/funcsJson.go +++ b/pkg/expressions/stdlib/funcsJson.go @@ -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) diff --git a/pkg/expressions/stdlib/funcsJson_test.go b/pkg/expressions/stdlib/funcsJson_test.go new file mode 100644 index 0000000..bfb25e3 --- /dev/null +++ b/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}`, "") +} + +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) + } +} diff --git a/pkg/expressions/stdlib/funcs_test.go b/pkg/expressions/stdlib/funcs_test.go index 739ca24..c993ca0 100644 --- a/pkg/expressions/stdlib/funcs_test.go +++ b/pkg/expressions/stdlib/funcs_test.go @@ -128,18 +128,6 @@ func TestHumanize(t *testing.T) { "12,345 12,345.1235 ") } -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}`, "") -} - func TestFormat(t *testing.T) { testExpression(t, mockContext(), `{format "%10s" abc}`, " abc") }