Skip to content

Commit

Permalink
Removing lossy conversion to big.Float (#189)
Browse files Browse the repository at this point in the history
  • Loading branch information
bendbennett committed May 11, 2022
1 parent 8734625 commit bcf9b6f
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
2 changes: 1 addition & 1 deletion tftypes/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -440,7 +440,7 @@ func (val Value) As(dst interface{}) error {
if !ok {
return fmt.Errorf("can't unmarshal %s into %T, expected *big.Float", val.Type(), dst)
}
target.Set(v)
target.Copy(v)
return nil
case **big.Float:
if val.IsNull() {
Expand Down
6 changes: 3 additions & 3 deletions tftypes/value_msgpack.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,21 @@ func msgpackUnmarshal(dec *msgpack.Decoder, typ Type, path *AttributePath) (Valu
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
}
switch peek {
case msgpackCodes.Int8, msgpackCodes.Int16, msgpackCodes.Int32, msgpackCodes.Int64:
rv, err := dec.DecodeInt64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as int64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetInt64(rv)), nil
case msgpackCodes.Uint8, msgpackCodes.Uint16, msgpackCodes.Uint32, msgpackCodes.Uint64:
rv, err := dec.DecodeUint64()
if err != nil {
return Value{}, path.NewErrorf("couldn't decode number as uint64: %w", err)
}
return NewValue(Number, big.NewFloat(float64(rv))), nil
return NewValue(Number, new(big.Float).SetUint64(rv)), nil
case msgpackCodes.Float, msgpackCodes.Double:
rv, err := dec.DecodeFloat64()
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions tftypes/value_msgpack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,26 @@ func TestValueFromMsgPack(t *testing.T) {
value: NewValue(Number, big.NewFloat(1)),
typ: Number,
},
"int64-number": {
hex: "cf7fffffffffffffff",
value: NewValue(Number, new(big.Float).SetInt64(math.MaxInt64)),
typ: Number,
},
"uint64-number": {
hex: "b43138343436373434303733373039353531363135",
value: NewValue(Number, new(big.Float).SetUint64(math.MaxUint64)),
typ: Number,
},
"float-number": {
hex: "cb3ff8000000000000",
value: NewValue(Number, big.NewFloat(1.5)),
typ: Number,
},
"float64-number": {
hex: "cb7fefffffffffffff",
value: NewValue(Number, new(big.Float).SetFloat64(math.MaxFloat64)),
typ: Number,
},
"big-number": {
hex: "d96439393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939393939",
value: NewValue(Number, bigNumber),
Expand Down

0 comments on commit bcf9b6f

Please sign in to comment.