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: index not found should not trigger error #218

Merged
merged 1 commit into from
Nov 20, 2023
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
8 changes: 4 additions & 4 deletions pkg/engine/assert/expression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package assert

import (
"context"
"reflect"
"testing"

tassert "github.com/stretchr/testify/assert"
)

func Test_parseExpressionRegex(t *testing.T) {
Expand Down Expand Up @@ -170,9 +171,8 @@ func Test_parseExpressionRegex(t *testing.T) {
}}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := parseExpressionRegex(context.Background(), tt.in); !reflect.DeepEqual(got, tt.want) {
t.Errorf("parseExpressionRegex() = %v, want %v", got, tt.want)
}
got := parseExpressionRegex(context.Background(), tt.in)
tassert.Equal(t, tt.want, got)
})
}
}
19 changes: 10 additions & 9 deletions pkg/engine/assert/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import (
"context"
"fmt"
"reflect"

"github.com/jmespath-community/go-jmespath/pkg/binding"
Expand Down Expand Up @@ -33,26 +32,28 @@
}, nil
} else {
if reflectutils.GetKind(value) == reflect.Map {
projected := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement))
if !projected.IsValid() {
return nil, fmt.Errorf("failed to find the map index `%s`", expression.statement)
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(expression.statement))
var value interface{}
if mapValue.IsValid() {
value = mapValue.Interface()

Check warning on line 38 in pkg/engine/assert/project.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/project.go#L38

Added line #L38 was not covered by tests
}
return &projection{
foreach: expression.foreach,
foreachName: expression.foreachName,
binding: expression.binding,
result: projected.Interface(),
result: value,
}, nil
}
}
}
if reflectutils.GetKind(value) == reflect.Map {
projected := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key))
if !projected.IsValid() {
return nil, fmt.Errorf("failed to find the map index `%v`", key)
mapValue := reflect.ValueOf(value).MapIndex(reflect.ValueOf(key))
var value interface{}
if mapValue.IsValid() {
value = mapValue.Interface()

Check warning on line 53 in pkg/engine/assert/project.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/project.go#L50-L53

Added lines #L50 - L53 were not covered by tests
}
return &projection{
result: projected.Interface(),
result: value,

Check warning on line 56 in pkg/engine/assert/project.go

View check run for this annotation

Codecov / codecov/patch

pkg/engine/assert/project.go#L56

Added line #L56 was not covered by tests
}, nil
}
// TODO is this an error ?
Expand Down
40 changes: 40 additions & 0 deletions pkg/engine/assert/project_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package assert

import (
"context"
"testing"

"github.com/jmespath-community/go-jmespath/pkg/binding"
tassert "github.com/stretchr/testify/assert"
)

func Test_project(t *testing.T) {
tests := []struct {
name string
key interface{}
value interface{}
bindings binding.Bindings
want *projection
wantErr bool
}{{
name: "map index not found",
key: "foo",
value: map[string]interface{}{
"bar": 42,
},
bindings: nil,
want: nil,
wantErr: false,
}}
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)
}
})
}
}
Loading