-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathgo_transform.go
492 lines (461 loc) Β· 19 KB
/
go_transform.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
package codegen
import (
"bytes"
"fmt"
"strings"
"text/template"
"goa.design/goa/expr"
)
var transformGoArrayT, transformGoMapT, transformGoArrayElemT, transformGoMapElemT *template.Template
// NOTE: can't initialize inline because https://github.com/golang/go/issues/1817
func init() {
fm := template.FuncMap{"transformAttribute": transformAttribute, "transformHelperName": transformHelperName}
transformGoArrayT = template.Must(template.New("transformGoArray").Funcs(fm).Parse(transformGoArrayTmpl))
transformGoMapT = template.Must(template.New("transformGoMap").Funcs(fm).Parse(transformGoMapTmpl))
transformGoArrayElemT = template.Must(template.New("transformGoArrayElem").Funcs(fm).Parse(transformGoArrayElemTmpl))
transformGoMapElemT = template.Must(template.New("transformGoMapElem").Funcs(fm).Parse(transformGoMapElemTmpl))
}
// GoTransform produces Go code that initializes the data structure defined
// by target from an instance of the data structure described by source.
// The data structures can be objects, arrays or maps. The algorithm
// matches object fields by name and ignores object fields in target that
// don't have a match in source. The matching and generated code leverage
// mapped attributes so that attribute names may use the "name:elem"
// syntax to define the name of the design attribute and the name of the
// corresponding generated Go struct field. The function returns an error
// if target is not compatible with source (different type, fields of
// different type etc).
//
// source and target are the attributes used in the transformation
//
// sourceVar and targetVar are the variable names used in the transformation
//
// sourceCtx and targetCtx are the attribute contexts for the source and target
// attributes
//
// prefix is the transformation helper function prefix
//
func GoTransform(source, target *expr.AttributeExpr, sourceVar, targetVar string, sourceCtx, targetCtx *AttributeContext, prefix string) (string, []*TransformFunctionData, error) {
ta := &TransformAttrs{
SourceCtx: sourceCtx,
TargetCtx: targetCtx,
Prefix: prefix,
}
code, err := transformAttribute(source, target, sourceVar, targetVar, true, ta)
if err != nil {
return "", nil, err
}
funcs, err := transformAttributeHelpers(source, target, ta, make(map[string]*TransformFunctionData))
if err != nil {
return "", nil, err
}
return strings.TrimRight(code, "\n"), funcs, nil
}
// transformAttribute returns the code to transform source attribute to target
// attribute. It returns an error if source and target are not compatible for
// transformation.
func transformAttribute(source, target *expr.AttributeExpr, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (code string, err error) {
if err = IsCompatible(source.Type, target.Type, sourceVar, targetVar); err != nil {
return
}
switch {
case expr.IsArray(source.Type):
code, err = transformArray(expr.AsArray(source.Type), expr.AsArray(target.Type), sourceVar, targetVar, newVar, ta)
case expr.IsMap(source.Type):
code, err = transformMap(expr.AsMap(source.Type), expr.AsMap(target.Type), sourceVar, targetVar, newVar, ta)
case expr.IsObject(source.Type):
code, err = transformObject(source, target, sourceVar, targetVar, newVar, ta)
default:
assign := "="
if newVar {
assign = ":="
}
if _, ok := target.Type.(expr.UserType); ok {
// Primitive user type, these are used for error results
cast := ta.TargetCtx.Scope.Ref(target, ta.TargetCtx.Pkg)
return fmt.Sprintf("%s %s %s(%s)\n", targetVar, assign, cast, sourceVar), nil
}
code = fmt.Sprintf("%s %s %s\n", targetVar, assign, sourceVar)
}
return
}
// transformObject generates Go code to transform source object to target
// object.
func transformObject(source, target *expr.AttributeExpr, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
var (
initCode string
postInitCode string
)
{
// walk through primitives first to initialize the struct
walkMatches(source, target, func(srcMatt, tgtMatt *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) {
if !expr.IsPrimitive(srcc.Type) {
return
}
var (
deref string
srcPtr = ta.SourceCtx.IsPrimitivePointer(n, srcMatt.AttributeExpr)
tgtPtr = ta.TargetCtx.IsPrimitivePointer(n, tgtMatt.AttributeExpr)
srcField = sourceVar + "." + GoifyAtt(srcc, srcMatt.ElemName(n), true)
tgtField = GoifyAtt(tgtc, tgtMatt.ElemName(n), true)
)
{
switch {
case srcPtr && !tgtPtr:
if !srcMatt.IsRequired(n) {
postInitCode += fmt.Sprintf("if %s != nil {\n\t%s.%s = %s\n}\n", srcField, targetVar, tgtField, "*"+srcField)
return
}
deref = "*"
case !srcPtr && tgtPtr:
deref = "&"
}
}
initCode += fmt.Sprintf("\n%s: %s%s,", tgtField, deref, srcField)
})
if initCode != "" {
initCode += "\n"
}
}
buffer := &bytes.Buffer{}
deref := "&"
assign := "="
if newVar {
assign = ":="
}
buffer.WriteString(fmt.Sprintf("%s %s %s%s{%s}\n", targetVar, assign, deref, ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg), initCode))
buffer.WriteString(postInitCode)
// iterate through attributes to initialize rest of the struct fields and
// handle default values
var err error
walkMatches(source, target, func(srcMatt, tgtMatt *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) {
if err = IsCompatible(srcc.Type, tgtc.Type, sourceVar, targetVar); err != nil {
return
}
var (
code string
srcVar = sourceVar + "." + GoifyAtt(srcc, srcMatt.ElemName(n), true)
tgtVar = targetVar + "." + GoifyAtt(tgtc, tgtMatt.ElemName(n), true)
)
{
_, ok := srcc.Type.(expr.UserType)
switch {
case expr.IsArray(srcc.Type):
code, err = transformArrayElem(expr.AsArray(srcc.Type), expr.AsArray(tgtc.Type), srcVar, tgtVar, false, ta)
case expr.IsMap(srcc.Type):
code, err = transformMapElem(expr.AsMap(srcc.Type), expr.AsMap(tgtc.Type), srcVar, tgtVar, false, ta)
case ok:
code = fmt.Sprintf("%s = %s(%s)\n", tgtVar, transformHelperName(srcc, tgtc, ta), srcVar)
case expr.IsObject(srcc.Type):
code, err = transformAttribute(srcc, tgtc, srcVar, tgtVar, false, ta)
}
}
if err != nil {
return
}
// We need to check for a nil source if it holds a reference (pointer to
// primitive or an object, array or map) and is not required. We also want
// to always check nil if the attribute is not a primitive; it's a
// 1) user type and we want to avoid calling transform helper functions
// with nil value
// 2) it's an object, map or array to avoid making empty arrays and maps
// and to avoid derefencing nil.
var checkNil bool
{
isRef := !expr.IsPrimitive(srcc.Type) && !srcMatt.IsRequired(n) || ta.SourceCtx.IsPrimitivePointer(n, srcMatt.AttributeExpr) && expr.IsPrimitive(srcc.Type)
marshalNonPrimitive := !expr.IsPrimitive(srcc.Type) && ta.SourceCtx.UseDefault && ta.TargetCtx.UseDefault
checkNil = isRef || marshalNonPrimitive
}
if code != "" && checkNil {
code = fmt.Sprintf("if %s != nil {\n\t%s}\n", srcVar, code)
}
// Default value handling. We need to handle default values if the target
// type uses default values (i.e. attributes with default values are
// non-pointers) and has a default value set.
if tdef := tgtc.DefaultValue; tdef != nil && ta.TargetCtx.UseDefault {
if (ta.SourceCtx.IsPrimitivePointer(n, srcMatt.AttributeExpr) || !expr.IsPrimitive(srcc.Type)) && !srcMatt.IsRequired(n) {
code += fmt.Sprintf("if %s == nil {\n\t", srcVar)
if ta.TargetCtx.IsPrimitivePointer(n, tgtMatt.AttributeExpr) && expr.IsPrimitive(tgtc.Type) {
code += fmt.Sprintf("var tmp %s = %#v\n\t%s = &tmp\n", GoNativeTypeName(tgtc.Type), tdef, tgtVar)
} else {
code += fmt.Sprintf("%s = %#v\n", tgtVar, tdef)
}
code += "}\n"
}
}
buffer.WriteString(code)
})
if err != nil {
return "", err
}
return buffer.String(), nil
}
// transformArray generates Go code to transform source array to target array.
func transformArray(source, target *expr.Array, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[0]", targetVar+"[0]"); err != nil {
return "", err
}
data := map[string]interface{}{
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg),
"SourceElem": source.ElemType,
"TargetElem": target.ElemType,
"SourceVar": sourceVar,
"TargetVar": targetVar,
"NewVar": newVar,
"TransformAttrs": ta,
"LoopVar": string(105 + strings.Count(targetVar, "[")),
}
var buf bytes.Buffer
if err := transformGoArrayT.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// transformArrayElem generates Go code to transform an object field of type array.
func transformArrayElem(source, target *expr.Array, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
st, tt := source.ElemType.Type, target.ElemType.Type
if err := IsCompatible(st, tt, sourceVar+"[0]", targetVar+"[0]"); err != nil {
return "", err
}
if _, ok := st.(expr.UserType); ok {
data := map[string]interface{}{
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg),
"SourceElem": source.ElemType,
"TargetElem": target.ElemType,
"SourceVar": sourceVar,
"TargetVar": targetVar,
"NewVar": newVar,
"TransformAttrs": ta,
"LoopVar": string(105 + strings.Count(targetVar, "[")),
}
var buf bytes.Buffer
if err := transformGoArrayElemT.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
return transformArray(source, target, sourceVar, targetVar, newVar, ta)
}
// transformMap generates Go code to transform source map to target map.
func transformMap(source, target *expr.Map, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
if err := IsCompatible(source.KeyType.Type, target.KeyType.Type, sourceVar+"[key]", targetVar+"[key]"); err != nil {
return "", err
}
if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[*]", targetVar+"[*]"); err != nil {
return "", err
}
data := map[string]interface{}{
"KeyTypeRef": ta.TargetCtx.Scope.Ref(target.KeyType, ta.TargetCtx.Pkg),
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg),
"SourceKey": source.KeyType,
"TargetKey": target.KeyType,
"SourceElem": source.ElemType,
"TargetElem": target.ElemType,
"SourceVar": sourceVar,
"TargetVar": targetVar,
"NewVar": newVar,
"TransformAttrs": ta,
"LoopVar": "",
}
if depth := MapDepth(target); depth > 0 {
data["LoopVar"] = string(97 + depth)
}
var buf bytes.Buffer
if err := transformGoMapT.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
// transformMapElem generates Go code to transform an object field of type map.
func transformMapElem(source, target *expr.Map, sourceVar, targetVar string, newVar bool, ta *TransformAttrs) (string, error) {
if err := IsCompatible(source.KeyType.Type, target.KeyType.Type, sourceVar+"[key]", targetVar+"[key]"); err != nil {
return "", err
}
if err := IsCompatible(source.ElemType.Type, target.ElemType.Type, sourceVar+"[*]", targetVar+"[*]"); err != nil {
return "", err
}
if _, ok := target.ElemType.Type.(expr.UserType); ok {
data := map[string]interface{}{
"KeyTypeRef": ta.TargetCtx.Scope.Ref(target.KeyType, ta.TargetCtx.Pkg),
"ElemTypeRef": ta.TargetCtx.Scope.Ref(target.ElemType, ta.TargetCtx.Pkg),
"SourceKey": source.KeyType,
"TargetKey": target.KeyType,
"SourceElem": source.ElemType,
"TargetElem": target.ElemType,
"SourceVar": sourceVar,
"TargetVar": targetVar,
"NewVar": newVar,
"TransformAttrs": ta,
"LoopVar": "",
}
if depth := MapDepth(target); depth > 0 {
data["LoopVar"] = string(97 + depth)
}
var buf bytes.Buffer
if err := transformGoMapElemT.Execute(&buf, data); err != nil {
return "", err
}
return buf.String(), nil
}
return transformMap(source, target, sourceVar, targetVar, newVar, ta)
}
// transformAttributeHelpers returns the Go transform functions and their definitions
// that may be used in code produced by Transform. It returns an error if source and
// target are incompatible (different types, fields of different type etc).
// transformAttributeHelpers recurses through the attribute types and calls
// collectHelpers for each child attribute. collectHelpers actually produces the
// transform helper functions for the given attribute.
//
// source, target are the source and target attributes used in transformation
//
// ta holds the transform attributes
//
// seen keeps track of generated transform functions to avoid infinite recursion.
//
func transformAttributeHelpers(source, target *expr.AttributeExpr, ta *TransformAttrs, seen map[string]*TransformFunctionData) (helpers []*TransformFunctionData, err error) {
// Do not generate a transform function for the top most user type.
switch {
case expr.IsArray(source.Type):
helpers, err = transformAttributeHelpers(expr.AsArray(source.Type).ElemType, expr.AsArray(target.Type).ElemType, ta, seen)
case expr.IsMap(source.Type):
sm, tm := expr.AsMap(source.Type), expr.AsMap(target.Type)
helpers, err = transformAttributeHelpers(sm.ElemType, tm.ElemType, ta, seen)
if err == nil {
var other []*TransformFunctionData
other, err = collectHelpers(sm.KeyType, tm.KeyType, true, ta, seen)
helpers = append(helpers, other...)
}
case expr.IsObject(source.Type):
walkMatches(source, target, func(srcMatt, _ *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) {
if err != nil {
return
}
var hs []*TransformFunctionData
hs, err = collectHelpers(srcc, tgtc, srcMatt.IsRequired(n), ta, seen)
helpers = append(helpers, hs...)
})
}
return
}
// collectHelpers recurses through the given attributes and returns the transform
// helper functions required to generate the transform code. If the attributes type
// is array or map then the recursion is done via transformAttributeHelpers so that
// the tope level conversion function is skipped as the generate code does not make
// use of it (since it inlines that top-level transformation).
func collectHelpers(source, target *expr.AttributeExpr, req bool, ta *TransformAttrs, seen map[string]*TransformFunctionData) (helpers []*TransformFunctionData, err error) {
name := transformHelperName(source, target, ta)
if _, ok := seen[name]; ok {
return
}
if _, ok := source.Type.(expr.UserType); ok {
var h *TransformFunctionData
if h, err = generateHelper(source, target, req, ta, seen); h != nil {
helpers = append(helpers, h)
}
}
switch {
case expr.IsArray(source.Type):
helpers, err = collectHelpers(expr.AsArray(source.Type).ElemType, expr.AsArray(target.Type).ElemType, req, ta, seen)
case expr.IsMap(source.Type):
sm, tm := expr.AsMap(source.Type), expr.AsMap(target.Type)
helpers, err = collectHelpers(sm.ElemType, tm.ElemType, req, ta, seen)
if err == nil {
var other []*TransformFunctionData
other, err = collectHelpers(sm.KeyType, tm.KeyType, req, ta, seen)
helpers = append(helpers, other...)
}
case expr.IsObject(source.Type):
walkMatches(source, target, func(srcMatt, _ *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string) {
if err != nil {
return
}
var hs []*TransformFunctionData
hs, err = collectHelpers(srcc, tgtc, srcMatt.IsRequired(n), ta, seen)
helpers = append(helpers, hs...)
})
}
return
}
// generateHelper generates the code that transform instances of source into
// target. Both source and targe must be user types or generateHelper panics.
// generateHelper returns nil if a helper has already been generated for the
// pair source, target.
func generateHelper(source, target *expr.AttributeExpr, req bool, ta *TransformAttrs, seen map[string]*TransformFunctionData) (*TransformFunctionData, error) {
name := transformHelperName(source, target, ta)
if _, ok := seen[name]; ok {
return nil, nil
}
code, err := transformAttribute(source.Type.(expr.UserType).Attribute(), target, "v", "res", true, ta)
if err != nil {
return nil, err
}
if !req {
code = "if v == nil {\n\treturn nil\n}\n" + code
}
tfd := &TransformFunctionData{
Name: name,
ParamTypeRef: ta.SourceCtx.Scope.Ref(source, ta.SourceCtx.Pkg),
ResultTypeRef: ta.TargetCtx.Scope.Ref(target, ta.TargetCtx.Pkg),
Code: code,
}
seen[name] = tfd
return tfd, nil
}
// walkMatches iterates through the attributes of source and looks for
// attributes with identical names in target. walkMatches calls the walker
// function for each pair of matched attributes. Both source and target must be
// objects or else walkMatches panics.
func walkMatches(source, target *expr.AttributeExpr, walker func(src, tgt *expr.MappedAttributeExpr, srcc, tgtc *expr.AttributeExpr, n string)) {
srcMatt := expr.NewMappedAttributeExpr(source)
tgtMatt := expr.NewMappedAttributeExpr(target)
srcObj := expr.AsObject(srcMatt.Type)
tgtObj := expr.AsObject(tgtMatt.Type)
for _, nat := range *srcObj {
if att := tgtObj.Attribute(nat.Name); att != nil {
walker(srcMatt, tgtMatt, nat.Attribute, att, nat.Name)
}
}
}
// transformHelperName returns the transformation function name to initialize a
// target user type from an instance of a source user type.
func transformHelperName(source, target *expr.AttributeExpr, ta *TransformAttrs) string {
var (
sname string
tname string
prefix string
)
{
sname = Goify(ta.SourceCtx.Scope.Name(source, ta.SourceCtx.Pkg), true)
tname = Goify(ta.TargetCtx.Scope.Name(target, ta.TargetCtx.Pkg), true)
prefix = ta.Prefix
if prefix == "" {
prefix = "transform"
}
}
return Goify(prefix+sname+"To"+tname, false)
}
const (
transformGoArrayTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make([]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
for {{ .LoopVar }}, val := range {{ .SourceVar }} {
{{ transformAttribute .SourceElem .TargetElem "val" (printf "%s[%s]" .TargetVar .LoopVar) false .TransformAttrs -}}
}
`
transformGoArrayElemTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make([]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
for {{ .LoopVar }}, val := range {{ .SourceVar }} {
{{ .TargetVar }}[{{ .LoopVar }}] = {{ transformHelperName .SourceElem .TargetElem .TransformAttrs }}(val)
}
`
transformGoMapTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make(map[{{ .KeyTypeRef }}]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
for key, val := range {{ .SourceVar }} {
{{ transformAttribute .SourceKey .TargetKey "key" "tk" true .TransformAttrs -}}
{{ transformAttribute .SourceElem .TargetElem "val" (printf "tv%s" .LoopVar) true .TransformAttrs -}}
{{ .TargetVar }}[tk] = {{ printf "tv%s" .LoopVar }}
}
`
transformGoMapElemTmpl = `{{ .TargetVar }} {{ if .NewVar }}:={{ else }}={{ end }} make(map[{{ .KeyTypeRef }}]{{ .ElemTypeRef }}, len({{ .SourceVar }}))
for key, val := range {{ .SourceVar }} {
{{ transformAttribute .SourceKey .TargetKey "key" "tk" true .TransformAttrs -}}
{{ .TargetVar }}[tk] = {{ transformHelperName .SourceElem .TargetElem .TransformAttrs }}(val)
}
`
)