Skip to content

Commit b9d1615

Browse files
committed
Also support floats in NewValue
1 parent 171ea52 commit b9d1615

File tree

2 files changed

+50
-1
lines changed

2 files changed

+50
-1
lines changed

tfprotov5/tftypes/value.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,8 @@ type Value struct {
6161
// * String: string, *string
6262
//
6363
// * Number: *big.Float, int64, *int64, int32, *int32, int16, *int16, int8, *int8, int, *int,
64-
// uint64, *uint64, uint32, *uint32, uint16, *uint16, uint8, *uint8, byte, *byte, uint, *uint
64+
// uint64, *uint64, uint32, *uint32, uint16, *uint16, uint8, *uint8, byte, *byte, uint, *uint,
65+
// float64, *float64, float32, *float32
6566
//
6667
// * Bool: bool, *bool
6768
//
@@ -226,6 +227,28 @@ func NewValue(t Type, val interface{}) Value {
226227
typ: t,
227228
value: f,
228229
}
230+
case *float32:
231+
if val == nil {
232+
return Value{
233+
typ: t,
234+
value: nil,
235+
}
236+
}
237+
return Value{
238+
typ: t,
239+
value: big.NewFloat(float64(*val)),
240+
}
241+
case *float64:
242+
if val == nil {
243+
return Value{
244+
typ: t,
245+
value: nil,
246+
}
247+
}
248+
return Value{
249+
typ: t,
250+
value: big.NewFloat(*val),
251+
}
229252
case string, *big.Float, bool, map[string]Value, []Value:
230253
return Value{
231254
typ: t,

tfprotov5/tftypes/value_test.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ func TestValueAs(t *testing.T) {
2424
numberPointerPointer := func(in *big.Float) **big.Float {
2525
return &in
2626
}
27+
float64Pointer := func(in float64) *float64 {
28+
return &in
29+
}
30+
float32Pointer := func(in float32) *float32 {
31+
return &in
32+
}
2733
int64Pointer := func(in int64) *int64 {
2834
return &in
2935
}
@@ -108,6 +114,26 @@ func TestValueAs(t *testing.T) {
108114
as: big.NewFloat(0),
109115
expected: big.NewFloat(123),
110116
},
117+
"number-pointer-float64": {
118+
in: NewValue(Number, float64Pointer(123.4)),
119+
as: big.NewFloat(0),
120+
expected: big.NewFloat(123.4),
121+
},
122+
"number-pointer-float64-null": {
123+
in: NewValue(Number, (*float64)(nil)),
124+
as: numberPointerPointer(big.NewFloat(123.4)),
125+
expected: numberPointerPointer(nil),
126+
},
127+
"number-pointer-float32": {
128+
in: NewValue(Number, float32Pointer(0.125)),
129+
as: big.NewFloat(0),
130+
expected: big.NewFloat(0.125),
131+
},
132+
"number-pointer-float32-null": {
133+
in: NewValue(Number, (*float32)(nil)),
134+
as: numberPointerPointer(big.NewFloat(0.125)),
135+
expected: numberPointerPointer(nil),
136+
},
111137
"number-pointer-int64": {
112138
in: NewValue(Number, int64Pointer(123)),
113139
as: big.NewFloat(0),

0 commit comments

Comments
 (0)