From ccb7b913d54fafdf08b36ac7eb36e462b1ecc9eb Mon Sep 17 00:00:00 2001 From: Ondrej Fabry Date: Mon, 9 Sep 2019 09:56:59 +0200 Subject: Fix compatibility with latest master (20.01-rc0) - fixed generator for new string types - update simple client example - regenerate examples binapi for VPP 19.08 Change-Id: If4fe78c130d95641f35f75cd0262b35b032acaf8 Signed-off-by: Ondrej Fabry --- cmd/binapi-generator/generate.go | 17 +++++++++++------ cmd/binapi-generator/objects.go | 14 ++++++++------ cmd/binapi-generator/parse.go | 14 ++++++++++---- 3 files changed, 29 insertions(+), 16 deletions(-) (limited to 'cmd/binapi-generator') diff --git a/cmd/binapi-generator/generate.go b/cmd/binapi-generator/generate.go index e386f8d..cb1f470 100644 --- a/cmd/binapi-generator/generate.go +++ b/cmd/binapi-generator/generate.go @@ -568,26 +568,31 @@ func generateField(ctx *context, w io.Writer, fields []Field, i int) { fieldName := strings.TrimPrefix(field.Name, "_") fieldName = camelCaseName(fieldName) + dataType := convertToGoType(ctx, field.Type) + fieldType := dataType + // generate length field for strings - if field.Type == "string" { + if field.Type == "string" && field.Length == 0 { fmt.Fprintf(w, "\tXXX_%sLen uint32 `struc:\"sizeof=%s\"`\n", fieldName, fieldName) } - dataType := convertToGoType(ctx, field.Type) - fieldType := dataType - // check if it is array if field.Length > 0 || field.SizeFrom != "" { if dataType == "uint8" { dataType = "byte" } - fieldType = "[]" + dataType + if dataType == "string" && field.SpecifiedLen { + fieldType = "string" + dataType = "byte" + } else { + fieldType = "[]" + dataType + } } fmt.Fprintf(w, "\t%s %s", fieldName, fieldType) fieldTags := map[string]string{} - if field.Length > 0 { + if field.Length > 0 && field.SpecifiedLen { // fixed size array fieldTags["struc"] = fmt.Sprintf("[%d]%s", field.Length, dataType) } else { diff --git a/cmd/binapi-generator/objects.go b/cmd/binapi-generator/objects.go index 2d5321d..c4b645d 100644 --- a/cmd/binapi-generator/objects.go +++ b/cmd/binapi-generator/objects.go @@ -54,16 +54,18 @@ type Type struct { // Field represents VPP binary API object field type Field struct { - Name string - Type string - Length int - SizeFrom string - Meta FieldMeta + Name string + Type string + Length int + SpecifiedLen bool + SizeFrom string + Meta FieldMeta } // FieldMeta represents VPP binary API meta info for field type FieldMeta struct { - Limit int + Limit int + Default float64 } // Union represents VPP binary API union diff --git a/cmd/binapi-generator/parse.go b/cmd/binapi-generator/parse.go index 8852ae2..3867dd4 100644 --- a/cmd/binapi-generator/parse.go +++ b/cmd/binapi-generator/parse.go @@ -63,7 +63,8 @@ const ( // field meta info const ( - fieldMetaLimit = "limit" + fieldMetaLimit = "limit" + fieldMetaDefault = "default" ) // module options @@ -448,13 +449,16 @@ func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) { } if field.Len() >= 3 { - if field.At(2).GetType() == jsongo.TypeValue { + switch field.At(2).GetType() { + case jsongo.TypeValue: fieldLength, ok := field.At(2).Get().(float64) if !ok { return nil, fmt.Errorf("field length is %T, not float64", field.At(2).Get()) } f.Length = int(fieldLength) - } else if field.At(2).GetType() == jsongo.TypeMap { + f.SpecifiedLen = true + + case jsongo.TypeMap: fieldMeta := field.At(2) for _, key := range fieldMeta.GetKeys() { @@ -463,11 +467,13 @@ func parseField(ctx *context, field *jsongo.JSONNode) (*Field, error) { switch metaName := key.(string); metaName { case fieldMetaLimit: f.Meta.Limit = int(metaNode.Get().(float64)) + case fieldMetaDefault: + f.Meta.Default = metaNode.Get().(float64) default: logrus.Warnf("unknown meta info (%s) for field (%s)", metaName, fieldName) } } - } else { + default: return nil, errors.New("invalid JSON for field specified") } } -- cgit 1.2.3-korg