Skip to content

Commit 9c3a985

Browse files
authored
internal/codegen: add Enum.Valid and AllEnumValues (#1613)
1 parent 1b6ba1e commit 9c3a985

File tree

16 files changed

+434
-113
lines changed

16 files changed

+434
-113
lines changed

docs/reference/config.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ packages:
2020
emit_result_struct_pointers: false
2121
emit_params_struct_pointers: false
2222
emit_methods_with_db_argument: false
23+
emit_enum_valid_method: false
24+
emit_all_enum_values: false
2325
json_tags_case_style: "camel"
2426
output_db_file_name: "db.go"
2527
output_models_file_name: "models.go"
@@ -60,6 +62,12 @@ Each package document has the following keys:
6062
- If true, parameters are passed as pointers to structs. Defaults to `false`.
6163
- `emit_methods_with_db_argument`:
6264
- If true, generated methods will accept a DBTX argument instead of storing a DBTX on the `*Queries` struct. Defaults to `false`.
65+
- `emit_enum_valid_method`:
66+
- If true, generate a Valid method on enum types,
67+
indicating whether a string is a valid enum value.
68+
- `emit_all_enum_values`:
69+
- If true, emit a function per enum type
70+
that returns all valid enum values.
6371
- `json_tags_case_style`:
6472
- `camel` for camelCase, `pascal` for PascalCase, `snake` for snake_case or `none` to use the column name in the DB. Defaults to `none`.
6573
- `output_db_file_name`:

internal/cmd/shim.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,8 @@ func pluginGoCode(s config.SQLGo) *plugin.GoCode {
8686
EmitResultStructPointers: s.EmitResultStructPointers,
8787
EmitParamsStructPointers: s.EmitParamsStructPointers,
8888
EmitMethodsWithDbArgument: s.EmitMethodsWithDBArgument,
89+
EmitEnumValidMethod: s.EmitEnumValidMethod,
90+
EmitAllEnumValues: s.EmitAllEnumValues,
8991
JsonTagsCaseStyle: s.JSONTagsCaseStyle,
9092
Package: s.Package,
9193
Out: s.Out,

internal/codegen/golang/gen.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type tmplCtx struct {
3232
EmitInterface bool
3333
EmitEmptySlices bool
3434
EmitMethodsWithDBArgument bool
35+
EmitEnumValidMethod bool
36+
EmitAllEnumValues bool
3537
UsesCopyFrom bool
3638
UsesBatch bool
3739
}
@@ -84,6 +86,8 @@ func generate(req *plugin.CodeGenRequest, enums []Enum, structs []Struct, querie
8486
EmitPreparedQueries: golang.EmitPreparedQueries,
8587
EmitEmptySlices: golang.EmitEmptySlices,
8688
EmitMethodsWithDBArgument: golang.EmitMethodsWithDbArgument,
89+
EmitEnumValidMethod: golang.EmitEnumValidMethod,
90+
EmitAllEnumValues: golang.EmitAllEnumValues,
8791
UsesCopyFrom: usesCopyFrom(queries),
8892
UsesBatch: usesBatch(queries),
8993
SQLPackage: SQLPackageFromString(golang.SqlPackage),

internal/codegen/golang/templates/template.tmpl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,23 @@ func (e *{{.Name}}) Scan(src interface{}) error {
8686
}
8787
return nil
8888
}
89+
90+
{{ if $.EmitEnumValidMethod }}
91+
func (e {{.Name}}) Valid() bool {
92+
switch e {
93+
case {{ range $idx, $name := .Constants }}{{ if ne $idx 0 }},{{ "\n" }}{{ end }}{{ .Name }}{{ end }}:
94+
return true
95+
}
96+
return false
97+
}
98+
{{ end }}
99+
100+
{{ if $.EmitAllEnumValues }}
101+
func All{{ .Name }}Values() []{{ .Name }} {
102+
return []{{ .Name }}{ {{ range .Constants}}{{ "\n" }}{{ .Name }},{{ end }}
103+
}
104+
}
105+
{{ end }}
89106
{{end}}
90107

91108
{{range .Structs}}

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ type SQLGo struct {
128128
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
129129
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
130130
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument,omitempty" yaml:"emit_methods_with_db_argument"`
131+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
132+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
131133
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
132134
Package string `json:"package" yaml:"package"`
133135
Out string `json:"out" yaml:"out"`

internal/config/v_one.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type v1PackageSettings struct {
3232
EmitResultStructPointers bool `json:"emit_result_struct_pointers" yaml:"emit_result_struct_pointers"`
3333
EmitParamsStructPointers bool `json:"emit_params_struct_pointers" yaml:"emit_params_struct_pointers"`
3434
EmitMethodsWithDBArgument bool `json:"emit_methods_with_db_argument" yaml:"emit_methods_with_db_argument"`
35+
EmitEnumValidMethod bool `json:"emit_enum_valid_method,omitempty" yaml:"emit_enum_valid_method"`
36+
EmitAllEnumValues bool `json:"emit_all_enum_values,omitempty" yaml:"emit_all_enum_values"`
3537
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
3638
SQLPackage string `json:"sql_package" yaml:"sql_package"`
3739
Overrides []Override `json:"overrides" yaml:"overrides"`
@@ -126,6 +128,8 @@ func (c *V1GenerateSettings) Translate() Config {
126128
EmitResultStructPointers: pkg.EmitResultStructPointers,
127129
EmitParamsStructPointers: pkg.EmitParamsStructPointers,
128130
EmitMethodsWithDBArgument: pkg.EmitMethodsWithDBArgument,
131+
EmitEnumValidMethod: pkg.EmitEnumValidMethod,
132+
EmitAllEnumValues: pkg.EmitAllEnumValues,
129133
Package: pkg.Name,
130134
Out: pkg.Path,
131135
SQLPackage: pkg.SQLPackage,

internal/endtoend/testdata/codegen_json/gen/codegen_request.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"output_db_file_name": "",
4242
"output_models_file_name": "",
4343
"output_querier_file_name": "",
44-
"output_files_suffix": ""
44+
"output_files_suffix": "",
45+
"emit_enum_valid_method": false,
46+
"emit_all_enum_values": false
4547
},
4648
"json": {
4749
"out": "gen",

internal/endtoend/testdata/emit_enum_valid_and_values/go/db.go

Lines changed: 32 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_enum_valid_and_values/go/models.go

Lines changed: 52 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/endtoend/testdata/emit_enum_valid_and_values/go/query.sql.go

Lines changed: 70 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
CREATE TYPE ip_protocol AS enum ('tcp', 'ip', 'icmp');
2+
3+
CREATE TABLE bar_old (id_old serial not null, ip_old ip_protocol NOT NULL);
4+
5+
-- name: ListFoo :many
6+
SELECT id_old as foo_old, id_old as baz_old
7+
FROM bar_old
8+
WHERE ip_old = $1 AND id_old = $2;
9+
10+
-- name: ListBar :many
11+
SELECT * FROM bar_old;
12+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"version": "1",
3+
"packages": [
4+
{
5+
"path": "go",
6+
"engine": "postgresql",
7+
"sql_package": "pgx/v4",
8+
"name": "querytest",
9+
"schema": "query.sql",
10+
"queries": "query.sql",
11+
"emit_enum_valid_method": true,
12+
"emit_all_enum_values": true
13+
}
14+
],
15+
"rename": {
16+
"id_old": "IDNew",
17+
"bar_old": "BarNew",
18+
"foo_old": "FooNew",
19+
"ip_protocol": "IPProtocol",
20+
"ip_protocol_tcp": "IPProtocolTCP"
21+
}
22+
}

0 commit comments

Comments
 (0)