diff options
Diffstat (limited to 'binapigen/generate.go')
-rw-r--r-- | binapigen/generate.go | 84 |
1 files changed, 64 insertions, 20 deletions
diff --git a/binapigen/generate.go b/binapigen/generate.go index 679dd54..2be33d1 100644 --- a/binapigen/generate.go +++ b/binapigen/generate.go @@ -21,15 +21,15 @@ import ( "strconv" "strings" - "git.fd.io/govpp.git/internal/version" + "go.fd.io/govpp/version" ) // library dependencies const ( strconvPkg = GoImportPath("strconv") - govppApiPkg = GoImportPath("git.fd.io/govpp.git/api") - govppCodecPkg = GoImportPath("git.fd.io/govpp.git/codec") + govppApiPkg = GoImportPath("go.fd.io/govpp/api") + govppCodecPkg = GoImportPath("go.fd.io/govpp/codec") ) // generated names @@ -41,6 +41,19 @@ const ( fieldUnionData = "XXX_UnionData" // name for the union data field ) +// option keys +const ( + msgStatus = "status" + msgDeprecated = "deprecated" + msgInProgress = "in_progress" +) + +// generated option messages +const ( + deprecatedMsg = "the message will be removed in the future versions" + inProgressMsg = "the message form may change in the future versions" +) + func GenerateAPI(gen *Generator, file *File) *GenFile { logf("----------------------------") logf(" Generate API - %s", file.Desc.Name) @@ -55,7 +68,9 @@ func GenerateAPI(gen *Generator, file *File) *GenFile { g.P("// versions:") g.P("// binapi-generator: ", version.Version()) g.P("// VPP: ", g.gen.vppVersion) - g.P("// source: ", g.file.Desc.Path) + if !gen.opts.NoSourcePathInfo { + g.P("// source: ", g.file.Desc.Path) + } } g.P() @@ -75,14 +90,12 @@ func GenerateAPI(gen *Generator, file *File) *GenFile { g.P("const _ = ", govppApiPkg.Ident("GoVppAPIPackageIsVersion"), generatedCodeVersion) g.P() - if !file.isTypesFile() { - g.P("const (") - g.P(apiName, " = ", strconv.Quote(g.file.Desc.Name)) - g.P(apiVersion, " = ", strconv.Quote(g.file.Version)) - g.P(apiCrc, " = ", g.file.Desc.CRC) - g.P(")") - g.P() - } + g.P("const (") + g.P(apiName, " = ", strconv.Quote(g.file.Desc.Name)) + g.P(apiVersion, " = ", strconv.Quote(g.file.Version)) + g.P(apiCrc, " = ", g.file.Desc.CRC) + g.P(")") + g.P() for _, enum := range g.file.Enums { genEnum(g, enum) @@ -143,6 +156,23 @@ func genTypeComment(g *GenFile, goName string, vppName string, objKind string) { g.P("// ", goName, " defines ", objKind, " '", vppName, "'.") } +func genTypeOptionComment(g *GenFile, options map[string]string) { + // all messages for API versions < 1.0.0 are in_progress by default + if msg, ok := options[msgInProgress]; ok || options[msgStatus] == msgInProgress || + len(g.file.Version) > 1 && g.file.Version[0:2] == "0." { + if msg == "" { + msg = inProgressMsg + } + g.P("// InProgress: ", msg) + } + if msg, ok := options[msgDeprecated]; ok || options[msgStatus] == msgDeprecated { + if msg == "" { + msg = deprecatedMsg + } + g.P("// Deprecated: ", msg) + } +} + func genEnum(g *GenFile, enum *Enum) { logf("gen ENUM %s (%s) - %d entries", enum.GoName, enum.Name, len(enum.Entries)) @@ -176,7 +206,7 @@ func genEnum(g *GenFile, enum *Enum) { g.P(")") g.P() - if isEnumFlag(enum) { + if enum.IsFlag || isEnumFlag(enum) { size := BaseTypeSizes[enum.Type] * 8 g.P("func (x ", enum.GoName, ") String() string {") g.P(" s, ok := ", enum.GoName, "_name[", gotype, "(x)]") @@ -244,6 +274,8 @@ func genAlias(g *GenFile, alias *Alias) { genAddressWithPrefixConversion(g, alias.GoName) case "mac_address": genMacAddressConversion(g, alias.GoName) + case "timestamp": + genTimestampConversion(g, alias.GoName) } } @@ -283,8 +315,10 @@ func genUnion(g *GenFile, union *Union) { g.P("type ", union.GoName, " struct {") + // generate field comments + g.P("// ", union.GoName, " can be one of:") for _, field := range union.Fields { - g.P("// ", field.GoName, " *", getFieldType(g, field)) + g.P("// - ", field.GoName, " *", getFieldType(g, field)) } // generate data field @@ -297,22 +331,23 @@ func genUnion(g *GenFile, union *Union) { // generate methods for fields for _, field := range union.Fields { - genUnionFieldMethods(g, union.GoName, field) + genUnionField(g, union, field) } g.P() } -func genUnionFieldMethods(g *GenFile, structName string, field *Field) { - getterStruct := fieldGoType(g, field) +func genUnionField(g *GenFile, union *Union, field *Field) { + fieldType := fieldGoType(g, field) + constructorName := union.GoName + field.GoName // Constructor - g.P("func ", structName, field.GoName, "(a ", getterStruct, ") (u ", structName, ") {") + g.P("func ", constructorName, "(a ", fieldType, ") (u ", union.GoName, ") {") g.P(" u.Set", field.GoName, "(a)") g.P(" return") g.P("}") // Setter - g.P("func (u *", structName, ") Set", field.GoName, "(a ", getterStruct, ") {") + g.P("func (u *", union.GoName, ") Set", field.GoName, "(a ", fieldType, ") {") g.P(" buf := ", govppCodecPkg.Ident("NewBuffer"), "(u.", fieldUnionData, "[:])") encodeField(g, field, "a", func(name string) string { return "a." + name @@ -320,16 +355,24 @@ func genUnionFieldMethods(g *GenFile, structName string, field *Field) { g.P("}") // Getter - g.P("func (u *", structName, ") Get", field.GoName, "() (a ", getterStruct, ") {") + g.P("func (u *", union.GoName, ") Get", field.GoName, "() (a ", fieldType, ") {") g.P(" buf := ", govppCodecPkg.Ident("NewBuffer"), "(u.", fieldUnionData, "[:])") decodeField(g, field, "a", func(name string) string { return "a." + name }, 0) g.P(" return") g.P("}") + g.P() } +func withSuffix(s string, suffix string) string { + if strings.HasSuffix(s, suffix) { + return s + } + return s + suffix +} + func genField(g *GenFile, fields []*Field, i int) { field := fields[i] @@ -438,6 +481,7 @@ func genMessage(g *GenFile, msg *Message) { logf("gen MESSAGE %s (%s) - %d fields", msg.GoName, msg.Name, len(msg.Fields)) genTypeComment(g, msg.GoIdent.GoName, msg.Name, "message") + genTypeOptionComment(g, msg.Options) // generate message definition if len(msg.Fields) == 0 { |