Skip to content

Commit

Permalink
internal/codegen: add Enum.Valid and AllEnumValues
Browse files Browse the repository at this point in the history
This commit adds a Valid method for enum types
and an All...Values function to get all enum values.

This makes it easier to work with enums.

Fixes #1607
  • Loading branch information
josharian committed May 11, 2022
1 parent 52b6bb4 commit 3dcc55e
Show file tree
Hide file tree
Showing 25 changed files with 336 additions and 0 deletions.
12 changes: 12 additions & 0 deletions examples/batch/postgresql/models.go

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

12 changes: 12 additions & 0 deletions examples/booktest/mysql/models.go

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

12 changes: 12 additions & 0 deletions examples/booktest/postgresql/models.go

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

12 changes: 12 additions & 0 deletions examples/ondeck/mysql/models.go

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

12 changes: 12 additions & 0 deletions examples/ondeck/postgresql/models.go

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

12 changes: 12 additions & 0 deletions internal/codegen/golang/templates/template.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,18 @@ func (e *{{.Name}}) Scan(src interface{}) error {
}
return nil
}

func (e {{.Name}}) Valid() bool {
switch e {
case {{ range $idx, $name := .Constants}}{{ if gt $idx 0 }},{{ end }}{{.Name}}{{ end }}:
return true
}
return false
}

func All{{ .Name }}Values() []{{ .Name }} {
return []{{ .Name }}{ {{ range .Constants}}{{ .Name }}, {{ end }} }
}
{{end}}

{{range .Structs}}
Expand Down
12 changes: 12 additions & 0 deletions internal/endtoend/testdata/comment_on/postgresql/pgx/go/models.go

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

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

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

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

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

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

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

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

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

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

12 changes: 12 additions & 0 deletions internal/endtoend/testdata/ddl_comment/postgresql/pgx/go/models.go

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

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

24 changes: 24 additions & 0 deletions internal/endtoend/testdata/ddl_create_enum/mysql/go/models.go

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

Loading

0 comments on commit 3dcc55e

Please sign in to comment.