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
|
// Copyright (c) 2020 Cisco and/or its affiliates.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package binapigen
import (
"fmt"
"path"
"sort"
"strings"
"git.fd.io/govpp.git/binapigen/vppapi"
)
type File struct {
vppapi.File
Generate bool
PackageName string
Imports []string
Enums []*Enum
Unions []*Union
Structs []*Struct
Aliases []*Alias
Messages []*Message
imports map[string]string
refmap map[string]string
}
func newFile(gen *Generator, apifile *vppapi.File) (*File, error) {
file := &File{
File: *apifile,
PackageName: sanitizedName(apifile.Name),
imports: make(map[string]string),
refmap: make(map[string]string),
}
sortFileObjects(&file.File)
for _, imp := range apifile.Imports {
file.Imports = append(file.Imports, normalizeImport(imp))
}
for _, enum := range apifile.EnumTypes {
file.Enums = append(file.Enums, newEnum(gen, file, enum))
}
for _, alias := range apifile.AliasTypes {
file.Aliases = append(file.Aliases, newAlias(gen, file, alias))
}
for _, structType := range apifile.StructTypes {
file.Structs = append(file.Structs, newStruct(gen, file, structType))
}
for _, union := range apifile.UnionTypes {
file.Unions = append(file.Unions, newUnion(gen, file, union))
}
for _, msg := range apifile.Messages {
file.Messages = append(file.Messages, newMessage(gen, file, msg))
}
return file, nil
}
func (file *File) isTypes() bool {
return strings.HasSuffix(file.File.Name, "_types")
}
func (file *File) hasService() bool {
return file.Service != nil && len(file.Service.RPCs) > 0
}
func (file *File) addRef(typ string, name string, ref interface{}) {
apiName := toApiType(name)
if _, ok := file.refmap[apiName]; ok {
logf("%s type %v already in refmap", typ, apiName)
return
}
file.refmap[apiName] = name
}
func (file *File) importedFiles(gen *Generator) []*File {
var files []*File
for _, imp := range file.Imports {
impFile, ok := gen.FilesByName[imp]
if !ok {
logf("file %s import %s not found API files", file.Name, imp)
continue
}
files = append(files, impFile)
}
return files
}
func (file *File) loadTypeImports(gen *Generator, typeFiles []*File) {
if len(typeFiles) == 0 {
return
}
for _, t := range file.Structs {
for _, imp := range typeFiles {
if _, ok := file.imports[t.Name]; ok {
break
}
for _, at := range imp.File.StructTypes {
if at.Name != t.Name {
continue
}
if len(at.Fields) != len(t.Fields) {
continue
}
file.imports[t.Name] = imp.PackageName
}
}
}
for _, t := range file.AliasTypes {
for _, imp := range typeFiles {
if _, ok := file.imports[t.Name]; ok {
break
}
for _, at := range imp.File.AliasTypes {
if at.Name != t.Name {
continue
}
if at.Length != t.Length {
continue
}
if at.Type != t.Type {
continue
}
file.imports[t.Name] = imp.PackageName
}
}
}
for _, t := range file.EnumTypes {
for _, imp := range typeFiles {
if _, ok := file.imports[t.Name]; ok {
break
}
for _, at := range imp.File.EnumTypes {
if at.Name != t.Name {
continue
}
if at.Type != t.Type {
continue
}
file.imports[t.Name] = imp.PackageName
}
}
}
for _, t := range file.UnionTypes {
for _, imp := range typeFiles {
if _, ok := file.imports[t.Name]; ok {
break
}
for _, at := range imp.File.UnionTypes {
if at.Name != t.Name {
continue
}
file.imports[t.Name] = imp.PackageName
/*if gen.ImportTypes {
imp.Generate = true
}*/
}
}
}
}
type Enum struct {
vppapi.EnumType
GoName string
}
func newEnum(gen *Generator, file *File, apitype vppapi.EnumType) *Enum {
typ := &Enum{
EnumType: apitype,
GoName: camelCaseName(apitype.Name),
}
gen.enumsByName[fmt.Sprintf("%s.%s", file.Name, typ.Name)] = typ
file.addRef("enum", typ.Name, typ)
return typ
}
type Alias struct {
vppapi.AliasType
GoName string
}
func newAlias(gen *Generator, file *File, apitype vppapi.AliasType) *Alias {
typ := &Alias{
AliasType: apitype,
GoName: camelCaseName(apitype.Name),
}
gen.aliasesByName[fmt.Sprintf("%s.%s", file.Name, typ.Name)] = typ
file.addRef("alias", typ.Name, typ)
return typ
}
type Struct struct {
vppapi.StructType
GoName string
Fields []*Field
}
func newStruct(gen *Generator, file *File, apitype vppapi.StructType) *Struct {
typ := &Struct{
StructType: apitype,
GoName: camelCaseName(apitype.Name),
}
for _, fieldType := range apitype.Fields {
field := newField(gen, file, fieldType)
field.ParentStruct = typ
typ.Fields = append(typ.Fields, field)
}
gen.structsByName[fmt.Sprintf("%s.%s", file.Name, typ.Name)] = typ
file.addRef("struct", typ.Name, typ)
return typ
}
type Union struct {
vppapi.UnionType
GoName string
Fields []*Field
}
func newUnion(gen *Generator, file *File, apitype vppapi.UnionType) *Union {
typ := &Union{
UnionType: apitype,
GoName: camelCaseName(apitype.Name),
}
gen.unionsByName[fmt.Sprintf("%s.%s", file.Name, typ.Name)] = typ
for _, fieldType := range apitype.Fields {
field := newField(gen, file, fieldType)
field.ParentUnion = typ
typ.Fields = append(typ.Fields, field)
}
file.addRef("union", typ.Name, typ)
return typ
}
type Message struct {
vppapi.Message
GoName string
Fields []*Field
}
func newMessage(gen *Generator, file *File, apitype vppapi.Message) *Message {
msg := &Message{
Message: apitype,
GoName: camelCaseName(apitype.Name),
}
for _, fieldType := range apitype.Fields {
field := newField(gen, file, fieldType)
field.ParentMessage = msg
msg.Fields = append(msg.Fields, field)
}
return msg
}
type Field struct {
vppapi.Field
GoName string
// Field parent
ParentMessage *Message
ParentStruct *Struct
ParentUnion *Union
// Type reference
Enum *Enum
Alias *Alias
Struct *Struct
Union *Union
}
func newField(gen *Generator, file *File, apitype vppapi.Field) *Field {
typ := &Field{
Field: apitype,
GoName: camelCaseName(apitype.Name),
}
return typ
}
type Service = vppapi.Service
type RPC = vppapi.RPC
func sortFileObjects(file *vppapi.File) {
// sort imports
sort.SliceStable(file.Imports, func(i, j int) bool {
return file.Imports[i] < file.Imports[j]
})
// sort enum types
sort.SliceStable(file.EnumTypes, func(i, j int) bool {
return file.EnumTypes[i].Name < file.EnumTypes[j].Name
})
// sort alias types
sort.Slice(file.AliasTypes, func(i, j int) bool {
return file.AliasTypes[i].Name < file.AliasTypes[j].Name
})
// sort struct types
sort.SliceStable(file.StructTypes, func(i, j int) bool {
return file.StructTypes[i].Name < file.StructTypes[j].Name
})
// sort union types
sort.SliceStable(file.UnionTypes, func(i, j int) bool {
return file.UnionTypes[i].Name < file.UnionTypes[j].Name
})
// sort messages
sort.SliceStable(file.Messages, func(i, j int) bool {
return file.Messages[i].Name < file.Messages[j].Name
})
// sort services
if file.Service != nil {
sort.Slice(file.Service.RPCs, func(i, j int) bool {
// dumps first
if file.Service.RPCs[i].Stream != file.Service.RPCs[j].Stream {
return file.Service.RPCs[i].Stream
}
return file.Service.RPCs[i].RequestMsg < file.Service.RPCs[j].RequestMsg
})
}
}
func sanitizedName(name string) string {
switch name {
case "interface":
return "interfaces"
case "map":
return "maps"
default:
return name
}
}
func normalizeImport(imp string) string {
imp = path.Base(imp)
if idx := strings.Index(imp, "."); idx >= 0 {
imp = imp[:idx]
}
return imp
}
|