From cc54ee543d93403690b228d4280d7e8dba96e9a4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Charles-Edouard=20Br=C3=A9t=C3=A9ch=C3=A9?= Date: Mon, 20 Nov 2023 10:16:37 +0100 Subject: [PATCH] fix: index not found should not trigger error (#218) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Charles-Edouard Brétéché --- pkg/engine/assert/expression_test.go | 8 +++--- pkg/engine/assert/project.go | 19 ++++++------- pkg/engine/assert/project_test.go | 40 ++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 13 deletions(-) create mode 100644 pkg/engine/assert/project_test.go diff --git a/pkg/engine/assert/expression_test.go b/pkg/engine/assert/expression_test.go index 401ea183..20e8c1a3 100644 --- a/pkg/engine/assert/expression_test.go +++ b/pkg/engine/assert/expression_test.go @@ -2,8 +2,9 @@ package assert import ( "context" - "reflect" "testing" + + tassert "github.com/stretchr/testify/assert" ) func Test_parseExpressionRegex(t *testing.T) { @@ -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) }) } } diff --git a/pkg/engine/assert/project.go b/pkg/engine/assert/project.go index 788934c8..38fd1fbb 100644 --- a/pkg/engine/assert/project.go +++ b/pkg/engine/assert/project.go @@ -2,7 +2,6 @@ package assert import ( "context" - "fmt" "reflect" "github.com/jmespath-community/go-jmespath/pkg/binding" @@ -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 ? diff --git a/pkg/engine/assert/project_test.go b/pkg/engine/assert/project_test.go new file mode 100644 index 00000000..ca164c10 --- /dev/null +++ b/pkg/engine/assert/project_test.go @@ -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) + } + }) + } +}