Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
kyleconroy committed Nov 22, 2023
1 parent 40cac71 commit eb8d94d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
31 changes: 31 additions & 0 deletions internal/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,40 @@ func Generate(ctx context.Context, req *plugin.GenerateRequest) (*plugin.Generat
enums, structs = filterUnusedStructs(enums, structs, queries)
}

if err := validate(options, enums, structs, queries); err != nil {
return nil, err
}

return generate(req, options, enums, structs, queries)
}

func validate(options *opts.Options, enums []Enum, structs []Struct, queries []Query) error {
enumNames := make(map[string]struct{})
for _, enum := range enums {
enumNames[enum.Name] = struct{}{}
enumNames["Null"+enum.Name] = struct{}{}
}
structNames := make(map[string]struct{})
for _, struckt := range structs {
if _, ok := enumNames[struckt.Name]; ok {
return fmt.Errorf("struct name conflicts with enum name: %s", struckt.Name)
}
structNames[struckt.Name] = struct{}{}
}
if !options.EmitExportedQueries {
return nil
}
for _, query := range queries {
if _, ok := enumNames[query.ConstantName]; ok {
return fmt.Errorf("query constant name conflicts with enum name: %s", query.ConstantName)
}
if _, ok := structNames[query.ConstantName]; ok {
return fmt.Errorf("query constant name conflicts with struct name: %s", query.ConstantName)
}
}
return nil
}

func generate(req *plugin.GenerateRequest, options *opts.Options, enums []Enum, structs []Struct, queries []Query) (*plugin.GenerateResponse, error) {
i := &importer{
Options: options,
Expand Down
18 changes: 9 additions & 9 deletions internal/result.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,16 @@ func buildQueries(req *plugin.GenerateRequest, options *opts.Options, structs []
return qs, nil
}

var cmdReturnsData = map[string]struct{}{
metadata.CmdBatchMany: {},
metadata.CmdBatchOne: {},
metadata.CmdMany: {},
metadata.CmdOne: {},
}

func putOutColumns(query *plugin.Query) bool {
if len(query.Columns) > 0 {
return true
}
for _, allowed := range []string{metadata.CmdMany, metadata.CmdOne, metadata.CmdBatchMany} {
if query.Cmd == allowed {
return true
}
}
return false
_, found := cmdReturnsData[query.Cmd]
return found
}

// It's possible that this method will generate duplicate JSON tag values
Expand Down
2 changes: 1 addition & 1 deletion internal/result_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ func TestPutOutColumns_ForZeroColumns(t *testing.T) {
},
{
cmd: metadata.CmdBatchOne,
want: false,
want: true,
},
}
for _, tc := range tests {
Expand Down

0 comments on commit eb8d94d

Please sign in to comment.