Skip to content
Draft
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
48 changes: 12 additions & 36 deletions parser/argument.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@ func NewArgument(input *Input, field reflect.StructField) (*Argument, error) {
defaultValue: field.Tag.Get("default"),
}

if err := validateArgumentType(argument); err != nil {
return nil, err
}

type_, err := getOrCreateArgumentType(field.Type)
if err != nil {
return nil, err
Expand Down Expand Up @@ -79,45 +75,25 @@ func (arg *Argument) StructField() reflect.StructField {
return arg.structField
}

func validateArgumentType(arg *Argument) error {
kind, err := getTypeKind(arg.structField.Type)
func getOrCreateArgumentType(t reflect.Type) (Type, error) {
unupportedErr := fmt.Errorf("interface and union not supported for argument type")
kind, err := getTypeKind(t)
if err != nil {
return err
}

switch kind {
case KindInterface, KindUnion, KindInterfaceDefinition:
return fmt.Errorf(
"argument type %s not supported for field %s on struct %s \nif you think this is a mistake please open an issue at github.com/shreyas44/groot",
arg.structField.Type.Name(),
arg.structField.Name,
arg.Input().reflectType.Name(),
)
return nil, err
}

return nil
}

func getOrCreateArgumentType(t reflect.Type) (Type, error) {
parserType, ok := cache.get(t)
if ok {
kind, err := getTypeKind(t)
if err != nil {
return nil, err
}

switch kind {
case KindObject:
if _, ok := parserType.(*Input); ok {
return parserType, nil
}
case KindInterface, KindUnion, KindInterfaceDefinition:
err := fmt.Errorf("")
return nil, err
return nil, unupportedErr
default:
return parserType, nil
}

return parserType, nil
}

kind, err := getTypeKind(t)
if err != nil {
return nil, err
}

switch kind {
Expand All @@ -132,7 +108,7 @@ func getOrCreateArgumentType(t reflect.Type) (Type, error) {
case KindNullable:
return NewNullable(t, true)
case KindInterface, KindUnion, KindInterfaceDefinition:
return nil, fmt.Errorf("interface and union not supported for argument type")
return nil, unupportedErr
}

panic("parser: unexpected error occurred")
Expand Down
231 changes: 231 additions & 0 deletions parser/argument_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
package parser

import (
"errors"
"reflect"
"testing"

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

type ArgTestCustomScalar string
type ArgTestEnum string
type ArgTestEmptyInput struct{}
type ArgTestUnionMember1 struct{}
type ArgTestUnionMember2 struct{}

type ArgTestUnion struct {
UnionType
ArgTestUnionMember1
ArgTestUnionMember2
}

type ArgTestInterfaceDefinition struct {
InterfaceType
}

type ArgTestInterface interface {
ImplementsArgTestInterface() ArgTestInterfaceDefinition
}

type ArgTestInput struct {
StringArg string `json:"stringArg"`
NilJsonArg string `json:"-"`
ArgWithoutJSON string
//lint:ignore U1000 argument is used through reflection
unexportedArg string
}

const (
ArgTestEnum_One ArgTestEnum = "One"
ArgTestEnum_Two ArgTestEnum = "Two"
)

func (e ArgTestEnum) Values() []string {
return []string{
string(ArgTestEnum_One),
string(ArgTestEnum_Two),
}
}

func TestNewArgument(t *testing.T) {
inputType := reflect.TypeOf(ArgTestInput{})
stringType := reflect.TypeOf("")
stringArg, _ := inputType.FieldByName("StringArg")
input := &Input{reflectType: inputType}

arg, err := NewArgument(input, stringArg)
require.Nil(t, err)

expectedArg := &Argument{
input: input,
structField: stringArg,
type_: &Scalar{stringType},
jsonName: "stringArg",
}

assert.Equal(t, expectedArg, arg)

t.Run("TestNilArgumentReturned", func(t *testing.T) {
testCases := []struct {
name string
fieldName string
}{
{
name: "WithUnexportedArg",
fieldName: "unexportedArg",
},
{
name: "WithNilJsonArg",
fieldName: "NilJsonArg",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
field, _ := inputType.FieldByName(testCase.fieldName)
arg, err := NewArgument(input, field)
assert.Nil(t, arg)
assert.Nil(t, err)
})
}
})

t.Run("TestJSONName", func(t *testing.T) {
testCases := []struct {
name string
fieldName string
expectedJSONName string
}{
{
name: "WithJSONStructTag",
fieldName: "StringArg",
expectedJSONName: "stringArg",
},
{
name: "WithoutJSONStructTag",
fieldName: "ArgWithoutJSON",
expectedJSONName: "ArgWithoutJSON",
},
}

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
field, _ := inputType.FieldByName(testCase.fieldName)
arg, err := NewArgument(input, field)
assert.Nil(t, err)
assert.Equal(t, testCase.expectedJSONName, arg.JSONName())
})
}

})
}

func TestGetOrCreateArgumentType(t *testing.T) {
var (
stringType = reflect.TypeOf("")
customScalarType = reflect.TypeOf(ArgTestCustomScalar(""))
enumType = reflect.TypeOf(ArgTestEnum_One)
listType = reflect.TypeOf([]string{})
structType = reflect.TypeOf(ArgTestEmptyInput{})
interfaceType = reflect.TypeOf((*ArgTestInterface)(nil)).Elem()
interfaceDefType = reflect.TypeOf(ArgTestInterfaceDefinition{})
nullableStringType = reflect.TypeOf((*string)(nil))
unionType = reflect.TypeOf(ArgTestUnion{})
unsupportedErr = errors.New("interface and union not supported for argument type")
testCases = []struct {
name string
typ reflect.Type
expectedErr error
expectedType Type
}{
{
name: "Scalar",
typ: stringType,
expectedErr: nil,
expectedType: &Scalar{stringType},
},
{
name: "CustomScalar",
typ: customScalarType,
expectedErr: nil,
expectedType: &Scalar{customScalarType},
},
{
name: "Enum",
typ: enumType,
expectedErr: nil,
expectedType: &Enum{enumType, ArgTestEnum_One.Values()},
},
{
name: "List",
typ: listType,
expectedErr: nil,
expectedType: &Array{listType, &Scalar{stringType}},
},
{
name: "Input",
typ: structType,
expectedErr: nil,
expectedType: &Input{structType, nil, []*Argument{}},
},
{
name: "Nullable",
typ: nullableStringType,
expectedErr: nil,
expectedType: &Nullable{nullableStringType, &Scalar{stringType}},
},
{
name: "Interface",
typ: interfaceType,
expectedErr: unsupportedErr,
expectedType: nil,
},
{
name: "InterfaceDefinition",
typ: interfaceDefType,
expectedErr: unsupportedErr,
expectedType: nil,
},
{
name: "Union",
typ: unionType,
expectedErr: unsupportedErr,
expectedType: nil,
},
}
)

t.Run("WithEmptyCache", func(t *testing.T) {
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
resetCache()
typ, err := getOrCreateArgumentType(testCase.typ)
assert.Equal(t, testCase.expectedErr, err)
assert.Equal(t, testCase.expectedType, typ)
})
}
})

