From a8b468f0443647e872f294193ba66bf3450fc32b Mon Sep 17 00:00:00 2001 From: Maxime Labelle Date: Tue, 14 Jan 2025 12:41:08 +0000 Subject: [PATCH] Evaluate rhs after string slices (#76) * Fixes #69 - evaluate rhs after string slices Signed-off-by: Springcomp * Added unit-test Signed-off-by: Springcomp * Updated linter settings Signed-off-by: Springcomp * Added unit-test for failing case Signed-off-by: Springcomp * gofumpt Signed-off-by: Springcomp --------- Signed-off-by: Springcomp --- .golangci.yml | 11 +++++++++++ Makefile | 6 +++++- compliance | 2 +- pkg/interpreter/interpreter.go | 8 +++++++- pkg/interpreter/interpreter_test.go | 16 ++++++++++++++++ 5 files changed, 40 insertions(+), 3 deletions(-) diff --git a/.golangci.yml b/.golangci.yml index f2d8dc8..0446c77 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -64,3 +64,14 @@ linters: - whitespace # - wrapcheck # - wsl + +linters-settings: + depguard: + rules: + main: + files: + - $all + - "!$test" + allow: + - $gostd + - github.com/jmespath-community/go-jmespath \ No newline at end of file diff --git a/Makefile b/Makefile index de9f6a4..03d1562 100644 --- a/Makefile +++ b/Makefile @@ -21,6 +21,9 @@ build: test: build go test -v ./... +format: + gofumpt -l -w . + check: go vet ./... staticcheck ./... @@ -43,5 +46,6 @@ pprof-cpu: install-dev-cmds: go install honnef.co/go/tools/cmd/staticcheck@latest + go install mvdan.cc/gofumpt@latest go install golang.org/x/tools/cmd/stringer@latest - command -v golangci-lint || { curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.46.2; } + command -v golangci-lint || { curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $$(go env GOPATH)/bin v1.61.0; } diff --git a/compliance b/compliance index ffa8a5a..304b287 160000 --- a/compliance +++ b/compliance @@ -1 +1 @@ -Subproject commit ffa8a5ac6eb73c4297ff7d997ac5786ee4309d59 +Subproject commit 304b287a9537673227c2e300a34ff8e4757579c5 diff --git a/pkg/interpreter/interpreter.go b/pkg/interpreter/interpreter.go index 971c357..7228caa 100644 --- a/pkg/interpreter/interpreter.go +++ b/pkg/interpreter/interpreter.go @@ -374,7 +374,13 @@ func (intr *treeInterpreter) execute(node parsing.ASTNode, value any, functionCa } stringType, ok := left.(string) if allowString && ok { - return stringType, nil + // a projection is really a sub-expression in disguise + // we must evaluate the right hand expression + result, err := intr.Execute(node.Children[1], stringType) + if err != nil { + return nil, err + } + return result, nil } return nil, nil } diff --git a/pkg/interpreter/interpreter_test.go b/pkg/interpreter/interpreter_test.go index 8e8d015..4d01bf4 100644 --- a/pkg/interpreter/interpreter_test.go +++ b/pkg/interpreter/interpreter_test.go @@ -193,6 +193,22 @@ func TestCanSupportSliceOfStructsWithFunctions(t *testing.T) { assert.Equal(result.(float64), 2.0) } +func TestCanSupportEvaluatingRightHandSideOfStringSlice(t *testing.T) { + assert := assert.New(t) + data := make(map[string]interface{}) + result, err := search(t, "'foo'[:].length(@)", data) + assert.Nil(err) + assert.Equal(result.(float64), 3.0) +} + +func TestErrEvaluatingRightHandSideOfStringSlice(t *testing.T) { + assert := assert.New(t) + data := make(map[string]interface{}) + _, err := search(t, "'foo'[:].unknown(@)", data) + assert.NotNil(err) + assert.Equal("unknown function: unknown", err.Error()) +} + func BenchmarkInterpretSingleFieldStruct(b *testing.B) { assert := assert.New(b) intr := NewInterpreter(nil, nil)