Skip to content

Commit

Permalink
Finish Types.HasOnlyScalarOrArray() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 19, 2023
1 parent 0d72d26 commit 9c1f79a
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 3 deletions.
20 changes: 17 additions & 3 deletions internal/ast/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,17 @@ type Types []Type

func (types Types) HasOnlyScalarOrArray() bool {
for _, t := range types {
if t.Kind != KindScalar && t.Kind != KindArray {
return false
if t.Kind == KindArray {
if !t.AsArray().IsArrayOfScalars() {
return false
}

continue
}

// FIXME: for arrays, we should also inspect them recursively to make sure only scalar types are used
if t.Kind != KindScalar {
return false
}
}

return true
Expand Down Expand Up @@ -289,6 +295,14 @@ type ArrayType struct {
ValueType Type
}

func (t ArrayType) IsArrayOfScalars() bool {
if t.ValueType.Kind == KindArray {
return t.ValueType.AsArray().IsArrayOfScalars()
}

return t.ValueType.Kind == KindScalar
}

type EnumType struct {
Values []EnumValue // possible values. Value types might be different
}
Expand Down
162 changes: 162 additions & 0 deletions internal/ast/types_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
package ast

import (
"testing"

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

func TestTypes_HasOnlyScalarOrArray(t *testing.T) {
testCases := []struct {
description string
types Types
expected bool
}{
{
description: "only scalars",
types: []Type{
NewScalar(KindString),
NewScalar(KindBool),
},
expected: true,
},
{
description: "scalars and an array of scalars",
types: []Type{
NewScalar(KindString),
NewArray(NewScalar(KindInt8)),
},
expected: true,
},
{
description: "scalars and an array of refs",
types: []Type{
NewScalar(KindString),
NewArray(NewRef("SomeType")),
},
expected: false,
},
{
description: "ref",
types: []Type{
NewRef("SomeType"),
},
expected: false,
},
{
description: "scalars and ref",
types: []Type{
NewScalar(KindString),
NewRef("SomeType"),
},
expected: false,
},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.description, func(t *testing.T) {
req := require.New(t)

req.Equal(tc.expected, tc.types.HasOnlyScalarOrArray())
})
}
}

func TestTypes_HasOnlyRefs(t *testing.T) {
testCases := []struct {
description string
types Types
expected bool
}{
{
description: "only scalars",
types: []Type{
NewScalar(KindString),
NewScalar(KindBool),
},
expected: false,
},
{
description: "scalars and an array of scalars",
types: []Type{
NewScalar(KindString),
NewArray(NewScalar(KindInt8)),
},
expected: false,
},
{
description: "refs",
types: []Type{
NewRef("SomeType"),
NewRef("SomeOtherType"),
},
expected: true,
},
{
description: "ref",
types: []Type{
NewRef("SomeType"),
},
expected: true,
},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.description, func(t *testing.T) {
req := require.New(t)

req.Equal(tc.expected, tc.types.HasOnlyRefs())
})
}
}

func TestArrayType_IsArrayOfScalars(t *testing.T) {
testCases := []struct {
description string
array ArrayType
Expected bool
}{
{
description: "array of scalars",
array: ArrayType{
ValueType: NewScalar(KindString),
},
Expected: true,
},
{
description: "array of array of scalars",
array: ArrayType{
ValueType: NewArray(NewScalar(KindString)),
},
Expected: true,
},
{
description: "array of refs",
array: ArrayType{
ValueType: NewRef("SomeType"),
},
Expected: false,
},
{
description: "array of structs",
array: ArrayType{
ValueType: NewStruct(NewStructField("Foo", NewScalar(KindString))),
},
Expected: false,
},
}

for _, testCase := range testCases {
tc := testCase

t.Run(tc.description, func(t *testing.T) {
req := require.New(t)

req.Equal(tc.Expected, tc.array.IsArrayOfScalars())
})
}
}

0 comments on commit 9c1f79a

Please sign in to comment.