Skip to content

Commit

Permalink
fix: add path to assertion parsing (#515)
Browse files Browse the repository at this point in the history
Signed-off-by: Charles-Edouard Brétéché <charles.edouard@nirmata.com>
  • Loading branch information
eddycharly authored Sep 23, 2024
1 parent ae40afb commit 9d7931b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 10 deletions.
30 changes: 20 additions & 10 deletions pkg/core/assertion/assertion.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,21 @@ type Assertion interface {
}

func Parse(assertion any, compiler compilers.Compilers) (Assertion, error) {
out, err := parse(nil, assertion, compiler)
if err != nil {
return nil, err
}
return out, nil
}

func parse(path *field.Path, assertion any, compiler compilers.Compilers) (Assertion, *field.Error) {
switch reflectutils.GetKind(assertion) {
case reflect.Slice:
return parseSlice(assertion, compiler)
return parseSlice(path, assertion, compiler)
case reflect.Map:
return parseMap(assertion, compiler)
return parseMap(path, assertion, compiler)
default:
return parseScalar(assertion, compiler)
return parseScalar(path, assertion, compiler)
}
}

Expand Down Expand Up @@ -55,11 +63,12 @@ func (node sliceNode) Assert(path *field.Path, value any, bindings binding.Bindi
return errs, nil
}

func parseSlice(assertion any, compiler compilers.Compilers) (sliceNode, error) {
func parseSlice(path *field.Path, assertion any, compiler compilers.Compilers) (sliceNode, *field.Error) {
var assertions sliceNode
valueOf := reflect.ValueOf(assertion)
for i := 0; i < valueOf.Len(); i++ {
sub, err := Parse(valueOf.Index(i).Interface(), compiler)
path := path.Index(i)
sub, err := parse(path, valueOf.Index(i).Interface(), compiler)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -138,13 +147,14 @@ func (node mapNode) Assert(path *field.Path, value any, bindings binding.Binding
return errs, nil
}

func parseMap(assertion any, compiler compilers.Compilers) (mapNode, error) {
func parseMap(path *field.Path, assertion any, compiler compilers.Compilers) (mapNode, *field.Error) {
assertions := mapNode{}
iter := reflect.ValueOf(assertion).MapRange()
for iter.Next() {
key := iter.Key().Interface()
value := iter.Value().Interface()
assertion, err := Parse(value, compiler)
path := path.Child(fmt.Sprint(key))
assertion, err := parse(path, value, compiler)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -173,12 +183,12 @@ func (node scalarNode) Assert(path *field.Path, value any, bindings binding.Bind
return errs, nil
}

func parseScalar(in any, compiler compilers.Compilers) (scalarNode, error) {
func parseScalar(path *field.Path, in any, compiler compilers.Compilers) (scalarNode, *field.Error) {
proj, err := projection.ParseScalar(in, compiler)
if err != nil {
return nil, err
return nil, field.InternalError(path, err)
}
return proj, err
return proj, nil
}

func expectValueMessage(value any) string {
Expand Down
29 changes: 29 additions & 0 deletions pkg/core/assertion/assertion_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,32 @@ func TestAssert(t *testing.T) {
})
}
}

func TestParse(t *testing.T) {
tests := []struct {
name string
assertion any
want field.ErrorList
wantErr bool
}{{
name: "bad scalar",
assertion: map[string]any{
"foo": map[string]any{
"bar": "~.(`42`)",
},
},
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
compiler := compilers.DefaultCompilers
parsed, err := Parse(tt.assertion, compiler)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.NotNil(t, parsed)
}
})
}
}

0 comments on commit 9d7931b

Please sign in to comment.