Skip to content

Commit

Permalink
update for review
Browse files Browse the repository at this point in the history
  • Loading branch information
halfcrazy committed Jun 9, 2021
1 parent 7286faf commit 39faacb
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 27 deletions.
23 changes: 14 additions & 9 deletions modelgen/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down Expand Up @@ -181,24 +180,30 @@ 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:
return AtomicType(column.Type)
}
}

// 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
Expand Down
30 changes: 12 additions & 18 deletions modelgen/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 39faacb

Please sign in to comment.