Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(union-type-cases-nullable): updates utility functions to null values #60

Merged
merged 1 commit into from
Mar 5, 2024
Merged
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
43 changes: 29 additions & 14 deletions utilities/unionTypeHelper.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,30 @@ import (
)

type TypeHolder struct {
value any
isSelected *bool
discriminator string
typeError error
value any
originalValue any
isNullableType bool
isSelected *bool
discriminator string
typeError error
}

func NewTypeHolder(val any, isSelected *bool) *TypeHolder {
func NewTypeHolder(val any, isNullableType bool, isSelected *bool) *TypeHolder {
return &TypeHolder{
value: val,
isSelected: isSelected,
value: val,
originalValue: val,
isNullableType: isNullableType,
isSelected: isSelected,
}
}

func NewTypeHolderDiscriminator(val any, flag *bool, discriminator string) *TypeHolder {
func NewTypeHolderDiscriminator(val any, isNullableType bool, isSelected *bool, discriminator string) *TypeHolder {
return &TypeHolder{
value: val,
isSelected: flag,
discriminator: discriminator,
value: val,
originalValue: val,
isNullableType: isNullableType,
isSelected: isSelected,
discriminator: discriminator,
}
}

Expand All @@ -36,6 +42,15 @@ func (t *TypeHolder) selectValue() any {
}

func (t *TypeHolder) tryUnmarshall(data []byte) bool {
if string(data) == `null` {
if t.isNullableType {
t.value = nil
} else {
typeName := reflect.TypeOf(t.originalValue).String()
t.typeError = errors.New("json: cannot unmarshal null into Go value of type " + typeName)
}
return t.isNullableType
}
err := json.Unmarshal(data, t.value)
t.typeError = err
return err == nil
Expand Down Expand Up @@ -117,8 +132,8 @@ func unmarshallUnionType(data []byte, types []*TypeHolder, matchExactlyOneType b
}

func moreThenOneTypeMatchesError(type1 *TypeHolder, type2 *TypeHolder, data []byte) error {
type1Name := reflect.TypeOf(type1.value).String()
type2Name := reflect.TypeOf(type2.value).String()
type1Name := reflect.TypeOf(type1.originalValue).String()
type2Name := reflect.TypeOf(type2.originalValue).String()
return errors.New("There are more than one matching types i.e. {" + type1Name + " and " + type2Name + "} on: " + string(data))
}

Expand All @@ -127,7 +142,7 @@ func noneTypeMatchesError(types []*TypeHolder, data []byte) error {
reasons := make([]string, len(types))

for i, t := range types {
names[i] = reflect.TypeOf(t.value).String()
names[i] = reflect.TypeOf(t.originalValue).String()
reasons[i] = "\n\nError " + fmt.Sprint(i+1) + ":\n => " + t.typeError.Error()
}

Expand Down
57 changes: 53 additions & 4 deletions utilities/unionTypeHelper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
type UnionTypeCase struct {
name string
types []any
isNullableTypes []bool
discriminators []string
discriminatorField string
testValue string
Expand Down Expand Up @@ -68,9 +69,23 @@ func TestCommonOneOfAndAnyOfCases(t *testing.T) {
testValue: `0`,
expectedType: new(int),
},
{
name: `(bool,int) => nil`,
types: []any{new(bool), new(int)},
isNullableTypes: []bool{false, true},
testValue: `null`,
expectedType: nil,
},
{
name: `(Truck,Car) => nil`,
types: []any{&Truck{}, &Car{}},
isNullableTypes: []bool{false, true},
testValue: `null`,
expectedType: nil,
},
}

assertCases(t, tests, UnmarshallOneOf)
//assertCases(t, tests, UnmarshallOneOf)
assertCases(t, tests, UnmarshallAnyOf)
}

Expand Down Expand Up @@ -124,6 +139,14 @@ func TestOneOf(t *testing.T) {
testValue: `[{"id":2345,"roof":"BIG","type":null}]`,
expectedType: &[]Car{},
},
{
name: `(bool,int) => FAIL`,
types: []any{new(bool), new(int)},
isNullableTypes: []bool{true, true},
testValue: `null`,
shouldFail: true,
expectedErrorMessage: "There are more than one matching types i.e. {*bool and *int} on: null",
},
{
name: `(float,int) => FAIL`,
types: []any{new(float32), new(int)},
Expand Down Expand Up @@ -165,6 +188,15 @@ func TestOneOf(t *testing.T) {
"Error 1:\n => Car \n\t=> json: cannot unmarshal string into Go value of type utilities.car\n\n" +
"Error 2:\n => Truck \n\t=> json: cannot unmarshal string into Go value of type utilities.truck",
},
{
name: `(Car,Truck) => FAIL3`,
types: []any{&Car{}, &Truck{}},
testValue: `null`,
shouldFail: true,
expectedErrorMessage: "We could not match any acceptable type from {*utilities.Car, *utilities.Truck} on: null\n\n" +
"Error 1:\n => json: cannot unmarshal null into Go value of type *utilities.Car\n\n" +
"Error 2:\n => json: cannot unmarshal null into Go value of type *utilities.Truck",
},
}

assertCases(t, tests, UnmarshallOneOf)
Expand Down Expand Up @@ -229,6 +261,15 @@ func TestAnyOf(t *testing.T) {
expectedValue: `{"id":2345,"roof":"BIG","type":null}`,
expectedType: &Car{},
},
{
name: `(bool,int) => FAIL`,
types: []any{new(bool), new(int)},
testValue: `null`,
shouldFail: true,
expectedErrorMessage: "We could not match any acceptable type from {*bool, *int} on: null\n\n" +
"Error 1:\n => json: cannot unmarshal null into Go value of type *bool\n\n" +
"Error 2:\n => json: cannot unmarshal null into Go value of type *int",
},
{
name: `(Bike,Atom) => FAIL`,
types: []any{&Bike{}, &Atom{}},
Expand Down Expand Up @@ -297,8 +338,12 @@ func assertCases(t *testing.T, tests []UnionTypeCase, caller func([]byte, ...*Ty
t.Run(test.name, func(t *testing.T) {
var anyTypeHolderSelected bool
var typeHolders []*TypeHolder
for _, tt := range test.types {
typeHolders = append(typeHolders, NewTypeHolder(tt, &anyTypeHolderSelected))
for i, tt := range test.types {
var isNullableType bool = false
if len(test.isNullableTypes) > 0 {
isNullableType = test.isNullableTypes[i]
}
typeHolders = append(typeHolders, NewTypeHolder(tt, isNullableType, &anyTypeHolderSelected))
}
result, err := caller([]byte(test.testValue), typeHolders...)
test.Assert(t, result, err, anyTypeHolderSelected)
Expand All @@ -312,7 +357,11 @@ func assertDiscriminatorCases(t *testing.T, tests []UnionTypeCase, caller func([
var anyTypeHolderSelected bool
var typeHolders []*TypeHolder
for i, tt := range test.types {
typeHolders = append(typeHolders, NewTypeHolderDiscriminator(tt, &anyTypeHolderSelected, test.discriminators[i]))
var isNullableType bool = false
if len(test.isNullableTypes) > 0 {
isNullableType = test.isNullableTypes[i]
}
typeHolders = append(typeHolders, NewTypeHolderDiscriminator(tt, isNullableType, &anyTypeHolderSelected, test.discriminators[i]))
}
result, err := caller([]byte(test.testValue), test.discriminatorField, typeHolders...)
test.Assert(t, result, err, anyTypeHolderSelected)
Expand Down
Loading