Skip to content

Commit

Permalink
Merge pull request #143 from michaeldcanady/main
Browse files Browse the repository at this point in the history
When using (*JsonParseNode).GetXValue methods it panics fix
  • Loading branch information
andrueastman authored Aug 14, 2024
2 parents 28b99ec + 5b6093f commit 332365e
Show file tree
Hide file tree
Showing 7 changed files with 1,004 additions and 51 deletions.
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
}
18 changes: 14 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [1.0.8] - 2024-08-13

### Changed

- Modified how number values are derived, allowing values to be cast as the various types.

### Fixed

- Panicing when type is asserted to be what it isn't.

## [1.0.7] - 2024-02-29

### Added
Expand Down Expand Up @@ -135,7 +145,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [0.5.5] - 2022-07-12

- Fixed bug where string literals of `\t` and `\r` would result in generating an invalid JSON.
- Fixed bug where string literals of `\t` and `\r` would result in generating an invalid JSON.

### Changed

Expand All @@ -161,14 +171,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Updated supported types for Additional Data, unsupported types now throwing an error instead of ignoring.
- Changed logic that trims excessive commas to be called only once on serialization.
- Updated supported types for Additional Data, unsupported types now throwing an error instead of ignoring.
- Changed logic that trims excessive commas to be called only once on serialization.

## [0.5.0] - 2022-05-26

### Changed

- Updated reference to abstractions to support enum responses.
- Updated reference to abstractions to support enum responses.

## [0.4.0] - 2022-05-19

Expand Down
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
139 changes: 97 additions & 42 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ func NewJsonParseNode(content []byte) (*JsonParseNode, error) {
value, err := loadJsonTree(decoder)
return value, err
}

func loadJsonTree(decoder *json.Decoder) (*JsonParseNode, error) {
for {
token, err := decoder.Token()
Expand Down Expand Up @@ -158,6 +159,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 +187,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 +301,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 +328,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 +352,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 +393,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 +420,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 +551,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 +566,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 +581,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 +596,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 +612,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 +630,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 +645,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

0 comments on commit 332365e

Please sign in to comment.