-
-
Notifications
You must be signed in to change notification settings - Fork 564
/
Copy pathscope.go
236 lines (218 loc) Β· 6.32 KB
/
scope.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
package codegen
import (
"fmt"
"strconv"
"strings"
"goa.design/goa/expr"
)
type (
// NameScope defines a naming scope.
NameScope struct {
names map[string]string // type hash to unique name
counts map[string]int // raw type name to occurrence count
}
// Hasher is the interface implemented by the objects that must be
// scoped.
Hasher interface {
// Hash computes a unique instance hash suitable for indexing
// in a map.
Hash() string
}
// Scoper provides a scope for generating unique names.
Scoper interface {
Scope() *NameScope
}
)
// NewNameScope creates an empty name scope.
func NewNameScope() *NameScope {
return &NameScope{
names: make(map[string]string),
counts: make(map[string]int),
}
}
// HashedUnique builds the unique name for key using name and - if not unique -
// appending suffix and - if still not unique - a counter value. It returns
// the same value when called multiple times for a key returning the same hash.
func (s *NameScope) HashedUnique(key Hasher, name string, suffix ...string) string {
if n, ok := s.names[key.Hash()]; ok {
return n
}
name = s.Unique(name, suffix...)
s.names[key.Hash()] = name
return name
}
// Unique returns a unique name for the given name. A suffix is appended to the
// name if given name is not unique. If suffixed name is still not unique, a
// counter value is added to the suffixed name until unique.
func (s *NameScope) Unique(name string, suffix ...string) string {
c, ok := s.counts[name]
if !ok {
s.counts[name]++
return name
}
if len(suffix) > 0 {
name += suffix[0]
c, ok = s.counts[name]
if !ok {
s.counts[name]++
return name
}
}
for i := c; ; i++ {
ret := name + strconv.Itoa(i)
if _, ok := s.counts[ret]; !ok {
s.counts[ret]++
return ret
}
}
}
// Name returns a unique name for the given name by adding a counter value to
// the name until unique. It returns the same value when called multiple times
// for the same given name.
func (s *NameScope) Name(name string) string {
i, ok := s.counts[name]
if !ok {
return name
}
name += strconv.Itoa(i + 1)
return name
}
// GoTypeDef returns the Go code that defines a Go type which matches the data
// structure definition (the part that comes after `type foo`).
//
// ptr if true indicates that the attribute must be stored in a pointer
// (except array and map types which are always non-pointers)
//
// useDefault if true indicates that the attribute must not be a pointer
// if it has a default value.
func (s *NameScope) GoTypeDef(att *expr.AttributeExpr, ptr, useDefault bool) string {
switch actual := att.Type.(type) {
case expr.Primitive:
if t, _ := getMetaTypeInfo(att); t != "" {
return t
}
return GoNativeTypeName(actual)
case *expr.Array:
d := s.GoTypeDef(actual.ElemType, ptr, useDefault)
if expr.IsObject(actual.ElemType.Type) {
d = "*" + d
}
return "[]" + d
case *expr.Map:
keyDef := s.GoTypeDef(actual.KeyType, ptr, useDefault)
if expr.IsObject(actual.KeyType.Type) {
keyDef = "*" + keyDef
}
elemDef := s.GoTypeDef(actual.ElemType, ptr, useDefault)
if expr.IsObject(actual.ElemType.Type) {
elemDef = "*" + elemDef
}
return fmt.Sprintf("map[%s]%s", keyDef, elemDef)
case *expr.Object:
var ss []string
ss = append(ss, "struct {")
for _, nat := range *actual {
var (
fn string
tdef string
desc string
tags string
name = nat.Name
at = nat.Attribute
)
{
fn = GoifyAtt(at, name, true)
tdef = s.GoTypeDef(at, ptr, useDefault)
if expr.IsObject(at.Type) ||
att.IsPrimitivePointer(name, useDefault) ||
(ptr && expr.IsPrimitive(at.Type) && at.Type.Kind() != expr.AnyKind && at.Type.Kind() != expr.BytesKind) {
tdef = "*" + tdef
}
if at.Description != "" {
desc = Comment(at.Description) + "\n\t"
}
tags = AttributeTags(att, at)
}
ss = append(ss, fmt.Sprintf("\t%s%s %s%s", desc, fn, tdef, tags))
}
ss = append(ss, "}")
return strings.Join(ss, "\n")
case expr.UserType:
return s.GoTypeName(att)
default:
panic(fmt.Sprintf("unknown data type %T", actual)) // bug
}
}
// GoVar returns the Go code that returns the address of a variable of the Go type
// which matches the given attribute type.
func (s *NameScope) GoVar(varName string, dt expr.DataType) string {
// For a raw struct, no need to indirecting
if isRawStruct(dt) {
return varName
}
return "&" + varName
}
// GoTypeRef returns the Go code that refers to the Go type which matches the
// given attribute type.
func (s *NameScope) GoTypeRef(att *expr.AttributeExpr) string {
name := s.GoTypeName(att)
return goTypeRef(name, att.Type)
}
// GoFullTypeRef returns the Go code that refers to the Go type which matches
// the given attribute type defined in the given package if a user type.
func (s *NameScope) GoFullTypeRef(att *expr.AttributeExpr, pkg string) string {
name := s.GoFullTypeName(att, pkg)
return goTypeRef(name, att.Type)
}
// GoTypeName returns the Go type name of the given attribute type.
func (s *NameScope) GoTypeName(att *expr.AttributeExpr) string {
return s.GoFullTypeName(att, "")
}
// GoFullTypeName returns the Go type name of the given data type qualified with
// the given package name if applicable and if not the empty string.
func (s *NameScope) GoFullTypeName(att *expr.AttributeExpr, pkg string) string {
switch actual := att.Type.(type) {
case expr.Primitive:
if t, _ := getMetaTypeInfo(att); t != "" {
return t
}
return GoNativeTypeName(actual)
case *expr.Array:
return "[]" + s.GoFullTypeRef(actual.ElemType, pkg)
case *expr.Map:
return fmt.Sprintf("map[%s]%s",
s.GoFullTypeRef(actual.KeyType, pkg),
s.GoFullTypeRef(actual.ElemType, pkg))
case *expr.Object:
return s.GoTypeDef(att, false, false)
case expr.UserType:
if actual == expr.ErrorResult {
return "goa.ServiceError"
}
n := s.HashedUnique(actual, Goify(actual.Name(), true), "")
if pkg == "" {
return n
}
return pkg + "." + n
case expr.CompositeExpr:
return s.GoFullTypeName(actual.Attribute(), pkg)
default:
panic(fmt.Sprintf("unknown data type %T", actual)) // bug
}
}
func goTypeRef(name string, dt expr.DataType) string {
// For a raw struct, no need to dereference
if isRawStruct(dt) {
return name
}
return "*" + name
}
func isRawStruct(dt expr.DataType) bool {
if _, ok := dt.(*expr.Object); ok {
return true
}
if expr.IsObject(dt) {
return false
}
return true
}