From 91e29296c911bf392613d3de13032ea58553e05b Mon Sep 17 00:00:00 2001 From: Ian Wahbe Date: Fri, 15 Sep 2023 10:13:21 -0700 Subject: [PATCH] Don't include exponents on coerced strings --- pf/internal/convert/convert_test.go | 30 ++++++++++++++++++++++++++++- pf/internal/convert/string.go | 3 ++- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/pf/internal/convert/convert_test.go b/pf/internal/convert/convert_test.go index c72ec0ef75..d14faad1ac 100644 --- a/pf/internal/convert/convert_test.go +++ b/pf/internal/convert/convert_test.go @@ -49,7 +49,35 @@ func TestConvertTurnaround(t *testing.T) { panic(err) } return resource.NewNumberProperty(v) - }, "0", "8.3").withNormProp(func(p resource.PropertyValue) any { return fmt.Sprintf("%v", p.V) })...) + }, "0", "8.3", "601234567890").withNormProp(func(p resource.PropertyValue) any { + switch { + case p.IsOutput(): + v := p.OutputValue() + if !v.Known { + return "unknown" + } + p = v.Element + fallthrough + + // When converting back from a number, we specify that we have the same + // visual representation. + case p.IsNumber(): + switch p.NumberValue() { + case 0: + return "0" + case 8.3: + return "8.3" + case 601234567890: + return "601234567890" + default: + panic("unexpected test input") + } + case p.IsNull(): + return "" + default: + return p.StringValue() + } + })...) cases = append(cases, convertTurnaroundTestCases(tftypes.Bool, resource.NewBoolProperty, false, true)...) cases = append(cases, convertTurnaroundTestCases(tftypes.Number, resource.NewNumberProperty, float64(0), 42, 3.12)...) diff --git a/pf/internal/convert/string.go b/pf/internal/convert/string.go index 8f05c346c0..c00dc30be1 100644 --- a/pf/internal/convert/string.go +++ b/pf/internal/convert/string.go @@ -16,6 +16,7 @@ package convert import ( "fmt" + "strconv" "github.com/hashicorp/terraform-plugin-go/tftypes" @@ -74,7 +75,7 @@ func (*stringEncoder) fromPropertyValue(p resource.PropertyValue) (tftypes.Value case p.IsBool(): return tftypes.NewValue(tftypes.String, fmt.Sprintf("%v", p.BoolValue())), nil case p.IsNumber(): - return tftypes.NewValue(tftypes.String, fmt.Sprintf("%v", p.NumberValue())), nil + return tftypes.NewValue(tftypes.String, strconv.FormatFloat(p.NumberValue(), 'f', -1, 64)), nil case p.IsString(): return tftypes.NewValue(tftypes.String, p.StringValue()), nil