Skip to content

Commit d5c5302

Browse files
fix: Return an error instead of generating duplicate Go names (#2962)
* fix: Return an error instead of generating duplicate Go names Resolves #588 * Improve error messages --------- Co-authored-by: Kyle Conroy <[email protected]>
1 parent 64313a6 commit d5c5302

File tree

13 files changed

+75
-0
lines changed

13 files changed

+75
-0
lines changed

internal/codegen/golang/gen.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,40 @@ func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.Generat
124124
enums, structs = filterUnusedStructs(enums, structs, queries)
125125
}
126126

127+
if err := validate(options, enums, structs, queries); err != nil {
128+
return nil, err
129+
}
130+
127131
return generate(req, options, enums, structs, queries)
128132
}
129133

134+
func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Query) error {
135+
enumNames := make(map[string]struct{})
136+
for _, enum := range enums {
137+
enumNames[enum.Name] = struct{}{}
138+
enumNames["Null"+enum.Name] = struct{}{}
139+
}
140+
structNames := make(map[string]struct{})
141+
for _, struckt := range structs {
142+
if _, ok := enumNames[struckt.Name]; ok {
143+
return fmt.Errorf("struct name conflicts with enum name: %s", struckt.Name)
144+
}
145+
structNames[struckt.Name] = struct{}{}
146+
}
147+
if !options.EmitExportedQueries {
148+
return nil
149+
}
150+
for _, query := range queries {
151+
if _, ok := enumNames[query.ConstantName]; ok {
152+
return fmt.Errorf("query constant name conflicts with enum name: %s", query.ConstantName)
153+
}
154+
if _, ok := structNames[query.ConstantName]; ok {
155+
return fmt.Errorf("query constant name conflicts with struct name: %s", query.ConstantName)
156+
}
157+
}
158+
return nil
159+
}
160+
130161
func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) {
131162
i := &importer{
132163
Options: options,
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: GetFoos :many
2+
SELECT * FROM foos;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TYPE foo AS ENUM ();
2+
3+
CREATE TABLE foos ();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "2"
2+
sql:
3+
- schema: "schema.sql"
4+
queries: "query.sql"
5+
engine: "postgresql"
6+
gen:
7+
go:
8+
out: "db"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package
2+
error generating code: struct name conflicts with enum name: Foo
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: GetFoos :many
2+
SELECT * FROM null_foos;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
CREATE TYPE foo AS ENUM ();
2+
3+
CREATE TABLE null_foos ();
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
version: "2"
2+
sql:
3+
- schema: "schema.sql"
4+
queries: "query.sql"
5+
engine: "postgresql"
6+
gen:
7+
go:
8+
out: "db"
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# package
2+
error generating code: struct name conflicts with enum name: NullFoo
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
-- name: Foo :one
2+
SELECT 1;

0 commit comments

Comments
 (0)