-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgen_copy_from.go
304 lines (262 loc) · 9.68 KB
/
gen_copy_from.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
package main
import (
"io"
j "github.com/dave/jennifer/jen"
)
// MessageCopyFromGenerator is the visitor struct to generate tfsdk.Schema of a message
type MessageCopyFromGenerator struct {
*Message
i *Imports
}
// FieldCopyFromGenerator is a visitor for a field
type FieldCopyFromGenerator struct {
*Field
i *Imports
}
// NewMessageCopyFromGenerator returns new MessageCopyFromGenerator struct
func NewMessageCopyFromGenerator(m *Message, i *Imports) *MessageCopyFromGenerator {
return &MessageCopyFromGenerator{m, i}
}
// NewFieldCopyFromGenerator returns new FieldCopyFromGenerator struct
func NewFieldCopyFromGenerator(f *Field, i *Imports) *FieldCopyFromGenerator {
return &FieldCopyFromGenerator{f, i}
}
// Generate generates Copy<Name>FromTerraform method
func (m *MessageCopyFromGenerator) Generate(writer io.Writer) (int, error) {
methodName := "Copy" + m.Name + "FromTerraform"
tf := j.Id("tf").Id(m.i.WithPackage(Types, "Object"))
obj := j.Id("obj").Op("*").Id(m.i.WithType(m.GoType))
diags := j.Var().Id("diags").Id(m.i.WithPackage(Diag, "Diagnostics"))
ctx := j.Id("_").Id(m.i.WithPackage("context", "Context"))
// func Copy<name>FromTerraform(_ context.Context, tf types.Object, obj *apitypes.<name>) diag.Diagnostics
// ... statements for a fields
method :=
j.Commentf("// %v copies contents of the source Terraform object into a target struct\n", methodName).
Func().Id(methodName).
Params(ctx, tf, obj).
Id(m.i.WithPackage(Diag, "Diagnostics")).
BlockFunc(func(g *j.Group) {
g.Add(diags)
m.GenerateFields(g)
g.Return(j.Id("diags"))
})
return writer.Write([]byte(method.GoString() + "\n"))
}
// GenerateFields generates specific statements for CopyToTF<name> methods
func (m *MessageCopyFromGenerator) GenerateFields(g *j.Group) {
// Reset all oneOf fields in advance, otherwise if all oneOf branches would be null in the passed
// object, the oneOf field won't be nil
for _, m := range m.OneOfNames {
g.Add(j.Id("obj." + m).Op("=").Nil())
}
for _, f := range m.Fields {
g.Add(NewFieldCopyFromGenerator(f, m.i).Generate())
}
}
// Generate generates CopyFrom fragment for a field of different kind
func (f *FieldCopyFromGenerator) Generate() *j.Statement {
switch f.Kind {
case PrimitiveKind:
return f.genPrimitive()
case ObjectKind:
return f.genObject()
case PrimitiveListKind, PrimitiveMapKind:
return f.genPrimitiveListOrMap()
case ObjectListKind, ObjectMapKind:
return f.genObjectListOrMap()
case CustomKind:
return f.genCustom()
}
return nil
}
// errMissingDiag diags.Append(attrMissingDiag{path})
func (f *FieldCopyFromGenerator) errAttrMissingDiag(g *j.Group) {
g.Id("diags.Append").Call(
j.Id("attrReadMissingDiag").Values(j.Lit(f.Path)),
)
}
// errAttrConversionFailure diags.Append(attrConversionFailureDiag{path, typ})
func (f *FieldCopyFromGenerator) errAttrConversionFailure(path string, typ string) func(g *j.Group) {
return func(g *j.Group) {
g.Id("diags.Append").Call(
j.Id("attrReadConversionFailureDiag").Values(j.Lit(path), j.Lit(typ)),
)
}
}
// nextField reads current field value from Terraform object and asserts it's type against expected
func (f *FieldCopyFromGenerator) nextField(g func(g *j.Group)) *j.Statement {
return j.Block(
// a, ok := ft.Attrs["key"]
j.List(j.Id("a"), j.Id("ok")).Op(":=").Id("tf.Attrs").Index(j.Lit(f.NameSnake)),
j.If(j.Id("!ok")).BlockFunc(f.errAttrMissingDiag).Else().Block(
// v, ok := a.(types.Int64)
j.List(j.Id("v"), j.Id("ok")).Op(":=").Id("a").Assert(j.Id(f.i.WithType(f.ValueType))),
j.If(j.Id("!ok")).BlockFunc(
f.errAttrConversionFailure(f.Path, f.ValueType),
).Else().BlockFunc(g),
),
)
}
// genPrimitiveBody generates fragment which converts attr.Value v to go variable t
func (f *FieldCopyFromGenerator) genPrimitiveBody(g *j.Group) {
// var t float32 || *float32, acts as zero value if needed
g.Var().Id("t").Id(f.i.WithType(f.GoElemType))
// if !v.Null {
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
if !f.IsNullable {
// obj.Float = float32(v.Value)
g.Id("t").Op("=").Id(f.i.WithType(f.ValueCastFromType)).Parens(j.Id("v.Value"))
} else {
// c := float32(v.Value)
g.Id("c").Op(":=").Id(f.i.WithType(f.ValueCastFromType)).Parens(j.Id("v.Value"))
// obj.Float = &c
g.Id("t").Op("=&").Id("c")
}
})
}
// genListOrMapBody generates base iterator statement, which iterates over a list or map
func (f *FieldCopyFromGenerator) genListOrMapIterator(g *j.Group, typ *j.Statement, els func(g *j.Group)) {
objFieldName := "obj." + f.Name
// obj.List = make([]string, len(v.Elems)) - same for maps
g.Id(objFieldName).Op("=").Make(j.Id(f.i.WithType(f.GoType)), j.Len(j.Id("v.Elems")))
// if !v.Null
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
// for k, el := range v.Elems - where k is either index or map key
g.For(j.List(j.Id("k"), j.Id("a"))).Op(":=").Range().Id("v.Elems").BlockFunc(func(g *j.Group) {
// v, ok := a.(types.String)
g.List(j.Id("v"), j.Id("ok")).Op(":=").Id("a").Assert(typ)
g.If(j.Id("!ok")).BlockFunc(
f.errAttrConversionFailure(f.Path, typ.GoString()),
).Else().BlockFunc(els)
})
})
}
// genPrimitive generates CopyFrom fragment for a primitive field, wrapped by oneOf extraction
func (f *FieldCopyFromGenerator) genPrimitive() *j.Statement {
return f.nextField(func(g *j.Group) {
f.genPrimitiveBody(g)
if f.OneOfName != "" {
// Do not set empty oneOf value to not override values possibly set by other branches
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
g.Id("obj." + f.OneOfName).Op("=").Id("&" + f.i.WithType(f.OneOfType)).Values(j.Dict{
j.Id(f.Name): j.Id("t"),
})
})
return
}
if f.ParentIsOptionalEmbed {
// If the current value is Null or Unknown, we should not set the parent field, otherwise we will get the default values for all the inner fields.
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
g.If(j.Id("obj." + f.ParentIsOptionalEmbedFieldName).Op("==").Nil()).Block(
j.Id("obj." + f.ParentIsOptionalEmbedFieldName).Op("=").Id("&" + f.ParentIsOptionalEmbedFullType + "{}"),
)
g.Id("obj." + f.Name).Op("=").Id("t")
})
return
}
g.Id("obj." + f.Name).Op("=").Id("t")
})
}
// genObject generates CopyFrom fragment for a nested object
func (f *FieldCopyFromGenerator) genObject() *j.Statement {
m := NewMessageCopyFromGenerator(f.Message, f.i)
objFieldName := "obj." + f.Name
return f.nextField(func(g *j.Group) {
if f.OneOfName == "" {
if f.IsNullable {
// obj.Nested = nil
g.Id(objFieldName).Op("=").Nil()
} else {
// obj.Nested = Nested{}
g.Id(objFieldName).Op("=").Id(f.i.WithType(f.GoElemType)).Values()
}
// if !v.Null
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
if !m.IsEmpty {
// tf := v
g.Id("tf").Op(":=").Id("v")
if f.IsNullable {
// obj.Nested = &Nested{}
g.Id(objFieldName).Op("=&").Id(f.i.WithType(f.GoElemTypeIndirect)).Values()
// obj := obj.Nested
g.Id("obj").Op(":=").Id(objFieldName)
} else {
// obj := &obj.Nested
g.Id("obj").Op(":=&").Id(objFieldName)
}
m.GenerateFields(g)
}
})
} else {
// We do not need nullable checks because all oneOf branches are nullable by design
// We do not need to assign OneOf explicitly to not overrite other OneOf branch values
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
g.Id("b").Op(":=&").Id(f.i.WithType(f.GoElemTypeIndirect)).Values()
g.Id("obj." + f.OneOfName).Op("=").Id("&" + f.i.WithType(f.OneOfType)).Values(j.Dict{
j.Id(f.Name): j.Id("b"),
})
if !m.IsEmpty {
g.Id("obj").Op(":=").Id("b")
g.Id("tf").Op(":=").Id("v")
m.GenerateFields(g)
}
})
}
})
}
// getValueField returns list/map value field
func (f *FieldCopyFromGenerator) getValueField() *Field {
if f.IsMap {
return f.MapValueField
}
return f.Field
}
// genPrimitiveListOrMap generates CopyFrom fragment for a list or a map of a primitive values
func (f *FieldCopyFromGenerator) genPrimitiveListOrMap() *j.Statement {
objFieldName := "obj." + f.Name
field := f.getValueField()
return f.nextField(func(g *j.Group) {
f.genListOrMapIterator(g, j.Id(f.i.WithType(field.ElemValueType)), func(g *j.Group) {
f.genPrimitiveBody(g)
// obj.List[k] = t
g.Id(objFieldName).Index(j.Id("k")).Op("=").Id("t")
})
})
}
// genNestedListOrMap generates CopyFrom fragment for a list or a map of a nested objects
func (f *FieldCopyFromGenerator) genObjectListOrMap() *j.Statement {
objFieldName := "obj." + f.Name
field := f.getValueField()
m := NewMessageCopyFromGenerator(field.Message, f.i)
return f.nextField(func(g *j.Group) {
f.genListOrMapIterator(g, j.Id(f.i.WithType(field.ElemValueType)), func(g *j.Group) {
// var t Nested || *Nested
g.Var().Id("t").Id(f.i.WithType(f.GoElemType))
g.If(j.Id("!v.Null && !v.Unknown")).BlockFunc(func(g *j.Group) {
// tf := v
g.Id("tf").Op(":=").Id("v")
if f.IsNullable {
// t = &Nested{}
g.Id("t").Op("=&").Id(f.i.WithType(f.GoElemTypeIndirect)).Values()
// obj := t - obj is just an alias to reuse field generator code
g.Id("obj").Op(":=").Id("t")
} else {
// obj := &t
g.Id("obj").Op(":=&").Id("t")
}
m.GenerateFields(g)
})
// obj.List[k] = t
g.Id(objFieldName).Index(j.Id("k")).Op("=").Id("t")
})
})
}
// genCustom generates statement representing custom type
func (f *FieldCopyFromGenerator) genCustom() *j.Statement {
return j.Block(
// a, ok := ft.Attrs["key"]
j.List(j.Id("a"), j.Id("ok")).Op(":=").Id("tf.Attrs").Index(j.Lit(f.NameSnake)),
j.If(j.Id("!ok")).BlockFunc(f.errAttrMissingDiag),
j.Id("CopyFrom"+f.Suffix).Params(j.Id("diags"), j.Id("a"), j.Id("&obj."+f.Name)),
)
}