Skip to content
Open
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
24 changes: 17 additions & 7 deletions evaluate.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,15 +222,25 @@ func evaluateMatchExpression(expression *grammar.MatchExpression, datum interfac
ValueTransformationHook: opts.withHookFn,
},
}
val, err := ptr.Get(datum)
if err != nil {
if errors.Is(err, pointerstructure.ErrNotFound) && opts.withUnknown != nil {
err = nil
val = *opts.withUnknown
}

var val interface{}
var err error

switch expression.Selector.Type {
case grammar.SelectorTypeInlineValue:
val = strings.Split(expression.Selector.InlineValue.Raw, ",")

default:
val, err = ptr.Get(datum)
if err != nil {
return false, fmt.Errorf("error finding value in datum: %w", err)
if errors.Is(err, pointerstructure.ErrNotFound) && opts.withUnknown != nil {
err = nil
val = *opts.withUnknown
}

if err != nil {
return false, fmt.Errorf("error finding value in datum: %w", err)
}
}
}

Expand Down
4 changes: 4 additions & 0 deletions evaluate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,10 @@ var evaluateTests map[string]expressionTest = map[string]expressionTest{
{expression: "String not matches `^anchored.*`", result: true, benchQuick: true},
{expression: "String matches `^anchored.*`", result: false},
{expression: "String not matches `^ex.*`", result: false},
{expression: "foobar in [ `foo`, `bar` ]", result: false},
{expression: "foobar in [`barfoo`,`foobar`]", result: true},
{expression: "6 in [`abc123`, `456789`]", result: false},
{expression: "6 in [`abc123`, `6`, `456789`]", result: true},
},
},
"Flat Struct Alt Types": {
Expand Down
3 changes: 1 addition & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module github.com/hashicorp/go-bexpr
go 1.14

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mitchellh/pointerstructure v1.2.1
github.com/stretchr/testify v1.7.0
github.com/stretchr/testify v1.8.1
)
11 changes: 8 additions & 3 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,14 @@ github.com/mitchellh/pointerstructure v1.2.1/go.mod h1:BRAsLI5zgXmw97Lf6s25bs8oh
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5CcY=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
8 changes: 6 additions & 2 deletions grammar/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,13 @@ const (
SelectorTypeUnknown = iota
SelectorTypeBexpr
SelectorTypeJsonPointer
SelectorTypeInlineValue
)

type Selector struct {
Type SelectorType
Path []string
Type SelectorType
Path []string
InlineValue *MatchValue
}

func (sel Selector) String() string {
Expand All @@ -119,6 +121,8 @@ func (sel Selector) String() string {
return strings.Join(sel.Path, ".")
case SelectorTypeJsonPointer:
return strings.Join(sel.Path, "/")
case SelectorTypeInlineValue:
return sel.InlineValue.Raw
default:
return ""
}
Expand Down
Loading