1 // Copyright (c) 2020 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.
20 "github.com/sirupsen/logrus"
25 defineApiPrefix = "vl_api_"
26 defineApiSuffix = "_t"
29 // BaseType represents base types in VPP binary API.
33 U8 BaseType = iota + 1
47 BaseTypes = map[BaseType]string{
60 BaseTypeNames = map[string]BaseType{
75 var BaseTypeSizes = map[BaseType]int{
111 // toApiType returns name that is used as type reference in VPP binary API
112 func toApiType(name string) string {
113 return defineApiPrefix + name + defineApiSuffix
116 func fromApiType(typ string) string {
118 name = strings.TrimPrefix(name, defineApiPrefix)
119 name = strings.TrimSuffix(name, defineApiSuffix)
123 func getSizeOfType(module *File, typ *Struct) (size int) {
124 for _, field := range typ.Fields {
125 enum := getEnumByRef(module, field.Type)
127 size += getSizeOfBinapiTypeLength(enum.Type, field.Length)
130 size += getSizeOfBinapiTypeLength(field.Type, field.Length)
135 func getEnumByRef(file *File, ref string) *Enum {
136 for _, typ := range file.Enums {
137 if ref == toApiType(typ.Name) {
144 func getTypeByRef(file *File, ref string) *Struct {
145 for _, typ := range file.Structs {
146 if ref == toApiType(typ.Name) {
153 func getAliasByRef(file *File, ref string) *Alias {
154 for _, alias := range file.Aliases {
155 if ref == toApiType(alias.Name) {
162 func getUnionByRef(file *File, ref string) *Union {
163 for _, union := range file.Unions {
164 if ref == toApiType(union.Name) {
171 func getBinapiTypeSize(binapiType string) (size int) {
172 typName := BaseTypeNames[binapiType]
173 return BaseTypeSizes[typName]
176 // binapiTypes is a set of types used VPP binary API for translation to Go types
177 var binapiTypes = map[string]string{
188 var BaseTypesGo = map[BaseType]string{
202 func getActualType(file *File, typ string) (actual string) {
203 for _, enum := range file.Enums {
204 if enum.GoName == typ {
208 for _, alias := range file.Aliases {
209 if alias.GoName == typ {
216 // convertToGoType translates the VPP binary API type into Go type
217 func convertToGoType(file *File, binapiType string) (typ string) {
218 if t, ok := binapiTypes[binapiType]; ok {
221 } else if r, ok := file.refmap[binapiType]; ok {
222 // specific types (enums/types/unions)
223 typ = camelCaseName(r)
226 case "bool", "string":
230 logrus.Warnf("found unknown VPP binary API type %q, using byte", binapiType)
237 func getSizeOfBinapiTypeLength(typ string, length int) (size int) {
238 if n := getBinapiTypeSize(typ); n > 0 {
249 func getUnionSize(file *File, union *Union) (maxSize int) {
250 for _, field := range union.Fields {
251 typ := getTypeByRef(file, field.Type)
253 if size := getSizeOfType(file, typ); size > maxSize {
258 alias := getAliasByRef(file, field.Type)
260 if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
265 logf("no type or alias found for union %s field type %q", union.Name, field.Type)
269 logf("getUnionSize: %s %+v max=%v", union.Name, union.Fields, maxSize)