forked from goadesign/goa
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransformer.go
261 lines (241 loc) · 8.22 KB
/
transformer.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
package codegen
import (
"fmt"
"goa.design/goa/v3/expr"
)
type (
// Attributor defines the behavior of an attribute expression during code
// generation.
Attributor interface {
Scoper
// Name generates a valid name for the given attribute type. ptr and
// useDefault are used to generate inline struct type definitions.
Name(att *expr.AttributeExpr, pkg string, ptr, useDefault bool) string
// Ref generates a valid reference to the given attribute type.
Ref(att *expr.AttributeExpr, pkg string) string
// Field generates a valid data structure field identifier for the given
// attribute and field name. If firstUpper is true then the field name
// first letter is capitalized.
Field(att *expr.AttributeExpr, name string, firstUpper bool) string
}
// AttributeContext contains properties which impacts the code generating
// behavior of an attribute.
AttributeContext struct {
// Pointer if true indicates that the attribute uses pointers to hold
// primitive types even if they are required or has a default value.
// It ignores UseDefault and IgnoreRequired properties.
Pointer bool
// IgnoreRequired if true indicates that the transport object
// (proto) uses non-pointers to hold required attributes and
// therefore do not need to be validated.
IgnoreRequired bool
// UseDefault if true indicates that the attribute uses non-pointers for
// primitive types if they have default value. If false, the attribute with
// primitive types are non-pointers if they are required, otherwise they
// are pointers.
UseDefault bool
// Scope is the attribute scope.
Scope Attributor
// DefaultPkg is the default package name where the attribute
// type is found. it can be overridden via struct:pkg:path meta.
DefaultPkg string
// IsInterface is true if the attribute is an interface (union type).
// In this case assigning child attributes requires a type assertion.
IsInterface bool
}
// AttributeScope contains the scope of an attribute. It implements the
// Attributor interface.
AttributeScope struct {
// scope is the name scope for the attribute.
scope *NameScope
}
// TransformAttrs are the attributes that help in the transformation.
TransformAttrs struct {
// SourceCtx and TargetCtx are the source and target attribute context.
SourceCtx, TargetCtx *AttributeContext
// Prefix is the transform function helper prefix.
Prefix string
}
// TransformFunctionData describes a helper function used to transform
// user types. These are necessary to prevent potential infinite
// recursion when a type attribute is defined recursively. For example:
//
// var Recursive = Type("Recursive", func() {
// Attribute("r", "Recursive")
// }
//
// Transforming this type requires generating an intermediary function:
//
// func recursiveToRecursive(r *Recursive) *service.Recursive {
// var t service.Recursive
// if r.R != nil {
// t.R = recursiveToRecursive(r.R)
// }
// }
//
TransformFunctionData struct {
Name string
ParamTypeRef string
ResultTypeRef string
Code string
}
)
// NewAttributeContext initializes an attribute context.
func NewAttributeContext(pointer, reqIgnore, useDefault bool, pkg string, scope *NameScope) *AttributeContext {
return &AttributeContext{
Pointer: pointer,
IgnoreRequired: reqIgnore,
UseDefault: useDefault,
Scope: NewAttributeScope(scope),
DefaultPkg: pkg,
}
}
// NewAttributeScope initializes an attribute scope.
func NewAttributeScope(scope *NameScope) *AttributeScope {
return &AttributeScope{scope: scope}
}
// IsCompatible returns an error if a and b are not both objects, both arrays,
// both maps, both unions or one union and one object. actx and bctx are used
// to build the error message if any.
func IsCompatible(a, b expr.DataType, actx, bctx string) error {
switch {
case expr.IsObject(a):
if !expr.IsObject(b) && !expr.IsUnion(b) {
return fmt.Errorf("%s is an object but %s type is %s", actx, bctx, b.Name())
}
case expr.IsArray(a):
if !expr.IsArray(b) {
return fmt.Errorf("%s is an array but %s type is %s", actx, bctx, b.Name())
}
case expr.IsMap(a):
if !expr.IsMap(b) {
return fmt.Errorf("%s is a hash but %s type is %s", actx, bctx, b.Name())
}
case expr.IsUnion(a):
if !expr.IsUnion(b) && !expr.IsObject(b) {
return fmt.Errorf("%s is a union but %s type is %s", actx, bctx, b.Name())
}
default:
aUT, isAUT := a.(expr.UserType)
bUT, isBUT := b.(expr.UserType)
switch {
case isAUT && isBUT:
return IsCompatible(aUT.Attribute().Type, bUT.Attribute().Type, actx, bctx)
case isAUT:
return IsCompatible(aUT.Attribute().Type, b, actx, bctx)
case isBUT:
return IsCompatible(a, bUT.Attribute().Type, actx, bctx)
case a.Kind() != b.Kind():
return fmt.Errorf("%s is a %s but %s type is %s", actx, a.Name(), bctx, b.Name())
}
}
return nil
}
// AppendHelpers takes care of only appending helper functions from newH that
// are not already in oldH.
func AppendHelpers(oldH, newH []*TransformFunctionData) []*TransformFunctionData {
for _, h := range newH {
found := false
for _, h2 := range oldH {
if h.Name == h2.Name {
found = true
break
}
}
if !found {
oldH = append(oldH, h)
}
}
return oldH
}
// MapDepth returns the level of nested maps. For unnested maps, it returns 0.
func MapDepth(m *expr.Map) int {
return mapDepth(m.ElemType.Type, 0)
}
func mapDepth(dt expr.DataType, depth int, seen ...map[string]struct{}) int {
if mp := expr.AsMap(dt); mp != nil {
depth++
depth = mapDepth(mp.ElemType.Type, depth, seen...)
} else if ar := expr.AsArray(dt); ar != nil {
depth = mapDepth(ar.ElemType.Type, depth, seen...)
} else if mo := expr.AsObject(dt); mo != nil {
var s map[string]struct{}
if len(seen) > 0 {
s = seen[0]
} else {
s = make(map[string]struct{})
seen = append(seen, s)
}
key := dt.Name()
if u, ok := dt.(expr.UserType); ok {
key = u.ID()
}
if _, ok := s[key]; ok {
return depth
}
s[key] = struct{}{}
var level int
for _, nat := range *mo {
// if object type has attributes of type map then find out the attribute that has
// the deepest level of nested maps
lvl := 0
lvl = mapDepth(nat.Attribute.Type, lvl, seen...)
if lvl > level {
level = lvl
}
}
depth += level
}
return depth
}
// IsPrimitivePointer returns true if the attribute with the given name is a
// primitive pointer in the given parent attribute.
func (a *AttributeContext) IsPrimitivePointer(name string, att *expr.AttributeExpr) bool {
if at := att.Find(name); at != nil && (at.Type == expr.Any || at.Type == expr.Bytes) {
return false
}
if a.Pointer {
return true
}
return att.IsPrimitivePointer(name, a.UseDefault)
}
// Pkg returns the package name of the given type.
func (a *AttributeContext) Pkg(att *expr.AttributeExpr) string {
if loc := UserTypeLocation(att.Type); loc != nil {
return loc.PackageName()
}
return a.DefaultPkg
}
// Dup creates a shallow copy of the AttributeContext.
func (a *AttributeContext) Dup() *AttributeContext {
return &AttributeContext{
Pointer: a.Pointer,
IgnoreRequired: a.IgnoreRequired,
UseDefault: a.UseDefault,
Scope: a.Scope,
DefaultPkg: a.DefaultPkg,
}
}
// Name returns the type name for the given attribute.
func (a *AttributeScope) Name(att *expr.AttributeExpr, pkg string, ptr, useDefault bool) string {
if _, ok := att.Type.(expr.UserType); !ok && expr.IsObject(att.Type) {
// In the special case of anonymous / inline struct types the "name" is
// in fact the struct typedef. In this case we need to force the
// generation of the fields as pointers if needed as the default
// GoTransform algorithm does not allow for an override.
return a.scope.GoTypeDef(att, ptr, useDefault)
}
return a.scope.GoFullTypeName(att, pkg)
}
// Ref returns the type name for the given attribute.
func (a *AttributeScope) Ref(att *expr.AttributeExpr, pkg string) string {
return a.scope.GoFullTypeRef(att, pkg)
}
// Field returns a valid Go struct field name.
func (a *AttributeScope) Field(att *expr.AttributeExpr, name string, firstUpper bool) string {
return GoifyAtt(att, name, firstUpper)
}
// Scope returns the name scope.
func (a *AttributeScope) Scope() *NameScope {
return a.scope
}