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

Don't include exponents on coerced strings #1371

Merged
merged 1 commit into from
Sep 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
30 changes: 29 additions & 1 deletion pf/internal/convert/convert_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)...)

Expand Down
3 changes: 2 additions & 1 deletion pf/internal/convert/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package convert

import (
"fmt"
"strconv"

"github.com/hashicorp/terraform-plugin-go/tftypes"

Expand Down Expand Up @@ -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
Expand Down
Loading