From b296ca2145bea28b854924ce98240c948c9c7899 Mon Sep 17 00:00:00 2001 From: JatinDevDG Date: Mon, 14 Dec 2020 11:48:36 +0530 Subject: [PATCH] changed slice type to interface --- validator/vars.go | 9 ++++----- validator/vars_test.go | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/validator/vars.go b/validator/vars.go index b7ba0f25..a68b1538 100644 --- a/validator/vars.go +++ b/validator/vars.go @@ -10,8 +10,6 @@ import ( "github.com/dgraph-io/gqlparser/v2/gqlerror" ) -var UnexpectedType = fmt.Errorf("Unexpected Type") - // VariableValues coerces and validates variable values func VariableValues(schema *ast.Schema, op *ast.OperationDefinition, variables map[string]interface{}) (map[string]interface{}, *gqlerror.Error) { coercedVars := map[string]interface{}{} @@ -82,9 +80,10 @@ func (v *varValidator) validateVarType(typ *ast.Type, val reflect.Value) (reflec if val.Kind() != reflect.Slice { // GraphQL spec says that non-null values should be coerced to an array when possible. // Hence if the value is not a slice, we create a slice and add val to it. - slc := reflect.MakeSlice(reflect.SliceOf(val.Type()), 0, 0) - slc = reflect.Append(slc, val) - val = slc + slc := make([]interface{}, 0) + slc = append(slc, val.Interface()) + val = reflect.ValueOf(slc) + val.Convert(val.Type()) } for i := 0; i < val.Len(); i++ { resetPath() diff --git a/validator/vars_test.go b/validator/vars_test.go index a1807901..eb7a17c2 100644 --- a/validator/vars_test.go +++ b/validator/vars_test.go @@ -157,7 +157,7 @@ func TestValidateVars(t *testing.T) { "var": map[string]interface{}{"name": "hello"}, }) require.Nil(t, gerr) - require.EqualValues(t, []map[string]interface{}{{"name": "hello"}}, vars["var"]) + require.EqualValues(t, []interface{}{map[string]interface{}{"name": "hello"}}, vars["var"]) }) t.Run("non-null int value should be coerced to an array", func(t *testing.T) { @@ -166,7 +166,7 @@ func TestValidateVars(t *testing.T) { "var": 5, }) require.Nil(t, gerr) - expected := []int{5} + expected := []interface{}{5} require.EqualValues(t, expected, vars["var"]) }) @@ -176,7 +176,7 @@ func TestValidateVars(t *testing.T) { "var": []map[string]interface{}{{"and": 5}}, }) require.Nil(t, gerr) - expected := []map[string]interface{}{{"and": []int{5}}} + expected := []map[string]interface{}{{"and": []interface{}{5}}} require.EqualValues(t, expected, vars["var"]) })