-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathtransformer.go
234 lines (215 loc) Β· 6.93 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
package codegen
import (
"fmt"
"goa.design/goa/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.
Name(att *expr.AttributeExpr, pkg string) 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 the field name's 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 attribute uses non-pointers
// to hold optional attributes (i.e. attributes that are not required).
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
// Pkg is the package name where the attribute type is found.
Pkg string
// Scope is the attribute scope.
Scope Attributor
}
// 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,
Pkg: pkg,
Scope: &AttributeScope{scope: scope},
}
}
// IsCompatible returns an error if a and b are not both objects, both arrays,
// both maps or both the same primitive type. 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) {
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())
}
default:
if 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
}
if a.IgnoreRequired {
return false
}
return att.IsPrimitivePointer(name, a.UseDefault)
}
// IsRequired returns true if the attribute with given name is a required
// attribute in the parent. If IgnoreRequired is set to true, IsRequired always
// returns false.
func (a *AttributeContext) IsRequired(name string, att *expr.AttributeExpr) bool {
if a.IgnoreRequired {
return false
}
return att.IsRequired(name)
}
// 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,
}
}
// Name returns the type name for the given attribute.
func (a *AttributeScope) Name(att *expr.AttributeExpr, pkg string) string {
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
}