Skip to content

Commit 1a9a8d8

Browse files
author
Paddy Carver
committed
Add NewDynamicValue helper.
1 parent 826bf1f commit 1a9a8d8

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

tfprotov5/dynamic_value.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,26 @@ import (
2121
// terraform-plugin-go and is no longer a valid value.
2222
var ErrUnknownDynamicValueType = errors.New("DynamicValue had no JSON or msgpack data set")
2323

24+
// NewDynamicValue creates a DynamicValue from a tftypes.Value. You must
25+
// specify the tftype.Type you want to send the value as, and it must be a type
26+
// that is compatible with the Type of the Value. Usually it should just be the
27+
// Type of the Value, but it can also be the DynamicPseudoType.
28+
func NewDynamicValue(t tftypes.Type, v tftypes.Value) (DynamicValue, error) {
29+
b, err := v.MarshalMsgPack(t)
30+
if err != nil {
31+
return DynamicValue{}, err
32+
}
33+
return DynamicValue{
34+
MsgPack: b,
35+
}, nil
36+
}
37+
2438
// DynamicValue represents a nested encoding value that came from the protocol.
2539
// The only way providers should ever interact with it is by calling its
2640
// `Unmarshal` method to retrive a `tftypes.Value`. Although the type system
2741
// allows for other interactions, they are explicitly not supported, and will
2842
// not be considered when evaluating for breaking changes. Treat this type as
2943
// an opaque value, and *only* call its `Unmarshal` method.
30-
//
31-
// TODO: document how to create a DynamicValue.
3244
type DynamicValue struct {
3345
MsgPack []byte
3446
JSON []byte

0 commit comments

Comments
 (0)