Skip to content

Commit

Permalink
Evaluate rhs after string slices (#76)
Browse files Browse the repository at this point in the history
* Fixes #69 - evaluate rhs after string slices

Signed-off-by: Springcomp <[email protected]>

* Added unit-test

Signed-off-by: Springcomp <[email protected]>

* Updated linter settings

Signed-off-by: Springcomp <[email protected]>

* Added unit-test for failing case

Signed-off-by: Springcomp <[email protected]>

* gofumpt

Signed-off-by: Springcomp <[email protected]>

---------

Signed-off-by: Springcomp <[email protected]>
  • Loading branch information
springcomp authored Jan 14, 2025
1 parent a74fd8e commit a8b468f
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 3 deletions.
11 changes: 11 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,14 @@ linters:
- whitespace
# - wrapcheck
# - wsl

linters-settings:
depguard:
rules:
main:
files:
- $all
- "!$test"
allow:
- $gostd
- github.com/jmespath-community/go-jmespath
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ build:
test: build
go test -v ./...

format:
gofumpt -l -w .

check:
go vet ./...
staticcheck ./...
Expand All @@ -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; }
2 changes: 1 addition & 1 deletion compliance
Submodule compliance updated 3 files
+139 −0 README.md
+0 −134 README.rst
+12 −0 tests/slice.json
8 changes: 7 additions & 1 deletion pkg/interpreter/interpreter.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
16 changes: 16 additions & 0 deletions pkg/interpreter/interpreter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit a8b468f

Please sign in to comment.