Skip to content

Commit f20d81a

Browse files
author
Paddy Carver
committed
Fix bug in msgpack invalid value error.
Our invalid value type error when marshaling to msgpack is supposed to tell people what type they passed in. Unfortunately, we're telling them the type of the Value they passed in, which is always tftypes.Value, not the *contents* of that Value, which is the actual type they should know about. This leads to confusing error messages that aren't actually helpful. Switching to pass in the content of the tftypes.Value gives a much better error message.
1 parent 95da7fb commit f20d81a

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

tfprotov5/tftypes/value_msgpack.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func marshalMsgPack(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
5151
func marshalMsgPackDynamicPseudoType(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
5252
dst, ok := val.value.(Value)
5353
if !ok {
54-
return unexpectedValueTypeError(p, Value{}, val, typ)
54+
return unexpectedValueTypeError(p, Value{}, val.value, typ)
5555
}
5656
typeJSON, err := dst.typ.MarshalJSON()
5757
if err != nil {
@@ -75,7 +75,7 @@ func marshalMsgPackDynamicPseudoType(val Value, typ Type, p Path, enc *msgpack.E
7575
func marshalMsgPackString(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
7676
s, ok := val.value.(string)
7777
if !ok {
78-
return unexpectedValueTypeError(p, s, val, typ)
78+
return unexpectedValueTypeError(p, s, val.value, typ)
7979
}
8080
err := enc.EncodeString(s)
8181
if err != nil {
@@ -93,7 +93,7 @@ func marshalMsgPackNumber(val Value, typ Type, p Path, enc *msgpack.Encoder) err
9393
// advantage of that...
9494
n, ok := val.value.(*big.Float)
9595
if !ok {
96-
return unexpectedValueTypeError(p, n, val, typ)
96+
return unexpectedValueTypeError(p, n, val.value, typ)
9797
}
9898
if iv, acc := n.Int64(); acc == big.Exact {
9999
err := enc.EncodeInt(iv)
@@ -117,7 +117,7 @@ func marshalMsgPackNumber(val Value, typ Type, p Path, enc *msgpack.Encoder) err
117117
func marshalMsgPackBool(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
118118
b, ok := val.value.(bool)
119119
if !ok {
120-
return unexpectedValueTypeError(p, b, val, typ)
120+
return unexpectedValueTypeError(p, b, val.value, typ)
121121
}
122122
err := enc.EncodeBool(b)
123123
if err != nil {
@@ -129,7 +129,7 @@ func marshalMsgPackBool(val Value, typ Type, p Path, enc *msgpack.Encoder) error
129129
func marshalMsgPackList(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
130130
l, ok := val.value.([]Value)
131131
if !ok {
132-
return unexpectedValueTypeError(p, l, val, typ)
132+
return unexpectedValueTypeError(p, l, val.value, typ)
133133
}
134134
err := enc.EncodeArrayLen(len(l))
135135
if err != nil {
@@ -149,7 +149,7 @@ func marshalMsgPackList(val Value, typ Type, p Path, enc *msgpack.Encoder) error
149149
func marshalMsgPackSet(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
150150
s, ok := val.value.([]Value)
151151
if !ok {
152-
return unexpectedValueTypeError(p, s, val, typ)
152+
return unexpectedValueTypeError(p, s, val.value, typ)
153153
}
154154
err := enc.EncodeArrayLen(len(s))
155155
if err != nil {
@@ -169,7 +169,7 @@ func marshalMsgPackSet(val Value, typ Type, p Path, enc *msgpack.Encoder) error
169169
func marshalMsgPackMap(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
170170
m, ok := val.value.(map[string]Value)
171171
if !ok {
172-
return unexpectedValueTypeError(p, m, val, typ)
172+
return unexpectedValueTypeError(p, m, val.value, typ)
173173
}
174174
err := enc.EncodeMapLen(len(m))
175175
if err != nil {
@@ -193,7 +193,7 @@ func marshalMsgPackMap(val Value, typ Type, p Path, enc *msgpack.Encoder) error
193193
func marshalMsgPackTuple(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
194194
t, ok := val.value.([]Value)
195195
if !ok {
196-
return unexpectedValueTypeError(p, t, val, typ)
196+
return unexpectedValueTypeError(p, t, val.value, typ)
197197
}
198198
types := typ.(Tuple).ElementTypes
199199
err := enc.EncodeArrayLen(len(types))
@@ -215,7 +215,7 @@ func marshalMsgPackTuple(val Value, typ Type, p Path, enc *msgpack.Encoder) erro
215215
func marshalMsgPackObject(val Value, typ Type, p Path, enc *msgpack.Encoder) error {
216216
o, ok := val.value.(map[string]Value)
217217
if !ok {
218-
return unexpectedValueTypeError(p, o, val, typ)
218+
return unexpectedValueTypeError(p, o, val.value, typ)
219219
}
220220
types := typ.(Object).AttributeTypes
221221
keys := make([]string, 0, len(types))

0 commit comments

Comments
 (0)