Skip to content
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

Evaluate rhs after string slices #76

Merged
merged 5 commits into from
Jan 14, 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
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
Loading