Skip to content

Commit

Permalink
fix: index not found should not trigger error (#218)
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 Nov 20, 2023
1 parent c04375f commit cc54ee5
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 13 deletions.
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 @@ package assert

import (
"context"
"fmt"
"reflect"

"github.com/jmespath-community/go-jmespath/pkg/binding"
Expand Down Expand Up @@ -33,26 +32,28 @@ func project(ctx context.Context, key interface{}, value interface{}, bindings b
}, 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()
}
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()
}
return &projection{
result: projected.Interface(),
result: value,
}, 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)
}
})
}
}

0 comments on commit cc54ee5

Please sign in to comment.