-
Notifications
You must be signed in to change notification settings - Fork 91
/
struct.go
224 lines (198 loc) · 7.47 KB
/
struct.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
package reflect
import (
"context"
"fmt"
"reflect"
"strings"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
// Struct builds a new struct using the data in `object`, as long as `object`
// is a `tftypes.Object`. It will take the struct type from `target`, which
// must be a struct type.
//
// The properties on `target` must be tagged with a "tfsdk" label containing
// the field name to map to that property. Every property must be tagged, and
// every property must be present in the type of `object`, and all the
// attributes in the type of `object` must have a corresponding property.
// Properties that don't map to object attributes must have a `tfsdk:"-"` tag,
// explicitly defining them as not part of the object. This is to catch typos
// and other mistakes early.
//
// Struct is meant to be called from Into, not directly.
func Struct(ctx context.Context, typ attr.Type, object tftypes.Value, target reflect.Value, opts Options, path *tftypes.AttributePath) (reflect.Value, diag.Diagnostics) {
var diags diag.Diagnostics
// this only works with object values, so make sure that constraint is
// met
if target.Kind() != reflect.Struct {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("expected a struct type, got %s", target.Type()),
}))
return target, diags
}
if !object.Type().Is(tftypes.Object{}) {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("cannot reflect %s into a struct, must be an object", object.Type().String()),
}))
return target, diags
}
attrsType, ok := typ.(attr.TypeWithAttributeTypes)
if !ok {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("cannot reflect object using type information provided by %T, %T must be an attr.TypeWithAttributeTypes", typ, typ),
}))
return target, diags
}
// collect a map of fields that are in the object passed in
var objectFields map[string]tftypes.Value
err := object.As(&objectFields)
if err != nil {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: err,
}))
return target, diags
}
// collect a map of fields that are defined in the tags of the struct
// passed in
targetFields, err := getStructTags(ctx, target, path)
if err != nil {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("error retrieving field names from struct tags: %w", err),
}))
return target, diags
}
// we require an exact, 1:1 match of these fields to avoid typos
// leading to surprises, so let's ensure they have the exact same
// fields defined
var objectMissing, targetMissing []string
for field := range targetFields {
if _, ok := objectFields[field]; !ok {
objectMissing = append(objectMissing, field)
}
}
for field := range objectFields {
if _, ok := targetFields[field]; !ok {
targetMissing = append(targetMissing, field)
}
}
if len(objectMissing) > 0 || len(targetMissing) > 0 {
var missing []string
if len(objectMissing) > 0 {
missing = append(missing, fmt.Sprintf("Struct defines fields not found in object: %s.", commaSeparatedString(objectMissing)))
}
if len(targetMissing) > 0 {
missing = append(missing, fmt.Sprintf("Object defines fields not found in struct: %s.", commaSeparatedString(targetMissing)))
}
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("mismatch between struct and object: %s", strings.Join(missing, " ")),
}))
return target, diags
}
attrTypes := attrsType.AttributeTypes()
// now that we know they match perfectly, fill the struct with the
// values in the object
result := reflect.New(target.Type()).Elem()
for field, structFieldPos := range targetFields {
attrType, ok := attrTypes[field]
if !ok {
diags.Append(diag.WithPath(path, DiagIntoIncompatibleType{
Val: object,
TargetType: target.Type(),
Err: fmt.Errorf("could not find type information for attribute in supplied attr.Type %T", typ),
}))
return target, diags
}
structField := result.Field(structFieldPos)
fieldVal, fieldValDiags := BuildValue(ctx, attrType, objectFields[field], structField, opts, path.WithAttributeName(field))
diags.Append(fieldValDiags...)
if diags.HasError() {
return target, diags
}
structField.Set(fieldVal)
}
return result, diags
}
// FromStruct builds an attr.Value as produced by `typ` from the data in `val`.
// `val` must be a struct type, and must have all its properties tagged and be
// a 1:1 match with the attributes reported by `typ`. FromStruct will recurse
// into FromValue for each attribute, using the type of the attribute as
// reported by `typ`.
//
// It is meant to be called through FromValue, not directly.
func FromStruct(ctx context.Context, typ attr.TypeWithAttributeTypes, val reflect.Value, path *tftypes.AttributePath) (attr.Value, diag.Diagnostics) {
var diags diag.Diagnostics
objTypes := map[string]tftypes.Type{}
objValues := map[string]tftypes.Value{}
// collect a map of fields that are defined in the tags of the struct
// passed in
targetFields, err := getStructTags(ctx, val, path)
if err != nil {
err = fmt.Errorf("error retrieving field names from struct tags: %w", err)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert from struct value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return nil, diags
}
attrTypes := typ.AttributeTypes()
for name, fieldNo := range targetFields {
path := path.WithAttributeName(name)
fieldValue := val.Field(fieldNo)
attrVal, attrValDiags := FromValue(ctx, attrTypes[name], fieldValue.Interface(), path)
diags.Append(attrValDiags...)
if diags.HasError() {
return nil, diags
}
attrType, ok := attrTypes[name]
if !ok || attrType == nil {
err := fmt.Errorf("couldn't find type information for attribute in supplied attr.Type %T", typ)
diags.AddAttributeError(
path,
"Value Conversion Error",
"An unexpected error was encountered trying to convert from struct value. This is always an error in the provider. Please report the following to the provider developer:\n\n"+err.Error(),
)
return nil, diags
}
objTypes[name] = attrType.TerraformType(ctx)
tfObjVal, err := attrVal.ToTerraformValue(ctx)
if err != nil {
return nil, append(diags, toTerraformValueErrorDiag(err, path))
}
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfObjVal, path)...)
if diags.HasError() {
return nil, diags
}
}
objValues[name] = tfObjVal
}
tfVal := tftypes.NewValue(tftypes.Object{
AttributeTypes: objTypes,
}, objValues)
if typeWithValidate, ok := typ.(attr.TypeWithValidate); ok {
diags.Append(typeWithValidate.Validate(ctx, tfVal, path)...)
if diags.HasError() {
return nil, diags
}
}
retType := typ.WithAttributeTypes(attrTypes)
ret, err := retType.ValueFromTerraform(ctx, tfVal)
if err != nil {
return nil, append(diags, valueFromTerraformErrorDiag(err, path))
}
return ret, diags
}