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

When using (*JsonParseNode).GetXValue methods it panics fix #143

Merged
merged 12 commits into from
Aug 14, 2024
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"go.lintTool": "golangci-lint",
"go.formatFlags": ["-s", "-w"],
"go.lintOnSave": "package",
"go.lintFlags": ["-c", "~/.dotfiles/.golangci.yml", "--issues-exit-code=0"],
"editor.formatOnSave": true,
"editor.formatOnPaste": true,
"go.coverOnSave": true,
"go.coverageOptions": "showBothCoveredAndUncoveredCode",
"go.testOnSave": true
}
9 changes: 4 additions & 5 deletions intersection_type_wrapper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,25 +31,24 @@ func TestItParsesIntersectionTypeComplexProperty1(t *testing.T) {
}

func TestItParsesIntersectionTypeComplexProperty2(t *testing.T) {
source := "{\"displayName\":\"McGill\",\"officeLocation\":\"Montreal\", \"id\": 10}"
source := "{\"displayName\":\"McGill\",\"officeLocation\":\"Montreal\", \"id\": \"10\"}"
sourceArray := []byte(source)
parseNode, err := NewJsonParseNode(sourceArray)

if err != nil {
t.Error(err)
}
result, err := parseNode.GetObjectValue(internal.CreateIntersectionTypeMockFromDiscriminator)
if err != nil {
t.Error(err)
}
assert.Nil(t, err)
assert.NotNil(t, result)
cast, ok := result.(internal.IntersectionTypeMockable)
assert.NotNil(t, cast)
assert.True(t, ok)
assert.NotNil(t, cast.GetComposedType1())
assert.NotNil(t, cast.GetComposedType2())
assert.Nil(t, cast.GetStringValue())
assert.Nil(t, cast.GetComposedType3())
assert.Nil(t, cast.GetComposedType1().GetId())
assert.NotNil(t, cast.GetComposedType1().GetId())
assert.Nil(t, cast.GetComposedType2().GetId()) // it's expected to be null since we have conflicting properties here and the parser will only try one to avoid having to brute its way through
assert.Equal(t, "McGill", *cast.GetComposedType2().GetDisplayName())
}
Expand Down
150 changes: 102 additions & 48 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
if err != nil {
return nil, err
}
switch token.(type) {
switch val := token.(type) {
case json.Delim:
switch token.(json.Delim) {
case '{':
Expand Down Expand Up @@ -99,7 +99,7 @@ func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
}
return c, nil
case string:
v := token.(string)
v := val
c := &JsonParseNode{}
c.setValue(&v)
return c, nil
Expand All @@ -113,12 +113,12 @@ func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
v := token.(int8)
c.setValue(&v)
return c, nil
case byte:
case int32:
c := &JsonParseNode{}
v := token.(byte)
c.setValue(&v)
return c, nil
case float64:
case int64:
c := &JsonParseNode{}
v := token.(float64)
c.setValue(&v)
Expand All @@ -128,12 +128,12 @@ func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
v := token.(float32)
c.setValue(&v)
return c, nil
case int32:
case float64:
c := &JsonParseNode{}
v := token.(int32)
c.setValue(&v)
return c, nil
case int64:
case byte:
c := &JsonParseNode{}
v := token.(int64)
c.setValue(&v)
Expand All @@ -158,6 +158,9 @@ func (n *JsonParseNode) setValue(value interface{}) {

// GetChildNode returns a new parse node for the given identifier.
func (n *JsonParseNode) GetChildNode(index string) (absser.ParseNode, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if index == "" {
return nil, errors.New("index is empty")
}
Expand All @@ -183,12 +186,12 @@ func (n *JsonParseNode) GetChildNode(index string) (absser.ParseNode, error) {

// GetObjectValue returns the Parsable value from the node.
func (n *JsonParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Parsable, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if ctor == nil {
return nil, errors.New("constructor is nil")
}
if n == nil || n.value == nil {
return nil, nil
}
result, err := ctor(n)
if err != nil {
return nil, err
Expand Down Expand Up @@ -297,7 +300,7 @@ func (n *JsonParseNode) GetObjectValue(ctor absser.ParsableFactory) (absser.Pars

// GetCollectionOfObjectValues returns the collection of Parsable values from the node.
func (n *JsonParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory) ([]absser.Parsable, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if ctor == nil {
Expand All @@ -324,7 +327,7 @@ func (n *JsonParseNode) GetCollectionOfObjectValues(ctor absser.ParsableFactory)

// GetCollectionOfPrimitiveValues returns the collection of primitive values from the node.
func (n *JsonParseNode) GetCollectionOfPrimitiveValues(targetType string) ([]interface{}, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if targetType == "" {
Expand All @@ -348,7 +351,11 @@ func (n *JsonParseNode) GetCollectionOfPrimitiveValues(targetType string) ([]int
}
return result, nil
}

func (n *JsonParseNode) getPrimitiveValue(targetType string) (interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
switch targetType {
case "string":
return n.GetStringValue()
Expand Down Expand Up @@ -385,7 +392,7 @@ func (n *JsonParseNode) getPrimitiveValue(targetType string) (interface{}, error

// GetCollectionOfEnumValues returns the collection of Enum values from the node.
func (n *JsonParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]interface{}, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if parser == nil {
Expand All @@ -412,90 +419,119 @@ func (n *JsonParseNode) GetCollectionOfEnumValues(parser absser.EnumFactory) ([]

// GetStringValue returns a String value from the nodes.
func (n *JsonParseNode) GetStringValue() (*string, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
res, ok := n.value.(*string)
if ok {
return res, nil
} else {
return nil, nil

val, ok := n.value.(*string)
if !ok {
return nil, fmt.Errorf("type '%T' is not compatible with type string", n.value)
}
return val, nil
}

// GetBoolValue returns a Bool value from the nodes.
func (n *JsonParseNode) GetBoolValue() (*bool, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
return n.value.(*bool), nil

val, ok := n.value.(*bool)
if !ok {
return nil, fmt.Errorf("type '%T' is not compatible with type bool", n.value)
}
return val, nil
}

// GetInt8Value returns a int8 value from the nodes.
func (n *JsonParseNode) GetInt8Value() (*int8, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
return n.value.(*int8), nil
var val int8

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetBoolValue returns a Bool value from the nodes.
func (n *JsonParseNode) GetByteValue() (*byte, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
return n.value.(*byte), nil
var val byte

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetFloat32Value returns a Float32 value from the nodes.
func (n *JsonParseNode) GetFloat32Value() (*float32, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
cast := float32(*v)
return &cast, nil
var val float32

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetFloat64Value returns a Float64 value from the nodes.
func (n *JsonParseNode) GetFloat64Value() (*float64, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
return n.value.(*float64), nil
var val float64

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetInt32Value returns a Int32 value from the nodes.
func (n *JsonParseNode) GetInt32Value() (*int32, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
cast := int32(*v)
return &cast, nil
var val int32

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetInt64Value returns a Int64 value from the nodes.
func (n *JsonParseNode) GetInt64Value() (*int64, error) {
v, err := n.GetFloat64Value()
if err != nil {
return nil, err
}
if v == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
cast := int64(*v)
return &cast, nil
var val int64

if err := as(n.value, &val); err != nil {
return nil, err
}

return &val, nil
}

// GetTimeValue returns a Time value from the nodes.
func (n *JsonParseNode) GetTimeValue() (*time.Time, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -514,6 +550,9 @@ func (n *JsonParseNode) GetTimeValue() (*time.Time, error) {

// GetISODurationValue returns a ISODuration value from the nodes.
func (n *JsonParseNode) GetISODurationValue() (*absser.ISODuration, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -526,6 +565,9 @@ func (n *JsonParseNode) GetISODurationValue() (*absser.ISODuration, error) {

// GetTimeOnlyValue returns a TimeOnly value from the nodes.
func (n *JsonParseNode) GetTimeOnlyValue() (*absser.TimeOnly, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -538,6 +580,9 @@ func (n *JsonParseNode) GetTimeOnlyValue() (*absser.TimeOnly, error) {

// GetDateOnlyValue returns a DateOnly value from the nodes.
func (n *JsonParseNode) GetDateOnlyValue() (*absser.DateOnly, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -550,6 +595,9 @@ func (n *JsonParseNode) GetDateOnlyValue() (*absser.DateOnly, error) {

// GetUUIDValue returns a UUID value from the nodes.
func (n *JsonParseNode) GetUUIDValue() (*uuid.UUID, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
v, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -563,6 +611,9 @@ func (n *JsonParseNode) GetUUIDValue() (*uuid.UUID, error) {

// GetEnumValue returns a Enum value from the nodes.
func (n *JsonParseNode) GetEnumValue(parser absser.EnumFactory) (interface{}, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
if parser == nil {
return nil, errors.New("parser is nil")
}
Expand All @@ -578,6 +629,9 @@ func (n *JsonParseNode) GetEnumValue(parser absser.EnumFactory) (interface{}, er

// GetByteArrayValue returns a ByteArray value from the nodes.
func (n *JsonParseNode) GetByteArrayValue() ([]byte, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
s, err := n.GetStringValue()
if err != nil {
return nil, err
Expand All @@ -590,7 +644,7 @@ func (n *JsonParseNode) GetByteArrayValue() ([]byte, error) {

// GetRawValue returns a ByteArray value from the nodes.
func (n *JsonParseNode) GetRawValue() (interface{}, error) {
if n == nil || n.value == nil {
if isNil(n) || isNil(n.value) {
return nil, nil
}
switch v := n.value.(type) {
Expand Down
Loading
Loading