diff --git a/modelgen/table.go b/modelgen/table.go index 325f7373..1042cd0e 100644 --- a/modelgen/table.go +++ b/modelgen/table.go @@ -22,18 +22,17 @@ const BASE_TABLE_TEMPLATE = `{{ template "header" . }} package {{ index . "PackageName" }} -{{ if .Enums }} +{{ if index . "Enums" }} type ( -{{ range .Enums }} -{{ $e := . }} -{{ $e.Alias }} {{ $e.Type }} +{{ range index . "Enums" }} +{{ .Alias }} {{ .Type }} {{- end }} ) const ( -{{ range .Enums }} -{{ $e := . }} -{{ range .Sets }} +{{ range index . "Enums" }} +{{- $e := . }} +{{- range .Sets }} {{ $e.Alias }}{{ camelCase . }} {{ $e.Alias }} = {{ printVal . $e.Type }} {{- end }} {{- end }} @@ -181,17 +180,22 @@ func StructName(tableName string) string { return strings.Title(strings.ReplaceAll(tableName, "_", "")) } +// EnumName returns the name of the enum field +func EnumName(tableName, columnName string) string { + return strings.Title(StructName(tableName)) + camelCase(columnName) +} + // FieldType returns the string representation of a column type func FieldType(tableName, columnName string, column *ovsdb.ColumnSchema) string { switch column.Type { case ovsdb.TypeEnum: - return strings.Title(StructName(tableName)) + camelCase(columnName) + return EnumName(tableName, columnName) case ovsdb.TypeMap: return fmt.Sprintf("map[%s]%s", AtomicType(column.TypeObj.Key.Type), AtomicType(column.TypeObj.Value.Type)) case ovsdb.TypeSet: if FieldEnum(tableName, columnName, column) != nil { - return fmt.Sprintf("[]%s%s", strings.Title(StructName(tableName)), camelCase(columnName)) + return fmt.Sprintf("[]%s", EnumName(tableName, columnName)) } return fmt.Sprintf("[]%s", AtomicType(column.TypeObj.Key.Type)) default: @@ -199,6 +203,7 @@ func FieldType(tableName, columnName string, column *ovsdb.ColumnSchema) string } } +// FieldEnum returns the Enum if the column is an enum type func FieldEnum(tableName, columnName string, column *ovsdb.ColumnSchema) *Enum { if column.TypeObj.Key.Enum == nil { return nil diff --git a/modelgen/table_test.go b/modelgen/table_test.go index 7f209853..7aa08221 100644 --- a/modelgen/table_test.go +++ b/modelgen/table_test.go @@ -54,16 +54,14 @@ package test type ( AtomicTableEventType string - - AtomicTableProtocol string + AtomicTableProtocol string ) const ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" - - AtomicTableProtocolTCP AtomicTableProtocol = "tcp" - AtomicTableProtocolUDP AtomicTableProtocol = "udp" - AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" + AtomicTableProtocolTCP AtomicTableProtocol = "tcp" + AtomicTableProtocolUDP AtomicTableProtocol = "udp" + AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" ) // AtomicTable defines an object in atomicTable table @@ -94,16 +92,14 @@ package test type ( AtomicTableEventType string - - AtomicTableProtocol string + AtomicTableProtocol string ) const ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" - - AtomicTableProtocolTCP AtomicTableProtocol = "tcp" - AtomicTableProtocolUDP AtomicTableProtocol = "udp" - AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" + AtomicTableProtocolTCP AtomicTableProtocol = "tcp" + AtomicTableProtocolUDP AtomicTableProtocol = "udp" + AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" ) // AtomicTable defines an object in atomicTable table @@ -145,16 +141,14 @@ package test type ( AtomicTableEventType string - - AtomicTableProtocol string + AtomicTableProtocol string ) const ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" - - AtomicTableProtocolTCP AtomicTableProtocol = "tcp" - AtomicTableProtocolUDP AtomicTableProtocol = "udp" - AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" + AtomicTableProtocolTCP AtomicTableProtocol = "tcp" + AtomicTableProtocolUDP AtomicTableProtocol = "udp" + AtomicTableProtocolSCTP AtomicTableProtocol = "sctp" ) // AtomicTable defines an object in atomicTable table