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

fix: projecting over inexistent fields #432

Merged
merged 3 commits into from
Jul 30, 2024
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
2 changes: 1 addition & 1 deletion pkg/engine/assert/expression.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ type expression struct {
engine string
}

func parseExpressionRegex(ctx context.Context, in string) *expression {
func parseExpressionRegex(_ context.Context, in string) *expression {
expression := &expression{}
// 1. match foreach
if match := foreachRegex.FindStringSubmatch(in); match != nil {
Expand Down
2 changes: 2 additions & 0 deletions pkg/engine/assert/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@
projection, err := project(ctx, k, value, bindings, opts...)
if err != nil {
return nil, field.InternalError(path.Child(fmt.Sprint(k)), err)
} else if projection == nil {
errs = append(errs, field.Required(path.Child(fmt.Sprint(k)), "projection is not valid"))

Check warning on line 55 in pkg/engine/assert/parse.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/parse.go#L55

Added line #L55 was not covered by tests
} else {
if projection.binding != "" {
bindings = bindings.Register("$"+projection.binding, jpbinding.NewBinding(projection.result))
Expand Down
20 changes: 8 additions & 12 deletions pkg/engine/assert/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package assert

import (
"context"
"errors"
"reflect"

"github.com/jmespath-community/go-jmespath/pkg/binding"
Expand Down Expand Up @@ -33,31 +34,26 @@ func project(ctx context.Context, key any, value any, bindings binding.Bindings,
} else {
if reflectutils.GetKind(value) == reflect.Map {
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement))
var value any
if mapValue.IsValid() {
value = mapValue.Interface()
if !mapValue.IsValid() {
return nil, nil
}
return &projection{
foreach: expression.foreach,
foreachName: expression.foreachName,
binding: expression.binding,
result: value,
result: mapValue.Interface(),
}, nil
}
}
}
if reflectutils.GetKind(value) == reflect.Map {
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key))
var value any
if mapValue.IsValid() {
value = mapValue.Interface()
if !mapValue.IsValid() {
return nil, nil
}
return &projection{
result: value,
result: mapValue.Interface(),
}, nil
}
// TODO is this an error ?
return &projection{
result: value,
}, nil
return nil, errors.New("projection not recognized")
}
62 changes: 61 additions & 1 deletion pkg/engine/assert/project_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,76 @@ func Test_project(t *testing.T) {
bindings: nil,
want: nil,
wantErr: false,
}, {
name: "map index found",
key: "bar",
value: map[string]any{
"bar": 42,
},
bindings: nil,
want: &projection{
result: 42,
},
wantErr: false,
}, {
name: "map index found (and nil)",
key: "bar",
value: map[string]any{
"bar": nil,
},
bindings: nil,
want: &projection{
result: nil,
},
wantErr: false,
}, {
name: "non string key (not found)",
key: 3,
value: map[int]any{
2: "foo",
},
bindings: nil,
want: nil,
wantErr: false,
}, {
name: "non string key (found)",
key: 2,
value: map[int]any{
2: "foo",
},
bindings: nil,
want: &projection{
result: "foo",
},
wantErr: false,
}, {
name: "non string key (found and nil)",
key: 2,
value: map[int]any{
2: nil,
},
bindings: nil,
want: &projection{
result: nil,
},
wantErr: false,
}, {
name: "nil value",
key: "foo",
value: nil,
bindings: nil,
want: nil,
wantErr: true,
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := project(context.TODO(), tt.key, tt.value, tt.bindings)
if tt.wantErr {
tassert.Error(t, err)
tassert.Equal(t, tt.want, got)
} else {
tassert.NoError(t, err)
}
tassert.Equal(t, tt.want, got)
})
}
}
Loading