Skip to content

Commit 5e8aeb3

Browse files
author
Paddy
authored
Panic if an unsupported type is passed to NewValue. (#35)
Let's fail loudly if an unsupported type is passed to NewValue, so users don't get confusing errors that are hard to trace to their source. We can always make this more permissive later.
1 parent c6ea4dd commit 5e8aeb3

File tree

1 file changed

+22
-6
lines changed

1 file changed

+22
-6
lines changed

tfprotov5/tftypes/value.go

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,31 @@ type Value struct {
6464
//
6565
// * bool for Bool
6666
//
67-
// * map[string]Value or map[string]interface{} for Map and Object
67+
// * map[string]Value for Map and Object
6868
//
69-
// * []Value or []interface{} for Tuple, List, and Set
69+
// * []Value for Tuple, List, and Set
7070
func NewValue(t Type, val interface{}) Value {
71-
// TODO: handle ValueCreator implementations.
72-
return Value{
73-
typ: t,
74-
value: val,
71+
if val == nil || val == UnknownValue {
72+
return Value{
73+
typ: t,
74+
value: val,
75+
}
76+
}
77+
if creator, ok := val.(ValueCreator); ok {
78+
var err error
79+
val, err = creator.ToTerraform5Value()
80+
if err != nil {
81+
panic("error creating tftypes.Value: " + err.Error())
82+
}
83+
}
84+
switch val.(type) {
85+
case string, *big.Float, bool, map[string]Value, []Value:
86+
return Value{
87+
typ: t,
88+
value: val,
89+
}
7590
}
91+
panic(fmt.Sprintf("unknown type %T passed to NewValue", val))
7692
}
7793

7894
// As converts a Value into a Go value. `dst` must be set to a pointer to a

0 commit comments

Comments
 (0)