-
-// 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 {
- // basic types
- typ = t
- } else if r, ok := ctx.packageData.RefMap[binapiType]; ok {
- // specific types (enums/types/unions)
- typ = camelCaseName(r)
- } else {
- 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
-}
-
-func getSizeOfType(typ *Type) (size int) {
- for _, field := range typ.Fields {
- size += getSizeOfBinapiTypeLength(field.Type, field.Length)
- }
- return size
-}
-
-func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
- if n := getBinapiTypeSize(typ); n > 0 {
- if length > 0 {
- return n * length
- } else {
- return n
- }
- }
- return
-}
-
-func getTypeByRef(ctx *context, ref string) *Type {
- for _, typ := range ctx.packageData.Types {
- if ref == toApiType(typ.Name) {
- return &typ
- }
- }
- return nil
-}
-
-func getAliasByRef(ctx *context, ref string) *Alias {
- for _, alias := range ctx.packageData.Aliases {
- if ref == toApiType(alias.Name) {
- return &alias
- }
- }
- return nil
-}
-
-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 {
- maxSize = size
- }
- continue
- }
- alias := getAliasByRef(ctx, field.Type)
- if alias != nil {
- if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
- maxSize = size
- }
- continue
- } else {
- logf("no type or alias found for union %s field type %q", union.Name, field.Type)
- continue
- }
- }
- return
-}