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.
32 "github.com/bennyscetbun/jsongo"
35 // MessageType represents the type of a VPP message.
39 requestMessage messageType = iota // VPP request message
40 replyMessage // VPP reply message
41 otherMessage // other VPP message
45 apiImportPath = "git.fd.io/govpp.git/api" // import path of the govpp API
46 inputFileExt = ".json" // filename extension of files that should be processed as the input
49 // context is a structure storing details of a particular code generation task
51 inputFile string // file with input JSON data
52 inputBuff *bytes.Buffer // contents of the input file
53 inputLine int // currently processed line in the input file
54 outputFile string // file with output data
55 packageName string // name of the Go package being generated
56 packageDir string // directory where the package source files are located
57 types map[string]string // map of the VPP typedef names to generated Go typedef names
61 inputFile := flag.String("input-file", "", "Input JSON file.")
62 inputDir := flag.String("input-dir", ".", "Input directory with JSON files.")
63 outputDir := flag.String("output-dir", ".", "Output directory where package folders will be generated.")
66 if *inputFile == "" && *inputDir == "" {
67 fmt.Fprintln(os.Stderr, "ERROR: input-file or input-dir must be specified")
73 // process one input file
74 err = generateFromFile(*inputFile, *outputDir)
76 fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", *inputFile, err)
79 // process all files in specified directory
80 files, err := getInputFiles(*inputDir)
82 fmt.Fprintf(os.Stderr, "ERROR: code generation failed: %v\n", err)
84 for _, file := range files {
85 tmpErr = generateFromFile(file, *outputDir)
87 fmt.Fprintf(os.Stderr, "ERROR: code generation from %s failed: %v\n", file, err)
88 err = tmpErr // remember that the error occurred
97 // getInputFiles returns all input files located in specified directory
98 func getInputFiles(inputDir string) ([]string, error) {
99 files, err := ioutil.ReadDir(inputDir)
101 return nil, fmt.Errorf("reading directory %s failed: %v", inputDir, err)
103 res := make([]string, 0)
104 for _, f := range files {
105 if strings.HasSuffix(f.Name(), inputFileExt) {
106 res = append(res, inputDir+"/"+f.Name())
112 // generateFromFile generates Go bindings from one input JSON file
113 func generateFromFile(inputFile, outputDir string) error {
114 ctx, err := getContext(inputFile, outputDir)
119 inputData, err := readFile(inputFile)
123 ctx.inputBuff = bytes.NewBuffer(inputData)
126 jsonRoot, err := parseJSON(inputData)
131 // create output directory
132 err = os.MkdirAll(ctx.packageDir, 0777)
134 return fmt.Errorf("creating output directory %s failed: %v", ctx.packageDir, err)
138 f, err := os.Create(ctx.outputFile)
141 return fmt.Errorf("creating output file %s failed: %v", ctx.outputFile, err)
143 w := bufio.NewWriter(f)
145 // generate Go package code
146 err = generatePackage(ctx, w, jsonRoot)
151 // go format the output file (non-fatal if fails)
152 exec.Command("gofmt", "-w", ctx.outputFile).Run()
157 // getContext returns context details of the code generation task
158 func getContext(inputFile, outputDir string) (*context, error) {
159 if !strings.HasSuffix(inputFile, inputFileExt) {
160 return nil, fmt.Errorf("invalid input file name %s", inputFile)
163 ctx := &context{inputFile: inputFile}
164 inputFileName := filepath.Base(inputFile)
166 ctx.packageName = inputFileName[0:strings.Index(inputFileName, ".")]
167 if ctx.packageName == "interface" {
168 // 'interface' cannot be a package name, it is a go keyword
169 ctx.packageName = "interfaces"
172 ctx.packageDir = outputDir + "/" + ctx.packageName + "/"
173 ctx.outputFile = ctx.packageDir + ctx.packageName + ".go"
178 // readFile reads content of a file into memory
179 func readFile(inputFile string) ([]byte, error) {
181 inputData, err := ioutil.ReadFile(inputFile)
184 return nil, fmt.Errorf("reading data from file failed: %v", err)
187 return inputData, nil
190 // parseJSON parses a JSON data into an in-memory tree
191 func parseJSON(inputData []byte) (*jsongo.JSONNode, error) {
192 root := jsongo.JSONNode{}
194 err := json.Unmarshal(inputData, &root)
196 return nil, fmt.Errorf("JSON unmarshall failed: %v", err)
203 // generatePackage generates Go code of a package from provided JSON
204 func generatePackage(ctx *context, w *bufio.Writer, jsonRoot *jsongo.JSONNode) error {
205 // generate file header
206 generatePackageHeader(ctx, w, jsonRoot)
208 // generate data types
209 ctx.types = make(map[string]string)
210 types := jsonRoot.Map("types")
211 for i := 0; i < types.Len(); i++ {
213 err := generateMessage(ctx, w, typ, true)
220 messages := jsonRoot.Map("messages")
221 for i := 0; i < messages.Len(); i++ {
222 msg := messages.At(i)
223 err := generateMessage(ctx, w, msg, false)
232 return fmt.Errorf("flushing data to %s failed: %v", ctx.outputFile, err)
238 // generateMessage generates Go code of one VPP message encoded in JSON into provided writer
239 func generateMessage(ctx *context, w io.Writer, msg *jsongo.JSONNode, isType bool) error {
240 if msg.Len() == 0 || msg.At(0).GetType() != jsongo.TypeValue {
241 return errors.New("invalid JSON for message specified")
244 msgName, ok := msg.At(0).Get().(string)
246 return fmt.Errorf("invalid JSON for message specified, message name is %T, not a string", msg.At(0).Get())
248 structName := camelCaseName(strings.Title(msgName))
250 // generate struct fields into the slice & determine message type
251 fields := make([]string, 0)
252 msgType := otherMessage
253 for j := 0; j < msg.Len(); j++ {
254 if jsongo.TypeArray == msg.At(j).GetType() {
256 err := processMessageField(ctx, &fields, fld)
260 // determine whether ths is a request / reply / other message
262 fieldName, ok := fld.At(1).Get().(string)
264 if fieldName == "client_index" {
265 msgType = requestMessage
266 } else if fieldName == "context" {
267 msgType = replyMessage
269 msgType = otherMessage
276 // generate struct comment
277 generateMessageComment(ctx, w, structName, msgName, isType)
279 // generate struct header
280 fmt.Fprintln(w, "type", structName, "struct {")
282 // print out the fields
283 for _, field := range fields {
284 fmt.Fprintln(w, field)
287 // generate end of the struct
290 // generate name getter
292 generateTypeNameGetter(w, structName, msgName)
294 generateMessageNameGetter(w, structName, msgName)
297 // generate message type getter method
299 generateMessageTypeGetter(w, structName, msgType)
302 // generate CRC getter
303 crcIf := msg.At(msg.Len() - 1).At("crc").Get()
304 if crc, ok := crcIf.(string); ok {
305 generateCrcGetter(w, structName, crc)
308 // generate message factory
310 generateMessageFactory(w, structName)
313 // if this is a type, save it in the map for later use
315 ctx.types[fmt.Sprintf("vl_api_%s_t", msgName)] = structName
321 // processMessageField process JSON describing one message field into Go code emitted into provided slice of message fields
322 func processMessageField(ctx *context, fields *[]string, fld *jsongo.JSONNode) error {
323 if fld.Len() < 2 || fld.At(0).GetType() != jsongo.TypeValue || fld.At(1).GetType() != jsongo.TypeValue {
324 return errors.New("invalid JSON for message field specified")
326 fieldVppType, ok := fld.At(0).Get().(string)
328 return fmt.Errorf("invalid JSON for message specified, field type is %T, not a string", fld.At(0).Get())
330 fieldName, ok := fld.At(1).Get().(string)
332 return fmt.Errorf("invalid JSON for message specified, field name is %T, not a string", fld.At(1).Get())
335 // skip internal fields
336 fieldNameLower := strings.ToLower(fieldName)
337 if fieldNameLower == "crc" || fieldNameLower == "_vl_msg_id" {
340 if len(*fields) == 0 && (fieldNameLower == "client_index" || fieldNameLower == "context") {
344 fieldName = strings.TrimPrefix(fieldName, "_")
345 fieldName = camelCaseName(strings.Title(fieldName))
351 fieldStr += "\t" + fieldName + " "
354 arraySize = int(fld.At(2).Get().(float64))
358 dataType := translateVppType(ctx, fieldVppType, isArray)
363 // variable sized array
365 // array size is specified by another field
366 arraySizeField := string(fld.At(3).Get().(string))
367 arraySizeField = camelCaseName(strings.Title(arraySizeField))
368 // find & update the field that specifies the array size
369 for i, f := range *fields {
370 if strings.Contains(f, fmt.Sprintf("\t%s ", arraySizeField)) {
371 (*fields)[i] += fmt.Sprintf("\t`struc:\"sizeof=%s\"`", fieldName)
377 fieldStr += fmt.Sprintf("\t`struc:\"[%d]%s\"`", arraySize, dataType)
381 *fields = append(*fields, fieldStr)
385 // generatePackageHeader generates package header into provider writer
386 func generatePackageHeader(ctx *context, w io.Writer, rootNode *jsongo.JSONNode) {
387 fmt.Fprintln(w, "// Package "+ctx.packageName+" represents the VPP binary API of the '"+ctx.packageName+"' VPP module.")
388 fmt.Fprintln(w, "// DO NOT EDIT. Generated from '"+ctx.inputFile+"'")
390 fmt.Fprintln(w, "package "+ctx.packageName)
392 fmt.Fprintln(w, "import \""+apiImportPath+"\"")
395 fmt.Fprintln(w, "// VlApiVersion contains version of the API.")
396 vlAPIVersion := rootNode.Map("vl_api_version")
397 if vlAPIVersion != nil {
398 fmt.Fprintln(w, "const VlAPIVersion = ", vlAPIVersion.Get())
403 // generateMessageComment generates comment for a message into provider writer
404 func generateMessageComment(ctx *context, w io.Writer, structName string, msgName string, isType bool) {
407 fmt.Fprintln(w, "// "+structName+" represents the VPP binary API data type '"+msgName+"'.")
409 fmt.Fprintln(w, "// "+structName+" represents the VPP binary API message '"+msgName+"'.")
412 // print out the source of the generated message - the JSON
415 lineBuff, err := ctx.inputBuff.ReadBytes('\n')
420 line := string(lineBuff)
423 if strings.Contains(line, msgName) {
424 fmt.Fprintf(w, "// Generated from '%s', line %d:\n", ctx.inputFile, ctx.inputLine)
425 fmt.Fprintln(w, "//")
426 fmt.Fprint(w, "//", line)
430 fmt.Fprint(w, "//", line)
431 if len(strings.Trim(line, " ")) < 4 {
432 break // end of the message in JSON
436 fmt.Fprintln(w, "//")
439 // generateMessageNameGetter generates getter for original VPP message name into the provider writer
440 func generateMessageNameGetter(w io.Writer, structName string, msgName string) {
441 fmt.Fprintln(w, "func (*"+structName+") GetMessageName() string {")
442 fmt.Fprintln(w, "\treturn \""+msgName+"\"")
446 // generateTypeNameGetter generates getter for original VPP type name into the provider writer
447 func generateTypeNameGetter(w io.Writer, structName string, msgName string) {
448 fmt.Fprintln(w, "func (*"+structName+") GetTypeName() string {")
449 fmt.Fprintln(w, "\treturn \""+msgName+"\"")
453 // generateMessageTypeGetter generates message factory for the generated message into the provider writer
454 func generateMessageTypeGetter(w io.Writer, structName string, msgType messageType) {
455 fmt.Fprintln(w, "func (*"+structName+") GetMessageType() api.MessageType {")
456 if msgType == requestMessage {
457 fmt.Fprintln(w, "\treturn api.RequestMessage")
458 } else if msgType == replyMessage {
459 fmt.Fprintln(w, "\treturn api.ReplyMessage")
461 fmt.Fprintln(w, "\treturn api.OtherMessage")
466 // generateCrcGetter generates getter for CRC checksum of the message definition into the provider writer
467 func generateCrcGetter(w io.Writer, structName string, crc string) {
468 crc = strings.TrimPrefix(crc, "0x")
469 fmt.Fprintln(w, "func (*"+structName+") GetCrcString() string {")
470 fmt.Fprintln(w, "\treturn \""+crc+"\"")
474 // generateMessageFactory generates message factory for the generated message into the provider writer
475 func generateMessageFactory(w io.Writer, structName string) {
476 fmt.Fprintln(w, "func New"+structName+"() api.Message {")
477 fmt.Fprintln(w, "\treturn &"+structName+"{}")
481 // translateVppType translates the VPP data type into Go data type
482 func translateVppType(ctx *context, vppType string, isArray bool) string {
509 typ, ok := ctx.types[vppType]
514 panic(fmt.Sprintf("Unknown VPP type %s", vppType))
517 // camelCaseName returns correct name identifier (camelCase).
518 func camelCaseName(name string) (should string) {
519 // Fast path for simple cases: "_" and all lowercase.
524 for _, r := range name {
525 if !unicode.IsLower(r) {
534 // Split camelCase at any lower->upper transition, and split on underscores.
535 // Check each word for common initialisms.
536 runes := []rune(name)
537 w, i := 0, 0 // index of start of word, scan
538 for i+1 <= len(runes) {
539 eow := false // whether we hit the end of a word
540 if i+1 == len(runes) {
542 } else if runes[i+1] == '_' {
543 // underscore; shift the remainder forward over any run of underscores
546 for i+n+1 < len(runes) && runes[i+n+1] == '_' {
550 // Leave at most one underscore if the underscore is between two digits
551 if i+n+1 < len(runes) && unicode.IsDigit(runes[i]) && unicode.IsDigit(runes[i+n+1]) {
555 copy(runes[i+1:], runes[i+n+1:])
556 runes = runes[:len(runes)-n]
557 } else if unicode.IsLower(runes[i]) && !unicode.IsLower(runes[i+1]) {
567 word := string(runes[w:i])
568 if u := strings.ToUpper(word); commonInitialisms[u] {
569 // Keep consistent case, which is lowercase only at the start.
570 if w == 0 && unicode.IsLower(runes[w]) {
571 u = strings.ToLower(u)
573 // All the common initialisms are ASCII,
574 // so we can replace the bytes exactly.
575 copy(runes[w:], []rune(u))
576 } else if w > 0 && strings.ToLower(word) == word {
577 // already all lowercase, and not the first word, so uppercase the first character.
578 runes[w] = unicode.ToUpper(runes[w])
585 // commonInitialisms is a set of common initialisms that need to stay in upper case.
586 var commonInitialisms = map[string]bool{