This repository has been archived by the owner on May 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
definitions.go
401 lines (366 loc) · 11.4 KB
/
definitions.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
/*
IDL files describe models and web services in a format usable by the Babel tools.
The idl package defines types for the parse tree - the representation of parsed IDL.
For more information, see the README.md file.
*/
package idl
import (
"fmt"
"strings"
)
// Pair is a name/value pair that includes an optional format string for Sprintf.
// Pairs are used to represent constants and other values in the IDL.
type Pair struct {
Name string
Value interface{}
DataType string
}
// Const is a collection of constant value defintions. A Const block has a name
// and optional documentation comments.
type Const struct {
Comments []string
Name string
Values []*Pair
}
// Init initializes the Const for use.
func (c *Const) Init() {
c.Values = make([]*Pair, 0)
}
// Add appends a constant value definition to the Const block.
func (c *Const) Add(name string, value interface{}, dataType string) error {
for _, v := range c.Values {
if strings.ToLower(v.Name) == strings.ToLower(name) {
return fmt.Errorf("Constant redefined: %s.%s", c.Name, name)
}
}
if dataType == "" {
dataType = "string"
}
c.Values = append(c.Values, &Pair{Name: name, Value: value, DataType: dataType})
return nil
}
// FindValue returns the item with the given name.
func (c *Const) FindValue(name string) *Pair {
for _, v := range c.Values {
if v.Name == name {
return v
}
}
return nil
}
// Enum defines a group of enumerated values. An Enum block has a name and optional
// documentation comments.
type Enum struct {
Comments []string
Name string
Values []*Pair
}
// Init initializes the Enum for use.
func (e *Enum) Init() {
e.Values = make([]*Pair, 0)
}
// Add appends an enumeration value to the Enum block.
func (e *Enum) Add(name string, value int64) error {
for _, v := range e.Values {
if strings.ToLower(v.Name) == strings.ToLower(name) {
return fmt.Errorf("Enumeration redefined: %s.%s", e.Name, name)
}
}
e.Values = append(e.Values, &Pair{Name: name, Value: value, DataType: "int"})
return nil
}
// FindValue returns the item with the given name.
func (e *Enum) FindValue(name string) *Pair {
for _, v := range e.Values {
if v.Name == name {
return v
}
}
return nil
}
// Attribute defines extra metadata for definition following it. Attributes are
// written similar to C# attributes but have meaning specific to the output
// code generator. Attributes have a name and a collection of name/value pairs
// representing the Attirbute's parameters.
type Attribute struct {
Name string
Parameters []*Pair
Scope string
}
// Field defines a structure field, which has optional docmumentation comments, optional
// attributes, a type, and a name.
type Field struct {
Comments []string
Attributes []*Attribute
Type *Type
Name string
Initializer *Pair
}
// Init initializes the Field for use.
func (f *Field) Init() {
f.Comments = make([]string, 0)
f.Attributes = make([]*Attribute, 0)
}
// Required determines whether the field is required to have a value assigned.
func (f *Field) Required() bool {
return false
}
// optional determines whether the field is not required to have a vakue assigned.
func (f *Field) Optional() bool {
return !f.Required()
}
// IsCollection returns true if the type of the field is a list or map.
func (f *Field) IsCollection() bool {
return f.Type.IsList() || f.Type.IsMap()
}
// IsList returns true if the type of the field is a list
func (f *Field) IsList() bool {
return f.Type.IsList()
}
// IsMap returns true if the type of the field is a map
func (f *Field) IsMap() bool {
return f.Type.IsMap()
}
// SetInitializer assigns the given initial value and type, returning an error if it
// isn't compatible with the type of the field.
func (f *Field) SetInitializer(i interface{}, t string) error {
switch t {
case "int":
if !f.Type.IsInt() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "float":
if !f.Type.IsFloat() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "bool":
if !f.Type.IsBool() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "string":
if !f.Type.IsString() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "char":
if !f.Type.IsChar() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "#ref":
// These are checked later
default:
return fmt.Errorf("Invalid data type of initializer for %s: %s", f.Name, t)
}
f.Initializer = &Pair{Value: i, DataType: t, Name: f.Name}
return nil
}
// CheckInitializer verifies that the initalizer is appropriate for the type
func (f *Field) CheckInitializer(idl *Idl) error {
if f.Initializer != nil {
t := f.Initializer.DataType
switch t {
case "int":
if !f.Type.IsInt() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "float":
if !f.Type.IsFloat() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "bool":
if !f.Type.IsBool() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "string":
if !f.Type.IsString() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "char":
if !f.Type.IsChar() {
return fmt.Errorf("Invalid initialization of %s %s with %s", f.Type, f.Name, t)
}
case "#ref":
vals := strings.Split(f.Initializer.Value.(string), ".")
if len(vals) != 2 {
return fmt.Errorf("Invalid reference syntax for %s %s: %s", f.Type, f.Name, f.Initializer.Value)
}
enum := idl.FindEnum(vals[0])
cons := idl.FindConst(vals[0])
if enum != nil {
if !f.Type.IsEnum(idl) {
return fmt.Errorf("The field %s %s cannot be initialized with an enumeration: %s", f.Type, f.Name, enum.Name)
}
if f.Type.Name != enum.Name {
return fmt.Errorf("The field %s %s is being initialized with the wrong enumeration: %s", f.Type, f.Name, enum.Name)
}
if enum.FindValue(vals[1]) == nil {
return fmt.Errorf("The field %s %s is being initialized with a missing enumeration value: %s.%s", f.Type, f.Name, enum.Name, vals[1])
}
} else if cons != nil {
v := cons.FindValue(vals[1])
if v == nil {
return fmt.Errorf("The field %s %s is being initialized with a missing const value: %s.%s", f.Type, f.Name, cons.Name, vals[1])
}
if (v.DataType == "int" && !f.Type.IsInt()) || (v.DataType == "float" && !f.Type.IsFloat()) || (v.DataType == "bool" && !f.Type.IsBool()) || (v.DataType == "string" && !f.Type.IsString()) || (v.DataType == "char" && !f.Type.IsChar()) {
return fmt.Errorf("The field %s %s is being initialized with a constant of the wrong type: %s.%s", f.Type, f.Name, cons.Name, vals[1])
}
} else {
return fmt.Errorf("The field %s %s is being initialized something we can't find: %s.%s", f.Type, f.Name, vals[0], vals[1])
}
default:
return fmt.Errorf("Invalid data type of initializer for %s: %s", f.Name, t)
}
}
return nil
}
// Struct defines a collection of fields transmitted as part of a service call.
// Structs have optional documenation comments and attributes. They may also
// extend other Structs.
type Struct struct {
Comments []string
Attributes []*Attribute
Name string
Extends string
Fields []*Field
Abstract bool
}
// Init initializes the Struct for use.
func (s *Struct) Init() {
s.Comments = make([]string, 0)
s.Attributes = make([]*Attribute, 0)
s.Fields = make([]*Field, 0)
}
// AddField adds a field with the given data type and names to the Struct.
func (s *Struct) AddField(dataType *Type, name string) (*Field, error) {
for _, fld := range s.Fields {
if strings.ToLower(fld.Name) == strings.ToLower(name) {
return nil, fmt.Errorf("Structure field redefined: %s.%s", s.Name, name)
}
}
f := new(Field)
f.Init()
f.Name = name
f.Type = dataType
s.Fields = append(s.Fields, f)
return f, nil
}
// HasRequiredFields return true if the struct has 1 or more fields that are considered required.
func (s *Struct) HasRequiredFields() bool {
hasRequired := false
for _, fld := range s.Fields {
if fld.Required() {
hasRequired = true
}
}
return hasRequired
}
// RequiredFields gets all required fields for this struct
func (s *Struct) RequiredFields() []*Field {
flds := make([]*Field, 0)
for _, fld := range s.Fields {
if fld.Required() {
flds = append(flds, fld)
}
}
return flds
}
// BaseClasses returns the base classes of the named class ordered
// from top to bottom.
func (s *Struct) BaseClasses(idl *Idl) ([]*Struct, error) {
result := make([]*Struct, 0)
me := s
baseName := me.Extends
for baseName != "" {
me := idl.FindStruct(baseName)
if me == nil {
return nil, fmt.Errorf("Parent not found: %s", baseName)
}
baseName = me.Extends
result = append([]*Struct{me}, result...)
}
return result, nil
}
// SubClasses returns a list of structs that extend this one, in no paticular order.
// BUG WARNING: This can't look down into IDL files that are not currently
// loaded. It can only see subclasses in the current context.
func (s *Struct) SubClasses(idl *Idl) []*Struct {
result := make([]*Struct, 0)
found := make(map[string]bool)
for _, v := range idl.Structs {
if v.Extends == s.Name {
result = append(result, v)
}
}
for _, imp := range idl.UniqueImports() {
for _, v := range imp.Structs {
_, ok := found[v.Name]
if !ok {
if v.Extends == s.Name {
result = append(result, v)
}
}
}
}
return result
}
// Method defines a service method that can be called to communicate with a service.
// Methods have parameters and optional documentation comments and attributes.
type Method struct {
Comments []string
Attributes []*Attribute
Returns *Type
Name string
Parameters []*Field
}
// Init initializes a Method for use.
func (m *Method) Init() {
m.Comments = make([]string, 0)
m.Attributes = make([]*Attribute, 0)
m.Parameters = make([]*Field, 0)
}
// HasParameters returns true if there are parameters for this method.
func (m *Method) HasParameters() bool {
return len(m.Parameters) != 0
}
// AddParameter adds a parameter with the given data type and name to the Method.
func (m *Method) AddParameter(dataType *Type, name string) (*Field, error) {
for _, parm := range m.Parameters {
if strings.ToLower(parm.Name) == strings.ToLower(name) {
return nil, fmt.Errorf("Parameter redefined in method %s: %s", m.Name, name)
}
}
p := new(Field)
p.Init()
p.Name = name
p.Type = dataType
m.Parameters = append(m.Parameters, p)
return p, nil
}
// Service defines a web service interface. Services have optional documentation
// comments and attributes, as well as a collection of Methods.
type Service struct {
Comments []string
Attributes []*Attribute
Name string
Methods []*Method
}
// Init initializes the Service for use.
func (s *Service) Init() {
s.Comments = make([]string, 0)
s.Attributes = make([]*Attribute, 0)
s.Methods = make([]*Method, 0)
}
// AddMethod appends a method of the given return type and name to the service.
// A pointer to the new Method is returned.
func (s *Service) AddMethod(returnType *Type, name string) (*Method, error) {
for _, meth := range s.Methods {
if strings.ToLower(meth.Name) == strings.ToLower(name) {
return nil, fmt.Errorf("Method redefined: %s.%s", s.Name, name)
}
}
m := new(Method)
m.Init()
m.Name = name
m.Returns = returnType
s.Methods = append(s.Methods, m)
return m, nil
}