/
package.go
318 lines (269 loc) · 7.61 KB
/
package.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
package scanner
import (
"fmt"
"go/ast"
"strings"
)
// Package holds information about a single Go package and
// a reference of all defined structs and type aliases.
// A Package is only safe to use once it is resolved.
type Package struct {
Resolved bool
Path string
Name string
Structs []*Struct
Enums []*Enum
Funcs []*Func
Aliases map[string]Type
}
// collectEnums finds the enum values collected during the scan and generates
// the corresponding enum types, removing them as aliases from the package.
func (p *Package) collectEnums(ctx *context) {
for k := range p.Aliases {
if vals, ok := ctx.enumValues[k]; ok {
idx := strings.LastIndex(k, ".")
name := k[idx+1:]
if !ctx.shouldGenerateType(name) {
continue
}
hasStringMethod := containsString(ctx.enumWithString, k)
p.Enums = append(p.Enums, newEnum(ctx, name, vals, hasStringMethod))
delete(p.Aliases, k)
}
}
}
func containsString(arr []string, s string) bool {
for _, str := range arr {
if str == s {
return true
}
}
return false
}
// Type is the common interface for all possible types supported in protogo.
// Type is neither a representation of a Go type nor a representation of a
// protobuf type. Is an intermediate representation to ease future steps in
// the conversion from Go to protobuf.
// All types can be repeated (or not).
type Type interface {
fmt.Stringer
SetRepeated(bool)
IsRepeated() bool
SetNullable(bool)
IsNullable() bool
// TypeString returns a string representing the final type.
// Though this might seem that this should be just String, for Alias types
// both representations are different: a string representation of the final
// type, this is just the alias, while string contains also the underlying type.
TypeString() string
// Name returns the unqualified name.
UnqualifiedName() string
}
// BaseType contains the common fields for all the types.
type BaseType struct {
Repeated bool
Nullable bool
}
func newBaseType() *BaseType {
return &BaseType{
Repeated: false,
Nullable: false,
}
}
// IsRepeated reports wether the type is repeated or not.
func (t *BaseType) IsRepeated() bool { return t.Repeated }
// SetRepeated sets the type as repeated or not repeated.
func (t *BaseType) SetRepeated(v bool) { t.Repeated = v }
// IsNullable reports wether the type is a pointer or not.
func (t *BaseType) IsNullable() bool { return t.Nullable }
// SetNullable sets the type as pointer.
func (t *BaseType) SetNullable(v bool) { t.Nullable = v }
// TypeString returns a string representation for the type casting
func (t *BaseType) TypeString() string { panic("not implemented") }
// String returns a string representation for the type
func (t *BaseType) String() string { panic("not implemented") }
// String returns a string representation for the type
func (t *BaseType) UnqualifiedName() string { panic("not implemented") }
// Basic is a basic type, which only is identified by its name.
type Basic struct {
*BaseType
Name string
}
// NewBasic creates a new basic type given its name.
func NewBasic(name string) Type {
return &Basic{
newBaseType(),
name,
}
}
// IsNullable returns true. Basic types though cannot be nullable, they are considered so in protobuf.
func (b Basic) IsNullable() bool { return true }
// String returns a string representation for the type
func (b Basic) String() string {
return b.Name
}
// TypeString returns a string representation for the type casting
func (b Basic) TypeString() string {
return b.String()
}
// UnqualifiedName returns the bare name, without the package.
func (b Basic) UnqualifiedName() string {
return b.Name
}
// Named is non-basic type identified by a name on some package.
type Named struct {
*BaseType
Path string
Name string
}
// String returns a string representation for the type
func (n Named) String() string {
if n.Path == "" {
return n.Name
}
return fmt.Sprintf("%s.%s", n.Path, n.Name)
}
// TypeString returns a string representation for the type casting
func (n Named) TypeString() string {
return n.String()
}
// UnqualifiedName returns the bare name, without the package.
func (n Named) UnqualifiedName() string {
return n.Name
}
// NewNamed creates a new named type given its package path and name.
func NewNamed(path, name string) Type {
return &Named{
newBaseType(),
path,
name,
}
}
// Alias represents a type declaration from a type to another type
type Alias struct {
*BaseType
// Type represents the type being declared
Type Type
// Underlying represent the aliased type.
Underlying Type
}
func NewAlias(typ, underlying Type) Type {
return &Alias{
Type: typ,
Underlying: underlying,
}
}
func (a Alias) IsNullable() bool { return a.Type.IsNullable() || a.Underlying.IsNullable() }
func (a Alias) IsRepeated() bool { return a.Type.IsRepeated() || a.Underlying.IsRepeated() }
// String returns a string representation for the type
func (a Alias) String() string {
return fmt.Sprintf("type %s %s", a.Type.String(), a.Underlying.String())
}
// TypeString returns a string representation for the type casting
func (a Alias) TypeString() string {
return a.Type.TypeString()
}
// UnqualifiedName returns the bare name, without the package.
func (a Alias) UnqualifiedName() string {
return a.Type.UnqualifiedName()
}
// Map is a map type with a key and a value type.
type Map struct {
*BaseType
Key Type
Value Type
}
// NewMap creates a new map type with the given key and value types.
func NewMap(key, val Type) Type {
return &Map{
newBaseType(),
key,
val,
}
}
// String returns a string representation for the type
func (m Map) String() string {
return fmt.Sprintf("map[%s]%s", m.Key.String(), m.Value.String())
}
// TypeString returns a string representation for the type casting
func (m Map) TypeString() string {
return m.String()
}
// UnqualifiedName returns the bare name, without the package.
func (m Map) UnqualifiedName() string {
return m.String()
}
// Documentable is something whose documentation can be set.
type Documentable interface {
// SetDocs sets the documentation from an AST comment group.
SetDocs(*ast.CommentGroup)
}
// Docs holds the documentation of a struct, enum, value, field, etc.
type Docs struct {
Doc []string
}
// SetDocs sets the documentation from an AST comment group.
// It removes the //proteus:generate comment from the comments.
func (d *Docs) SetDocs(comments *ast.CommentGroup) {
var list []*ast.Comment
if comments != nil {
for _, c := range comments.List {
if !strings.HasPrefix(c.Text, genComment) {
list = append(list, c)
}
}
}
if len(list) > 0 {
d.Doc = strings.Split(strings.TrimSpace(
(&ast.CommentGroup{List: list}).Text(),
), "\n")
}
}
// Enum consists of a list of possible values.
type Enum struct {
Docs
Name string
Values []*EnumValue
IsStringer bool
}
// EnumValue is a possible value of an enum.
type EnumValue struct {
Docs
Name string
}
// Struct represents a Go struct with its name and fields.
// All structs
type Struct struct {
Docs
Generate bool
Name string
Fields []*Field
IsStringer bool
}
// HasField reports wether a struct has a given field name.
func (s *Struct) HasField(name string) bool {
for _, f := range s.Fields {
if f.Name == name {
return true
}
}
return false
}
// Field contains name and type of a struct field.
type Field struct {
Docs
Name string
Type Type
}
// Func is either a function or a method. Receiver will be nil in functions,
// otherwise it is a method.
type Func struct {
Docs
Name string
// Receiver will not be nil if it's a method.
Receiver Type
Input []Type
Output []Type
// IsVariadic will be true if the last input parameter is variadic.
IsVariadic bool
}