Skip to content

Commit 62577ff

Browse files
author
Paddy Carver
committed
Rename our UnmarshalTerraform5Type interface.
The names didn't make a whole lot of sense. "Unmarshal" evokes JSON or other encoding schemes, which isn't really what we're doing here. Also, we're not working with Types, we're working wtih Values. By using FromTerraform5Value, we can mirror it with ToTerraform5Value, and the name's are clearer about what the results will be and their usage.
1 parent e7bf149 commit 62577ff

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

tfprotov5/tftypes/value.go

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,12 @@ import (
88
"github.com/vmihailenco/msgpack"
99
)
1010

11-
type Unmarshaler interface {
12-
UnmarshalTerraform5Type(Value) error
11+
type ValueConverter interface {
12+
FromTerraform5Value(Value) error
13+
}
14+
15+
type ValueCreator interface {
16+
ToTerraform5Value() (Value, error)
1317
}
1418

1519
type ErrUnhandledType string
@@ -26,8 +30,6 @@ func (u msgPackUnknownType) MarshalMsgpack() ([]byte, error) {
2630
return []byte{0xd4, 0, 0}, nil
2731
}
2832

29-
// Value represents a form of a Terraform type that can be parsed into a Go
30-
// type.
3133
type Value struct {
3234
typ Type
3335
value interface{}
@@ -41,9 +43,9 @@ func NewValue(t Type, val interface{}) Value {
4143
}
4244

4345
func (val Value) As(dst interface{}) error {
44-
unmarshaler, ok := dst.(Unmarshaler)
46+
unmarshaler, ok := dst.(ValueConverter)
4547
if ok {
46-
return unmarshaler.UnmarshalTerraform5Type(val)
48+
return unmarshaler.FromTerraform5Value(val)
4749
}
4850
if !val.IsKnown() {
4951
return fmt.Errorf("unmarshaling unknown values is not supported")
@@ -135,7 +137,7 @@ func (val Value) As(dst interface{}) error {
135137
}
136138
return val.As(*target)
137139
}
138-
return fmt.Errorf("can't unmarshal into %T, needs UnmarshalTerraform5Type method", dst)
140+
return fmt.Errorf("can't unmarshal into %T, needs FromTerraform5Value method", dst)
139141
}
140142

141143
func (val Value) Is(t Type) bool {

tfprotov5/tftypes/value_example_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,9 +63,9 @@ type exampleResource struct {
6363
suppliedName *bool // true for yes, false for no, nil for we haven't asked
6464
}
6565

66-
// fill the tftypes.Unmarshaler interface to control how As works
66+
// fill the tftypes.ValueConverter interface to control how As works
6767
// we want a pointer to exampleResource so we can change the properties
68-
func (e *exampleResource) UnmarshalTerraform5Type(val tftypes.Value) error {
68+
func (e *exampleResource) FromTerraform5Value(val tftypes.Value) error {
6969
// this is an object type, so we're always going to get a
7070
// `tftypes.Value` that coerces to a map[string]tftypes.Value
7171
// as input
@@ -105,10 +105,10 @@ func ExampleValue_As_interface() {
105105
"supplied_name": tftypes.NewValue(tftypes.Bool, nil),
106106
})
107107

108-
// exampleResource has UnmarshalTerraform5Type method defined on it,
109-
// see value_example_test.go for implementation details
110-
// we'd put the function and type inline here, but apparently Go can't
111-
// have methods defined on types defined inside a function
108+
// exampleResource has FromTerraform5Value method defined on it, see
109+
// value_example_test.go for implementation details. We'd put the
110+
// function and type inline here, but apparently Go can't have methods
111+
// defined on types defined inside a function
112112
var res exampleResource
113113

114114
// call As as usual

0 commit comments

Comments
 (0)