Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancements for binapigen #93

Merged
merged 19 commits into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ BINAPI_GENERATOR = ./bin/binapi-generator

.DEFAULT_GOAL = help

export CGO_ENABLED=0
sknat marked this conversation as resolved.
Show resolved Hide resolved

check-%:
@: $(if $(value $*),,$(error $* is undefined))

Expand Down
6 changes: 6 additions & 0 deletions binapi/ethernet_types/ethernet_types.ba.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions binapi/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ package binapi
//
// go generate ./binapi
//
//go:generate binapi-generator -input-dir=/usr/share/vpp/api -output-dir=. -gen=rpc
//go:generate binapi-generator -input-file=/usr/share/vpp/api/core/vpe.api.json -output-dir=. -gen=http
//go:generate binapi-generator --input=/usr/share/vpp/api --output-dir=. --gen=rpc
//go:generate binapi-generator --output-dir=. --gen=http /usr/share/vpp/api/core/vpe.api.json
79 changes: 62 additions & 17 deletions binapi/ip_types/ip_types.ba.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 28 additions & 47 deletions binapigen/binapigen.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,17 @@ package binapigen
import (
"fmt"
"path"
"sort"
"strconv"
"strings"

"go.fd.io/govpp/binapigen/vppapi"
)

// generatedCodeVersion indicates a version of the generated code.
// It is incremented whenever an incompatibility between the generated code and
// GoVPP api package is introduced; the generated code references
// a constant, api.GoVppAPIPackageIsVersionN (where N is generatedCodeVersion).
const generatedCodeVersion = 2

// file options
const (
optFileVersion = "version"
// OptFileVersion is an option key for a version of file.
OptFileVersion = "version"
)

type File struct {
Expand Down Expand Up @@ -61,7 +57,7 @@ func newFile(gen *Generator, apifile *vppapi.File, packageName GoPackageName, im
GoImportPath: importPath,
}
if apifile.Options != nil {
file.Version = apifile.Options[optFileVersion]
file.Version = apifile.Options[OptFileVersion]
}

file.FilenamePrefix = path.Join(gen.opts.OutputDir, file.Desc.Name)
Expand Down Expand Up @@ -156,11 +152,8 @@ type Enum struct {
func newEnum(gen *Generator, file *File, apitype vppapi.EnumType, isFlag bool) *Enum {
typ := &Enum{
EnumType: apitype,
GoIdent: GoIdent{
GoName: camelCaseName(apitype.Name),
GoImportPath: file.GoImportPath,
},
IsFlag: isFlag,
GoIdent: file.GoImportPath.Ident(camelCaseName(apitype.Name)),
IsFlag: isFlag,
}
gen.enumsByName[typ.Name] = typ
return typ
Expand All @@ -179,10 +172,7 @@ type Alias struct {
func newAlias(gen *Generator, file *File, apitype vppapi.AliasType) *Alias {
typ := &Alias{
AliasType: apitype,
GoIdent: GoIdent{
GoName: camelCaseName(apitype.Name),
GoImportPath: file.GoImportPath,
},
GoIdent: file.GoImportPath.Ident(camelCaseName(apitype.Name)),
}
gen.aliasesByName[typ.Name] = typ
return typ
Expand Down Expand Up @@ -222,10 +212,7 @@ type Struct struct {
func newStruct(gen *Generator, file *File, apitype vppapi.StructType) *Struct {
typ := &Struct{
StructType: apitype,
GoIdent: GoIdent{
GoName: camelCaseName(apitype.Name),
GoImportPath: file.GoImportPath,
},
GoIdent: file.GoImportPath.Ident(camelCaseName(apitype.Name)),
}
gen.structsByName[typ.Name] = typ
for i, fieldType := range apitype.Fields {
Expand Down Expand Up @@ -255,10 +242,7 @@ type Union struct {
func newUnion(gen *Generator, file *File, apitype vppapi.UnionType) *Union {
typ := &Union{
UnionType: apitype,
GoIdent: GoIdent{
GoName: withSuffix(camelCaseName(apitype.Name), "Union"),
GoImportPath: file.GoImportPath,
},
GoIdent: file.GoImportPath.Ident(withSuffix(camelCaseName(apitype.Name), "Union")),
}
gen.unionsByName[typ.Name] = typ
for i, fieldType := range apitype.Fields {
Expand Down Expand Up @@ -569,6 +553,25 @@ func (rpc *RPC) resolveMessages(gen *Generator) error {
return nil
}

type structTags map[string]string

func (tags structTags) String() string {
if len(tags) == 0 {
return ""
}
var keys []string
for k := range tags {
keys = append(keys, k)
}
sort.Strings(keys)
var ss []string
for _, key := range keys {
tag := tags[key]
ss = append(ss, fmt.Sprintf(`%s:%s`, key, strconv.Quote(tag)))
}
return "`" + strings.Join(ss, " ") + "`"
}

// GoIdent is a Go identifier, consisting of a name and import path.
// The name is a single identifier and may not be a dot-qualified selector.
type GoIdent struct {
Expand Down Expand Up @@ -604,25 +607,3 @@ type GoPackageName string
func cleanPackageName(name string) GoPackageName {
return GoPackageName(sanitizedName(name))
}

// baseName returns the last path element of the name, with the last dotted suffix removed.
func baseName(name string) string {
// First, find the last element
if i := strings.LastIndex(name, "/"); i >= 0 {
name = name[i+1:]
}
// Now drop the suffix
if i := strings.LastIndex(name, "."); i >= 0 {
name = name[:i]
}
return name
}

// normalizeImport returns the last path element of the import, with all dotted suffixes removed.
func normalizeImport(imp string) string {
imp = path.Base(imp)
if idx := strings.Index(imp, "."); idx >= 0 {
imp = imp[:idx]
}
return imp
}
Loading