diff --git a/modelgen/table.go b/modelgen/table.go index d9b2c14e..4122fd9b 100644 --- a/modelgen/table.go +++ b/modelgen/table.go @@ -55,7 +55,7 @@ type ( {{- end }} ) -const ( +var ( {{ range index . "Enums" }} {{- $e := . }} {{- range .Sets }} @@ -167,6 +167,28 @@ func fieldType(tableName, columnName string, column *ovsdb.ColumnSchema, enumTyp return fmt.Sprintf("map[%s]%s", AtomicType(column.TypeObj.Key.Type), AtomicType(column.TypeObj.Value.Type)) case ovsdb.TypeSet: + // optional with max 1 element + if column.TypeObj.Min() == 0 && column.TypeObj.Max() == 1 { + if enumTypes && FieldEnum(tableName, columnName, column) != nil { + return fmt.Sprintf("*%s", enumName(tableName, columnName)) + } + return fmt.Sprintf("*%s", AtomicType(column.TypeObj.Key.Type)) + } + // required, max 1 element + if column.TypeObj.Min() == 1 && column.TypeObj.Max() == 1 { + if enumTypes && FieldEnum(tableName, columnName, column) != nil { + return enumName(tableName, columnName) + } + return AtomicType(column.TypeObj.Key.Type) + } + // use array for columns with max > 1 + if column.TypeObj.Max() > 1 { + if enumTypes && FieldEnum(tableName, columnName, column) != nil { + return fmt.Sprintf("[%d]%s", column.TypeObj.Max(), enumName(tableName, columnName)) + } + return fmt.Sprintf("[%d]%s", column.TypeObj.Max(), AtomicType(column.TypeObj.Key.Type)) + } + // use a slice if enumTypes && FieldEnum(tableName, columnName, column) != nil { return fmt.Sprintf("[]%s", enumName(tableName, columnName)) } diff --git a/modelgen/table_test.go b/modelgen/table_test.go index 15c6684b..7d2982c9 100644 --- a/modelgen/table_test.go +++ b/modelgen/table_test.go @@ -58,7 +58,7 @@ type ( AtomicTableProtocol string ) -const ( +var ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" AtomicTableProtocolTCP AtomicTableProtocol = "tcp" AtomicTableProtocolUDP AtomicTableProtocol = "udp" @@ -67,12 +67,12 @@ const ( // AtomicTable defines an object in atomicTable table type AtomicTable struct { - UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` - EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` - Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` - Int int ` + "`" + `ovsdb:"int"` + "`" + ` - Protocol []AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` - Str string ` + "`" + `ovsdb:"str"` + "`" + ` + UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` + EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` + Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` + Int int ` + "`" + `ovsdb:"int"` + "`" + ` + Protocol *AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` + Str string ` + "`" + `ovsdb:"str"` + "`" + ` } `, }, @@ -88,12 +88,12 @@ package test // AtomicTable defines an object in atomicTable table type AtomicTable struct { - UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` - EventType string ` + "`" + `ovsdb:"event_type"` + "`" + ` - Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` - Int int ` + "`" + `ovsdb:"int"` + "`" + ` - Protocol []string ` + "`" + `ovsdb:"protocol"` + "`" + ` - Str string ` + "`" + `ovsdb:"str"` + "`" + ` + UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` + EventType string ` + "`" + `ovsdb:"event_type"` + "`" + ` + Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` + Int int ` + "`" + `ovsdb:"int"` + "`" + ` + Protocol *string ` + "`" + `ovsdb:"protocol"` + "`" + ` + Str string ` + "`" + `ovsdb:"str"` + "`" + ` } `, }, @@ -118,7 +118,7 @@ type ( AtomicTableProtocol string ) -const ( +var ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" AtomicTableProtocolTCP AtomicTableProtocol = "tcp" AtomicTableProtocolUDP AtomicTableProtocol = "udp" @@ -127,18 +127,18 @@ const ( // AtomicTable defines an object in atomicTable table type AtomicTable struct { - UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` - EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` - Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` - Int int ` + "`" + `ovsdb:"int"` + "`" + ` - Protocol []AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` - Str string ` + "`" + `ovsdb:"str"` + "`" + ` + UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` + EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` + Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` + Int int ` + "`" + `ovsdb:"int"` + "`" + ` + Protocol *AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` + Str string ` + "`" + `ovsdb:"str"` + "`" + ` OtherUUID string OtherEventType string OtherFloat float64 OtherInt int - OtherProtocol []string + OtherProtocol *string OtherStr string } `, @@ -167,7 +167,7 @@ type ( AtomicTableProtocol string ) -const ( +var ( AtomicTableEventTypeEmptyLbBackends AtomicTableEventType = "empty_lb_backends" AtomicTableProtocolTCP AtomicTableProtocol = "tcp" AtomicTableProtocolUDP AtomicTableProtocol = "udp" @@ -176,12 +176,12 @@ const ( // AtomicTable defines an object in atomicTable table type AtomicTable struct { - UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` - EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` - Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` - Int int ` + "`" + `ovsdb:"int"` + "`" + ` - Protocol []AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` - Str string ` + "`" + `ovsdb:"str"` + "`" + ` + UUID string ` + "`" + `ovsdb:"_uuid"` + "`" + ` + EventType AtomicTableEventType ` + "`" + `ovsdb:"event_type"` + "`" + ` + Float float64 ` + "`" + `ovsdb:"float"` + "`" + ` + Int int ` + "`" + `ovsdb:"int"` + "`" + ` + Protocol *AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + ` + Str string ` + "`" + `ovsdb:"str"` + "`" + ` } func TestFunc() string {