Skip to content

Commit

Permalink
Don't include exponents on coerced strings
Browse files Browse the repository at this point in the history
  • Loading branch information
iwahbe committed Sep 15, 2023
1 parent 9563cfe commit 91e2929
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
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

0 comments on commit 91e2929

Please sign in to comment.