Skip to content

Commit

Permalink
Merge pull request #155 from amorenoz/fix_import
Browse files Browse the repository at this point in the history
Make enum expansion optional
  • Loading branch information
amorenoz authored Jun 11, 2021
2 parents 4039563 + 92f2694 commit 87c3031
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 45 deletions.
103 changes: 66 additions & 37 deletions modelgen/table.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

38 changes: 30 additions & 8 deletions modelgen/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestNewTableTemplate(t *testing.T) {

test := []struct {
name string
extend func(tmpl *template.Template, data map[string]interface{})
extend func(tmpl *template.Template, data TableTemplateData)
expected string
err bool
formatErr bool
Expand Down Expand Up @@ -74,13 +74,35 @@ type AtomicTable struct {
Protocol []AtomicTableProtocol ` + "`" + `ovsdb:"protocol"` + "`" + `
Str string ` + "`" + `ovsdb:"str"` + "`" + `
}
`,
},
{
name: "no enums",
extend: func(tmpl *template.Template, data TableTemplateData) {
data.WithEnumTypes(false)
},
expected: `// Code generated by "libovsdb.modelgen"
// DO NOT EDIT.
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"` + "`" + `
}
`,
},
{
name: "add fields using same data",
extend: func(tmpl *template.Template, data map[string]interface{}) {
extra := `{{ define "extraFields" }} {{ range index . "Fields" }} Other{{ .Name }} {{ .Type }}
{{end}} {{end}}`
extend: func(tmpl *template.Template, data TableTemplateData) {
extra := `{{ define "extraFields" }} {{- $tableName := index . "TableName" }} {{ range $field := index . "Fields" }} Other{{ FieldName $field.Column }} {{ FieldType $tableName $field.Column $field.Schema }}
{{ end }}
{{- end }}`
_, err := tmpl.Parse(extra)
if err != nil {
panic(err)
Expand Down Expand Up @@ -113,17 +135,17 @@ type AtomicTable struct {
Str string ` + "`" + `ovsdb:"str"` + "`" + `
OtherUUID string
OtherEventType AtomicTableEventType
OtherEventType string
OtherFloat float64
OtherInt int
OtherProtocol []AtomicTableProtocol
OtherProtocol []string
OtherStr string
}
`,
},
{
name: "add extra functions using extra data",
extend: func(tmpl *template.Template, data map[string]interface{}) {
extend: func(tmpl *template.Template, data TableTemplateData) {
extra := `{{ define "postStructDefinitions" }}
func {{ index . "TestName" }} () string {
return "{{ index . "StructName" }}"
Expand Down Expand Up @@ -170,7 +192,7 @@ func TestFunc() string {
{
name: "add bad code",
formatErr: true,
extend: func(tmpl *template.Template, data map[string]interface{}) {
extend: func(tmpl *template.Template, data TableTemplateData) {
extra := `{{ define "preStructDefinitions" }}
WRONG FORMAT
{{ end }}
Expand Down

0 comments on commit 87c3031

Please sign in to comment.