Skip to content

Commit

Permalink
tftypes: Return null Number when NewValue receives (*big.Float)(nil) (#…
Browse files Browse the repository at this point in the history
…114)

Reference: hashicorp/terraform-plugin-framework#89

This matches the consistency of other pointer types in the `valueFromNumber` function and can prevent unexpected panics.
  • Loading branch information
bflad authored Oct 5, 2021
1 parent 990b525 commit fee1e53
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .changelog/pending.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
```release-note:bug
tftypes: Return null `Number` when `NewValue` receives `(*big.Float)(nil)`
```
6 changes: 6 additions & 0 deletions tftypes/primitive.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,12 @@ func valueFromBool(in interface{}) (Value, error) {
func valueFromNumber(in interface{}) (Value, error) {
switch value := in.(type) {
case *big.Float:
if value == nil {
return Value{
typ: Number,
value: nil,
}, nil
}
return Value{
typ: Number,
value: value,
Expand Down
4 changes: 4 additions & 0 deletions tftypes/value_number_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ func TestNewValue_number(t *testing.T) {
expected: Value{typ: Number, value: nil},
result: NewValue(Number, nil),
},
"*big.Float-nil": {
expected: Value{typ: Number, value: nil},
result: NewValue(Number, (*big.Float)(nil)),
},
"*big.Float": {
expected: Value{typ: Number, value: big.NewFloat(123)},
result: NewValue(Number, big.NewFloat(123)),
Expand Down

0 comments on commit fee1e53

Please sign in to comment.