Recognize stat_dir_type_empty
[govpp.git] / binapigen / generate.go
index 679dd54..a2f941a 100644 (file)
@@ -55,7 +55,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()
 
@@ -176,7 +178,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 +246,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 +287,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 +303,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 +327,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]