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.
21 "github.com/sirupsen/logrus"
26 defineApiPrefix = "vl_api_"
27 defineApiSuffix = "_t"
30 // toApiType returns name that is used as type reference in VPP binary API
31 func toApiType(name string) string {
32 return defineApiPrefix + name + defineApiSuffix
35 func fromApiType(typ string) string {
37 name = strings.TrimPrefix(name, defineApiPrefix)
38 name = strings.TrimSuffix(name, defineApiSuffix)
56 var BaseTypeSizes = map[string]int{
70 var BaseTypesGo = map[string]string{
84 func fieldActualType(field *Field) (actual string) {
86 case field.TypeAlias != nil:
87 actual = field.TypeAlias.Type
88 case field.TypeEnum != nil:
89 actual = field.TypeEnum.Type
96 func fieldGoType(g *GenFile, field *Field) string {
98 case field.TypeAlias != nil:
99 return g.GoIdent(field.TypeAlias.GoIdent)
100 case field.TypeEnum != nil:
101 return g.GoIdent(field.TypeEnum.GoIdent)
102 case field.TypeStruct != nil:
103 return g.GoIdent(field.TypeStruct.GoIdent)
104 case field.TypeUnion != nil:
105 return g.GoIdent(field.TypeUnion.GoIdent)
107 t, ok := BaseTypesGo[field.Type]
109 logrus.Panicf("type %s is not base type", field.Type)
114 func getFieldType(g *GenFile, field *Field) string {
115 gotype := fieldGoType(g, field)
123 if _, ok := BaseTypesGo[field.Type]; !ok && field.Length > 0 {
124 return fmt.Sprintf("[%d]%s", field.Length, gotype)
131 func getUnionSize(union *Union) (maxSize int) {
132 for _, field := range union.Fields {
133 if size, isBaseType := getSizeOfField(field); isBaseType {
134 logrus.Panicf("union %s field %s has unexpected type %q", union.Name, field.Name, field.Type)
135 } else if size > maxSize {
139 //logf("getUnionSize: %s %+v max=%v", union.Name, union.Fields, maxSize)
143 func getSizeOfField(field *Field) (size int, isBaseType bool) {
144 if alias := field.TypeAlias; alias != nil {
145 size = getSizeOfBinapiBaseType(alias.Type, alias.Length)
148 if enum := field.TypeEnum; enum != nil {
149 size = getSizeOfBinapiBaseType(enum.Type, field.Length)
152 if structType := field.TypeStruct; structType != nil {
153 size = getSizeOfStruct(structType)
156 if union := field.TypeUnion; union != nil {
157 size = getUnionSize(union)
163 func getSizeOfStruct(typ *Struct) (size int) {
164 for _, field := range typ.Fields {
165 fieldSize, isBaseType := getSizeOfField(field)
167 size += getSizeOfBinapiBaseType(field.Type, field.Length)
175 // Returns size of base type multiplied by length. Length equal to zero
176 // returns base type size.
177 func getSizeOfBinapiBaseType(typ string, length int) (size int) {
178 if n := BaseTypeSizes[typ]; n > 0 {