t.Run("WithCacheContainingFieldTypes", func(t *testing.T) {
resetCache()

// fill cache
cache.set(stringType, &Scalar{stringType})
cache.set(structType, &Object{structType, []*Field{}, []*Interface{}})
cache.set(interfaceType, &Interface{interfaceType, []*Field{}})
cache.set(unionType, &Union{unionType, []*Object{
{reflect.TypeOf(ArgTestUnionMember1{}), []*Field{}, []*Interface{}},
{reflect.TypeOf(ArgTestUnionMember2{}), []*Field{}, []*Interface{}},
}})

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
typ, err := getOrCreateArgumentType(testCase.typ)
assert.Equal(t, testCase.expectedErr, err)
assert.Equal(t, testCase.expectedType, typ)
})
}
})
}
40 changes: 40 additions & 0 deletions parser/array_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package parser

import (
"reflect"
"testing"

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

type ExampleArrayElem struct{}

func TestParsedArray(t *testing.T) {
var (
stringListType = reflect.TypeOf([]string{})
stringType = reflect.TypeOf("")
structListType = reflect.TypeOf([]ExampleArrayElem{})
structElem = structListType.Elem()

testCases = []struct {
name string
isArg bool
reflectTyp reflect.Type
expectedType Type
}{
{"FieldWithStructElement", false, structListType, &Array{structListType, &Object{structElem, []*Field{}, []*Interface{}}}},
{"ArgWithStructElement", true, structListType, &Array{structListType, &Input{structElem, nil, []*Argument{}}}},
{"FieldWithStringElement", false, stringListType, &Array{stringListType, &Scalar{stringType}}},
{"ArgWithStringElement", true, stringListType, &Array{stringListType, &Scalar{stringType}}},
}
)

for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
typ, err := NewArray(testCase.reflectTyp, testCase.isArg)
require.Nil(t, err)
assert.Equal(t, testCase.expectedType, typ, testCase)
})
}
}
33 changes: 33 additions & 0 deletions parser/cache_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package parser

import (
"reflect"
"testing"

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

func resetCache() {
cache = map[reflect.Type]Type{}
}

func TestCache(t *testing.T) {
resetCache()
stringType := reflect.TypeOf("")
intType := reflect.TypeOf(0)
stringScalar := &Scalar{stringType}

t.Run("CacheExists", func(t *testing.T) {
cache.set(stringType, stringScalar)

cacheVal, exists := cache.get(stringType)
assert.Equal(t, stringScalar, cacheVal)
assert.True(t, exists)
})

t.Run("CacheNotExists", func(t *testing.T) {
cacheVal, exists := cache.get(intType)
assert.Nil(t, cacheVal)
assert.False(t, exists)
})
}
Loading