Optimize socketclient adapter and add various code improvements
[govpp.git] / cmd / binapi-generator / types.go
index 3aa9819..90c890f 100644 (file)
@@ -18,6 +18,8 @@ import (
        "fmt"
        "strconv"
        "strings"
+
+       "github.com/sirupsen/logrus"
 )
 
 // toApiType returns name that is used as type reference in VPP binary API
@@ -62,15 +64,20 @@ func convertToGoType(ctx *context, binapiType string) (typ string) {
                        typ = binapiType
                default:
                        // fallback type
-                       log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
+                       logrus.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
                        typ = "byte"
                }
        }
        return typ
 }
 
-func getSizeOfType(typ *Type) (size int) {
+func getSizeOfType(ctx *context, typ *Type) (size int) {
        for _, field := range typ.Fields {
+               enum := getEnumByRef(ctx, field.Type)
+               if enum != nil {
+                       size += getSizeOfBinapiTypeLength(enum.Type, field.Length)
+                       continue
+               }
                size += getSizeOfBinapiTypeLength(field.Type, field.Length)
        }
        return size
@@ -84,9 +91,19 @@ func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
                        return n
                }
        }
+
        return
 }
 
+func getEnumByRef(ctx *context, ref string) *Enum {
+       for _, typ := range ctx.packageData.Enums {
+               if ref == toApiType(typ.Name) {
+                       return &typ
+               }
+       }
+       return nil
+}
+
 func getTypeByRef(ctx *context, ref string) *Type {
        for _, typ := range ctx.packageData.Types {
                if ref == toApiType(typ.Name) {
@@ -109,7 +126,7 @@ func getUnionSize(ctx *context, union *Union) (maxSize int) {
        for _, field := range union.Fields {
                typ := getTypeByRef(ctx, field.Type)
                if typ != nil {
-                       if size := getSizeOfType(typ); size > maxSize {
+                       if size := getSizeOfType(ctx, typ); size > maxSize {
                                maxSize = size
                        }
                        continue
@@ -125,5 +142,6 @@ func getUnionSize(ctx *context, union *Union) (maxSize int) {
                        continue
                }
        }
+       logf("getUnionSize: %s %+v max=%v", union.Name, union.Fields, maxSize)
        return
 }