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

Invalid plan arising from integer rounding #190

Merged
merged 4 commits into from
May 12, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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