Skip to content

Commit

Permalink
efficiency concerns for more directly assert-able types
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael Canady committed Aug 12, 2024
1 parent 4932cd3 commit 3447cae
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 13 deletions.
18 changes: 8 additions & 10 deletions json_parse_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -417,27 +417,25 @@ func (n *JsonParseNode) GetStringValue() (*string, error) {
if isNil(n) || isNil(n.value) {
return nil, nil
}
var val string

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

return &val, nil
return val, nil
}

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

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

return &val, nil
return val, nil
}

// GetInt8Value returns a int8 value from the nodes.
Expand Down
6 changes: 3 additions & 3 deletions json_parse_node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ func TestJsonGetStringValue(t *testing.T) {
Title: "Integer",
Input: []byte(`1`),
Expected: (*string)(nil),
Error: errors.New("value '1' is not compatible with type string"),
Error: errors.New("type '*float64' is not compatible with type string"),
},
}

Expand Down Expand Up @@ -381,13 +381,13 @@ func TestJsonGetBoolValue(t *testing.T) {
Title: "Integer",
Input: []byte(`1`),
Expected: (*bool)(nil),
Error: errors.New("value '1' is not compatible with type bool"),
Error: errors.New("type '*float64' is not compatible with type bool"),
},
{
Title: "String",
Input: []byte(`"true"`),
Expected: (*bool)(nil),
Error: errors.New("value 'true' is not compatible with type bool"),
Error: errors.New("type '*string' is not compatible with type bool"),
},
}

Expand Down

0 comments on commit 3447cae

Please sign in to comment.