Skip to content

Commit eb8d94d

Browse files
committed
1 parent 40cac71 commit eb8d94d

File tree

3 files changed

+41
-10
lines changed

3 files changed

+41
-10
lines changed

internal/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,

internal/result.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -312,16 +312,16 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
312312
return qs, nil
313313
}
314314

315+
var cmdReturnsData = map[string]struct{}{
316+
metadata.CmdBatchMany: {},
317+
metadata.CmdBatchOne: {},
318+
metadata.CmdMany: {},
319+
metadata.CmdOne: {},
320+
}
321+
315322
func putOutColumns(query *plugin.Query) bool {
316-
if len(query.Columns) > 0 {
317-
return true
318-
}
319-
for _, allowed := range []string{metadata.CmdMany, metadata.CmdOne, metadata.CmdBatchMany} {
320-
if query.Cmd == allowed {
321-
return true
322-
}
323-
}
324-
return false
323+
_, found := cmdReturnsData[query.Cmd]
324+
return found
325325
}
326326

327327
// It's possible that this method will generate duplicate JSON tag values

internal/result_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func TestPutOutColumns_ForZeroColumns(t *testing.T) {
5050
},
5151
{
5252
cmd: metadata.CmdBatchOne,
53-
want: false,
53+
want: true,
5454
},
5555
}
5656
for _, tc := range tests {

0 commit comments

Comments
 (0)