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 getSizeOfBinapiTypeLength(typ string, length int) (size int) {
132 if n := BaseTypeSizes[typ]; n > 0 {
142 func getSizeOfType(typ *Struct) (size int) {
143 for _, field := range typ.Fields {
144 if enum := field.TypeEnum; enum != nil {
145 size += getSizeOfBinapiTypeLength(enum.Type, field.Length)
148 size += getSizeOfBinapiTypeLength(field.Type, field.Length)
153 func getUnionSize(union *Union) (maxSize int) {
154 for _, field := range union.Fields {
155 if typ := field.TypeStruct; typ != nil {
156 if size := getSizeOfType(typ); size > maxSize {
161 if alias := field.TypeAlias; alias != nil {
162 if size := getSizeOfBinapiTypeLength(alias.Type, alias.Length); size > maxSize {
167 logrus.Panicf("no type or alias found for union %s field type %q", union.Name, field.Type)
170 //logf("getUnionSize: %s %+v max=%v", union.Name, union.Fields, maxSize)