1 // Copyright (c) 2019 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.
23 // toApiType returns name that is used as type reference in VPP binary API
24 func toApiType(name string) string {
25 return fmt.Sprintf("vl_api_%s_t", name)
28 // binapiTypes is a set of types used VPP binary API for translation to Go types
29 var binapiTypes = map[string]string{
41 func getBinapiTypeSize(binapiType string) int {
42 if _, ok := binapiTypes[binapiType]; ok {
43 b, err := strconv.Atoi(strings.TrimLeft(binapiType, "uif"))
51 // convertToGoType translates the VPP binary API type into Go type
52 func convertToGoType(ctx *context, binapiType string) (typ string) {
53 if t, ok := binapiTypes[binapiType]; ok {
56 } else if r, ok := ctx.packageData.RefMap[binapiType]; ok {
57 // specific types (enums/types/unions)
58 typ = camelCaseName(r)
61 case "bool", "string":
65 log.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
72 func getSizeOfType(typ *Type) (size int) {
73 for _, field := range typ.Fields {
74 size += getSizeOfBinapiTypeLength(field.Type, field.Length)
79 func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
80 if n := getBinapiTypeSize(typ); n > 0 {
90 func getTypeByRef(ctx *context, ref string) *Type {
91 for _, typ := range ctx.packageData.Types {
92 if ref == toApiType(typ.Name) {
99 func getAliasByRef(ctx *context, ref string) *Alias {
100 for _, alias := range ctx.packageData.Aliases {
101 if ref == toApiType(alias.Name) {
108 func getUnionSize(ctx *context, union *Union) (maxSize int) {
109 for _, field := range union.Fields {
110 typ := getTypeByRef(ctx, field.Type)
112 if size := getSizeOfType(typ); size > maxSize {
117 alias := getAliasByRef(ctx, field.Type)
119 if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
124 logf("no type or alias found for union %s field type %q", union.Name, field.Type)