summaryrefslogtreecommitdiffstats
path: root/cmd
diff options
context:
space:
mode:
Diffstat (limited to 'cmd')
-rw-r--r--cmd/binapi-generator/definitions.go22
-rw-r--r--cmd/binapi-generator/generate.go81
-rw-r--r--cmd/binapi-generator/parse.go25
3 files changed, 70 insertions, 58 deletions
diff --git a/cmd/binapi-generator/definitions.go b/cmd/binapi-generator/definitions.go
index b8c3393..3ad782f 100644
--- a/cmd/binapi-generator/definitions.go
+++ b/cmd/binapi-generator/definitions.go
@@ -25,8 +25,6 @@ func getBinapiTypeSize(binapiType string) int {
b, err := strconv.Atoi(strings.TrimLeft(binapiType, "uif"))
if err == nil {
return b / 8
- } else {
- return 1
}
}
return -1
@@ -34,17 +32,15 @@ func getBinapiTypeSize(binapiType string) int {
// binapiTypes is a set of types used VPP binary API for translation to Go types
var binapiTypes = map[string]string{
- "bool": "bool",
- "u8": "uint8",
- "i8": "int8",
- "u16": "uint16",
- "i16": "int16",
- "u32": "uint32",
- "i32": "int32",
- "u64": "uint64",
- "i64": "int64",
- "f64": "float64",
- "string": "string",
+ "u8": "uint8",
+ "i8": "int8",
+ "u16": "uint16",
+ "i16": "int16",
+ "u32": "uint32",
+ "i32": "int32",
+ "u64": "uint64",
+ "i64": "int64",
+ "f64": "float64",
}
func usesInitialism(s string) string {
diff --git a/cmd/binapi-generator/generate.go b/cmd/binapi-generator/generate.go
index 22b4af6..e165c42 100644
--- a/cmd/binapi-generator/generate.go
+++ b/cmd/binapi-generator/generate.go
@@ -347,41 +347,6 @@ func generateAlias(ctx *context, w io.Writer, alias *Alias) {
fmt.Fprintln(w)
}
-// generateType writes generated code for the type into w
-func generateType(ctx *context, w io.Writer, typ *Type) {
- name := camelCaseName(typ.Name)
-
- logf(" writing type %q (%s) with %d fields", typ.Name, name, len(typ.Fields))
-
- // generate struct comment
- generateComment(ctx, w, name, typ.Name, "type")
-
- // generate struct definition
- fmt.Fprintf(w, "type %s struct {\n", name)
-
- // generate struct fields
- for i, field := range typ.Fields {
- // skip internal fields
- switch strings.ToLower(field.Name) {
- case "crc", "_vl_msg_id":
- continue
- }
-
- generateField(ctx, w, typ.Fields, i)
- }
-
- // generate end of the struct
- fmt.Fprintln(w, "}")
-
- // generate name getter
- generateTypeNameGetter(w, name, typ.Name)
-
- // generate CRC getter
- generateCrcGetter(w, name, typ.CRC)
-
- fmt.Fprintln(w)
-}
-
// generateUnion writes generated code for the union into w
func generateUnion(ctx *context, w io.Writer, union *Union) {
name := camelCaseName(union.Name)
@@ -466,6 +431,41 @@ func (u *%[1]s) Get%[2]s() (a %[3]s) {
`, structName, getterField, getterStruct)
}
+// generateType writes generated code for the type into w
+func generateType(ctx *context, w io.Writer, typ *Type) {
+ name := camelCaseName(typ.Name)
+
+ logf(" writing type %q (%s) with %d fields", typ.Name, name, len(typ.Fields))
+
+ // generate struct comment
+ generateComment(ctx, w, name, typ.Name, "type")
+
+ // generate struct definition
+ fmt.Fprintf(w, "type %s struct {\n", name)
+
+ // generate struct fields
+ for i, field := range typ.Fields {
+ // skip internal fields
+ switch strings.ToLower(field.Name) {
+ case "crc", "_vl_msg_id":
+ continue
+ }
+
+ generateField(ctx, w, typ.Fields, i)
+ }
+
+ // generate end of the struct
+ fmt.Fprintln(w, "}")
+
+ // generate name getter
+ generateTypeNameGetter(w, name, typ.Name)
+
+ // generate CRC getter
+ generateCrcGetter(w, name, typ.CRC)
+
+ fmt.Fprintln(w)
+}
+
// generateMessage writes generated code for the message into w
func generateMessage(ctx *context, w io.Writer, msg *Message) {
name := camelCaseName(msg.Name)
@@ -486,7 +486,8 @@ func generateMessage(ctx *context, w io.Writer, msg *Message) {
for i, field := range msg.Fields {
if i == 1 {
if field.Name == "client_index" {
- // "client_index" as the second member, this might be an event message or a request
+ // "client_index" as the second member,
+ // this might be an event message or a request
msgType = eventMessage
wasClientIndex = true
} else if field.Name == "context" {
@@ -495,7 +496,8 @@ func generateMessage(ctx *context, w io.Writer, msg *Message) {
}
} else if i == 2 {
if wasClientIndex && field.Name == "context" {
- // request needs "client_index" as the second member and "context" as the third member
+ // request needs "client_index" as the second member
+ // and "context" as the third member
msgType = requestMessage
}
}
@@ -537,6 +539,11 @@ func generateField(ctx *context, w io.Writer, fields []Field, i int) {
fieldName := strings.TrimPrefix(field.Name, "_")
fieldName = camelCaseName(fieldName)
+ // generate length field for strings
+ if field.Type == "string" {
+ fmt.Fprintf(w, "\tXXX_%sLen uint32 `struc:\"sizeof=%s\"`\n", fieldName, fieldName)
+ }
+
dataType := convertToGoType(ctx, field.Type)
fieldType := dataType
diff --git a/cmd/binapi-generator/parse.go b/cmd/binapi-generator/parse.go
index 5dfbe91..5bb3e8e 100644
--- a/cmd/binapi-generator/parse.go
+++ b/cmd/binapi-generator/parse.go
@@ -23,11 +23,6 @@ import (
"github.com/bennyscetbun/jsongo"
)
-// toApiType returns name that is used as type reference in VPP binary API
-func toApiType(name string) string {
- return fmt.Sprintf("vl_api_%s_t", name)
-}
-
// parsePackage parses provided JSON data into objects prepared for code generation
func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
logf(" %s contains: %d services, %d messages, %d types, %d enums, %d unions, %d aliases (version: %s)",
@@ -75,6 +70,10 @@ func parsePackage(ctx *context, jsonRoot *jsongo.JSONNode) (*Package, error) {
pkg.RefMap[toApiType(alias.Name)] = alias.Name
}
}
+ // sort aliases to ensure consistent order
+ sort.Slice(pkg.Aliases, func(i, j int) bool {
+ return pkg.Aliases[i].Name < pkg.Aliases[j].Name
+ })
// parse types
types := jsonRoot.Map("types")
@@ -479,6 +478,11 @@ func parseService(ctx *context, svcName string, svcNode *jsongo.JSONNode) (*Serv
return &svc, nil
}
+// toApiType returns name that is used as type reference in VPP binary API
+func toApiType(name string) string {
+ return fmt.Sprintf("vl_api_%s_t", name)
+}
+
// convertToGoType translates the VPP binary API type into Go type
func convertToGoType(ctx *context, binapiType string) (typ string) {
if t, ok := binapiTypes[binapiType]; ok {
@@ -488,9 +492,14 @@ func convertToGoType(ctx *context, binapiType string) (typ string) {
// specific types (enums/types/unions)
typ = camelCaseName(r)
} else {
- // fallback type
- log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
- typ = "byte"
+ switch binapiType {
+ case "bool", "string":
+ typ = binapiType
+ default:
+ // fallback type
+ log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
+ typ = "byte"
+ }
}
return typ
}