1 // Copyright (c) 2017 Cisco and/or its affiliates.
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at:
7 // http://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
28 inputFileExt = ".api.json" // file extension of the VPP API files
29 outputFileExt = ".ba.go" // file extension of the Go generated files
31 govppApiImportPath = "git.fd.io/govpp.git/api" // import path of the govpp API package
32 constAPIVersionCrc = "APIVersionCrc" // name for the API version CRC constant
35 // context is a structure storing data for code generation
37 inputFile string // input file with VPP API in JSON
38 outputFile string // output file with generated Go package
40 inputData []byte // contents of the input file
42 includeAPIVersionCrc bool // include constant with API version CRC string
43 includeComments bool // include parts of original source in comments
45 moduleName string // name of the source VPP module
46 packageName string // name of the Go package being generated
48 packageData *Package // parsed package data
51 // getContext returns context details of the code generation task
52 func getContext(inputFile, outputDir string) (*context, error) {
53 if !strings.HasSuffix(inputFile, inputFileExt) {
54 return nil, fmt.Errorf("invalid input file name: %q", inputFile)
62 inputFileName := filepath.Base(inputFile)
63 ctx.moduleName = inputFileName[:strings.Index(inputFileName, ".")]
65 // alter package names for modules that are reserved keywords in Go
66 switch ctx.moduleName {
68 ctx.packageName = "interfaces"
70 ctx.packageName = "maps"
72 ctx.packageName = ctx.moduleName
76 packageDir := filepath.Join(outputDir, ctx.packageName)
77 outputFileName := ctx.packageName + outputFileExt
78 ctx.outputFile = filepath.Join(packageDir, outputFileName)
83 // generatePackage generates code for the parsed package data and writes it into w
84 func generatePackage(ctx *context, w *bufio.Writer) error {
85 logf("generating package %q", ctx.packageName)
87 // generate file header
88 generateHeader(ctx, w)
89 generateImports(ctx, w)
91 if ctx.includeAPIVersionCrc {
92 fmt.Fprintf(w, "// %s defines API version CRC of the VPP binary API module.\n", constAPIVersionCrc)
93 fmt.Fprintf(w, "const %s = %v\n", constAPIVersionCrc, ctx.packageData.APIVersion)
98 if len(ctx.packageData.Services) > 0 {
99 generateServices(ctx, w, ctx.packageData.Services)
101 // TODO: generate implementation for Services interface
105 if len(ctx.packageData.Enums) > 0 {
106 fmt.Fprintf(w, "/* Enums */\n\n")
108 for _, enum := range ctx.packageData.Enums {
109 generateEnum(ctx, w, &enum)
114 if len(ctx.packageData.Aliases) > 0 {
115 fmt.Fprintf(w, "/* Aliases */\n\n")
117 for _, alias := range ctx.packageData.Aliases {
118 generateAlias(ctx, w, &alias)
123 if len(ctx.packageData.Types) > 0 {
124 fmt.Fprintf(w, "/* Types */\n\n")
126 for _, typ := range ctx.packageData.Types {
127 generateType(ctx, w, &typ)
132 if len(ctx.packageData.Unions) > 0 {
133 fmt.Fprintf(w, "/* Unions */\n\n")
135 for _, union := range ctx.packageData.Unions {
136 generateUnion(ctx, w, &union)
141 if len(ctx.packageData.Messages) > 0 {
142 fmt.Fprintf(w, "/* Messages */\n\n")
144 for _, msg := range ctx.packageData.Messages {
145 generateMessage(ctx, w, &msg)
148 // generate message registrations
149 fmt.Fprintln(w, "func init() {")
150 for _, msg := range ctx.packageData.Messages {
151 name := camelCaseName(msg.Name)
152 fmt.Fprintf(w, "\tapi.RegisterMessage((*%s)(nil), \"%s\")\n", name, ctx.moduleName+"."+name)
157 fmt.Fprintln(w, "var Messages = []api.Message{")
158 for _, msg := range ctx.packageData.Messages {
159 name := camelCaseName(msg.Name)
160 fmt.Fprintf(w, "\t(*%s)(nil),\n", name)
166 if err := w.Flush(); err != nil {
167 return fmt.Errorf("flushing data to %s failed: %v", ctx.outputFile, err)
173 // generateHeader writes generated package header into w
174 func generateHeader(ctx *context, w io.Writer) {
175 fmt.Fprintln(w, "// Code generated by GoVPP binapi-generator. DO NOT EDIT.")
176 fmt.Fprintf(w, "// source: %s\n", ctx.inputFile)
179 fmt.Fprintln(w, "/*")
180 fmt.Fprintf(w, " Package %s is a generated from VPP binary API module '%s'.\n", ctx.packageName, ctx.moduleName)
182 fmt.Fprintln(w, " It contains following objects:")
183 var printObjNum = func(obj string, num int) {
186 if strings.HasSuffix(obj, "s") {
192 fmt.Fprintf(w, "\t%3d %s\n", num, obj)
196 printObjNum("service", len(ctx.packageData.Services))
197 printObjNum("enum", len(ctx.packageData.Enums))
198 printObjNum("alias", len(ctx.packageData.Aliases))
199 printObjNum("type", len(ctx.packageData.Types))
200 printObjNum("union", len(ctx.packageData.Unions))
201 printObjNum("message", len(ctx.packageData.Messages))
202 fmt.Fprintln(w, "*/")
203 fmt.Fprintf(w, "package %s\n", ctx.packageName)
207 // generateImports writes generated package imports into w
208 func generateImports(ctx *context, w io.Writer) {
209 fmt.Fprintf(w, "import api \"%s\"\n", govppApiImportPath)
210 fmt.Fprintf(w, "import struc \"%s\"\n", "github.com/lunixbochs/struc")
211 fmt.Fprintf(w, "import bytes \"%s\"\n", "bytes")
214 fmt.Fprintf(w, "// Reference imports to suppress errors if they are not otherwise used.\n")
215 fmt.Fprintf(w, "var _ = api.RegisterMessage\n")
216 fmt.Fprintf(w, "var _ = struc.Pack\n")
217 fmt.Fprintf(w, "var _ = bytes.NewBuffer\n")
220 /*fmt.Fprintln(w, "// This is a compile-time assertion to ensure that this generated file")
221 fmt.Fprintln(w, "// is compatible with the GoVPP api package it is being compiled against.")
222 fmt.Fprintln(w, "// A compilation error at this line likely means your copy of the")
223 fmt.Fprintln(w, "// GoVPP api package needs to be updated.")
224 fmt.Fprintln(w, "const _ = api.GoVppAPIPackageIsVersion1 // please upgrade the GoVPP api package")
228 // generateComment writes generated comment for the object into w
229 func generateComment(ctx *context, w io.Writer, goName string, vppName string, objKind string) {
230 if objKind == "service" {
231 fmt.Fprintf(w, "// %s represents VPP binary API services:\n", goName)
233 fmt.Fprintf(w, "// %s represents VPP binary API %s '%s':\n", goName, objKind, vppName)
236 if !ctx.includeComments {
240 var isNotSpace = func(r rune) bool {
241 return !unicode.IsSpace(r)
244 // print out the source of the generated object
247 objTitle := fmt.Sprintf(`"%s",`, vppName)
249 case "alias", "service":
250 objTitle = fmt.Sprintf(`"%s": {`, vppName)
254 inputBuff := bytes.NewBuffer(ctx.inputData)
257 var trimIndent string
260 line, err := inputBuff.ReadString('\n')
266 noSpaceAt := strings.IndexFunc(line, isNotSpace)
268 indent = strings.Index(line, objTitle)
272 trimIndent = line[:indent]
273 // If no other non-whitespace character then we are at the message header.
274 if trimmed := strings.TrimSpace(line); trimmed == objTitle {
276 fmt.Fprintln(w, "//")
278 } else if noSpaceAt < indent {
279 break // end of the definition in JSON for array types
280 } else if objFound && mapType && noSpaceAt <= indent {
281 fmt.Fprintf(w, "//\t%s", strings.TrimPrefix(line, trimIndent))
282 break // end of the definition in JSON for map types (aliases, services)
284 fmt.Fprintf(w, "//\t%s", strings.TrimPrefix(line, trimIndent))
287 fmt.Fprintln(w, "//")
290 // generateServices writes generated code for the Services interface into w
291 func generateServices(ctx *context, w *bufio.Writer, services []Service) {
292 // generate services comment
293 generateComment(ctx, w, "Services", "services", "service")
295 // generate interface
296 fmt.Fprintf(w, "type %s interface {\n", "Services")
297 for _, svc := range services {
298 generateService(ctx, w, &svc)
305 // generateService writes generated code for the service into w
306 func generateService(ctx *context, w io.Writer, svc *Service) {
307 reqTyp := camelCaseName(svc.RequestType)
309 // method name is same as parameter type name by default
312 // use Dump as prefix instead of suffix for stream services
313 if m := strings.TrimSuffix(method, "Dump"); method != m {
318 params := fmt.Sprintf("*%s", reqTyp)
320 if replyType := camelCaseName(svc.ReplyType); replyType != "" {
321 repTyp := fmt.Sprintf("*%s", replyType)
323 repTyp = fmt.Sprintf("[]%s", repTyp)
325 returns = fmt.Sprintf("(%s, error)", repTyp)
328 fmt.Fprintf(w, "\t%s(%s) %s\n", method, params, returns)
331 // generateEnum writes generated code for the enum into w
332 func generateEnum(ctx *context, w io.Writer, enum *Enum) {
333 name := camelCaseName(enum.Name)
334 typ := binapiTypes[enum.Type]
336 logf(" writing enum %q (%s) with %d entries", enum.Name, name, len(enum.Entries))
338 // generate enum comment
339 generateComment(ctx, w, name, enum.Name, "enum")
341 // generate enum definition
342 fmt.Fprintf(w, "type %s %s\n", name, typ)
345 fmt.Fprintln(w, "const (")
347 // generate enum entries
348 for _, entry := range enum.Entries {
349 fmt.Fprintf(w, "\t%s %s = %v\n", entry.Name, name, entry.Value)
357 // generateAlias writes generated code for the alias into w
358 func generateAlias(ctx *context, w io.Writer, alias *Alias) {
359 name := camelCaseName(alias.Name)
361 logf(" writing type %q (%s), length: %d", alias.Name, name, alias.Length)
363 // generate struct comment
364 generateComment(ctx, w, name, alias.Name, "alias")
366 // generate struct definition
367 fmt.Fprintf(w, "type %s ", name)
369 if alias.Length > 0 {
370 fmt.Fprintf(w, "[%d]", alias.Length)
373 dataType := convertToGoType(ctx, alias.Type)
374 fmt.Fprintf(w, "%s\n", dataType)
379 // generateUnion writes generated code for the union into w
380 func generateUnion(ctx *context, w io.Writer, union *Union) {
381 name := camelCaseName(union.Name)
383 logf(" writing union %q (%s) with %d fields", union.Name, name, len(union.Fields))
385 // generate struct comment
386 generateComment(ctx, w, name, union.Name, "union")
388 // generate struct definition
389 fmt.Fprintln(w, "type", name, "struct {")
391 // maximum size for union
392 maxSize := getUnionSize(ctx, union)
394 // generate data field
395 fieldName := "Union_data"
396 fmt.Fprintf(w, "\t%s [%d]byte\n", fieldName, maxSize)
398 // generate end of the struct
401 // generate name getter
402 generateTypeNameGetter(w, name, union.Name)
404 // generate CRC getter
405 generateCrcGetter(w, name, union.CRC)
407 // generate getters for fields
408 for _, field := range union.Fields {
409 fieldName := camelCaseName(field.Name)
410 fieldType := convertToGoType(ctx, field.Type)
411 generateUnionGetterSetter(w, name, fieldName, fieldType)
414 // generate union methods
415 //generateUnionMethods(w, name)
420 // generateUnionMethods generates methods that implement struc.Custom
421 // interface to allow having Union_data field unexported
422 // TODO: do more testing when unions are actually used in some messages
423 func generateUnionMethods(w io.Writer, structName string) {
424 // generate struc.Custom implementation for union
426 func (u *%[1]s) Pack(p []byte, opt *struc.Options) (int, error) {
427 var b = new(bytes.Buffer)
428 if err := struc.PackWithOptions(b, u.union_data, opt); err != nil {
434 func (u *%[1]s) Unpack(r io.Reader, length int, opt *struc.Options) error {
435 return struc.UnpackWithOptions(r, u.union_data[:], opt)
437 func (u *%[1]s) Size(opt *struc.Options) int {
438 return len(u.union_data)
440 func (u *%[1]s) String() string {
441 return string(u.union_data[:])
446 func generateUnionGetterSetter(w io.Writer, structName string, getterField, getterStruct string) {
448 func %[1]s%[2]s(a %[3]s) (u %[1]s) {
452 func (u *%[1]s) Set%[2]s(a %[3]s) {
453 var b = new(bytes.Buffer)
454 if err := struc.Pack(b, &a); err != nil {
457 copy(u.Union_data[:], b.Bytes())
459 func (u *%[1]s) Get%[2]s() (a %[3]s) {
460 var b = bytes.NewReader(u.Union_data[:])
464 `, structName, getterField, getterStruct)
467 // generateType writes generated code for the type into w
468 func generateType(ctx *context, w io.Writer, typ *Type) {
469 name := camelCaseName(typ.Name)
471 logf(" writing type %q (%s) with %d fields", typ.Name, name, len(typ.Fields))
473 // generate struct comment
474 generateComment(ctx, w, name, typ.Name, "type")
476 // generate struct definition
477 fmt.Fprintf(w, "type %s struct {\n", name)
479 // generate struct fields
480 for i, field := range typ.Fields {
481 // skip internal fields
482 switch strings.ToLower(field.Name) {
483 case crcField, msgIdField:
487 generateField(ctx, w, typ.Fields, i)
490 // generate end of the struct
493 // generate name getter
494 generateTypeNameGetter(w, name, typ.Name)
496 // generate CRC getter
497 generateCrcGetter(w, name, typ.CRC)
502 // generateMessage writes generated code for the message into w
503 func generateMessage(ctx *context, w io.Writer, msg *Message) {
504 name := camelCaseName(msg.Name)
506 logf(" writing message %q (%s) with %d fields", msg.Name, name, len(msg.Fields))
508 // generate struct comment
509 generateComment(ctx, w, name, msg.Name, "message")
511 // generate struct definition
512 fmt.Fprintf(w, "type %s struct {", name)
514 msgType := otherMessage
515 wasClientIndex := false
517 // generate struct fields
519 for i, field := range msg.Fields {
521 if field.Name == clientIndexField {
522 // "client_index" as the second member,
523 // this might be an event message or a request
524 msgType = eventMessage
525 wasClientIndex = true
526 } else if field.Name == contextField {
527 // reply needs "context" as the second member
528 msgType = replyMessage
531 if wasClientIndex && field.Name == contextField {
532 // request needs "client_index" as the second member
533 // and "context" as the third member
534 msgType = requestMessage
538 // skip internal fields
539 switch strings.ToLower(field.Name) {
540 case crcField, msgIdField:
542 case clientIndexField, contextField:
552 generateField(ctx, w, msg.Fields, i)
555 // generate end of the struct
558 // generate name getter
559 generateMessageNameGetter(w, name, msg.Name)
561 // generate CRC getter
562 generateCrcGetter(w, name, msg.CRC)
564 // generate message type getter method
565 generateMessageTypeGetter(w, name, msgType)
570 // generateField writes generated code for the field into w
571 func generateField(ctx *context, w io.Writer, fields []Field, i int) {
574 fieldName := strings.TrimPrefix(field.Name, "_")
575 fieldName = camelCaseName(fieldName)
577 // generate length field for strings
578 if field.Type == "string" {
579 fmt.Fprintf(w, "\tXXX_%sLen uint32 `struc:\"sizeof=%s\"`\n", fieldName, fieldName)
582 dataType := convertToGoType(ctx, field.Type)
583 fieldType := dataType
585 // check if it is array
586 if field.Length > 0 || field.SizeFrom != "" {
587 if dataType == "uint8" {
590 fieldType = "[]" + dataType
592 fmt.Fprintf(w, "\t%s %s", fieldName, fieldType)
594 if field.Length > 0 {
596 fmt.Fprintf(w, "\t`struc:\"[%d]%s\"`", field.Length, dataType)
598 for _, f := range fields {
599 if f.SizeFrom == field.Name {
600 // variable sized array
601 sizeOfName := camelCaseName(f.Name)
602 fmt.Fprintf(w, "\t`struc:\"sizeof=%s\"`", sizeOfName)
610 // generateMessageNameGetter generates getter for original VPP message name into the provider writer
611 func generateMessageNameGetter(w io.Writer, structName, msgName string) {
612 fmt.Fprintf(w, `func (*%s) GetMessageName() string {
615 `, structName, msgName)
618 // generateTypeNameGetter generates getter for original VPP type name into the provider writer
619 func generateTypeNameGetter(w io.Writer, structName, msgName string) {
620 fmt.Fprintf(w, `func (*%s) GetTypeName() string {
623 `, structName, msgName)
626 // generateCrcGetter generates getter for CRC checksum of the message definition into the provider writer
627 func generateCrcGetter(w io.Writer, structName, crc string) {
628 crc = strings.TrimPrefix(crc, "0x")
629 fmt.Fprintf(w, `func (*%s) GetCrcString() string {
635 // generateMessageTypeGetter generates message factory for the generated message into the provider writer
636 func generateMessageTypeGetter(w io.Writer, structName string, msgType MessageType) {
637 fmt.Fprintln(w, "func (*"+structName+") GetMessageType() api.MessageType {")
638 if msgType == requestMessage {
639 fmt.Fprintln(w, "\treturn api.RequestMessage")
640 } else if msgType == replyMessage {
641 fmt.Fprintln(w, "\treturn api.ReplyMessage")
642 } else if msgType == eventMessage {
643 fmt.Fprintln(w, "\treturn api.EventMessage")
645 fmt.Fprintln(w, "\treturn api.OtherMessage")