Skip to content

Add emit_pointer option for Go generator #952

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions internal/codegen/golang/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,19 @@ import (
type Querier interface {
{{- range .GoQueries}}
{{- if eq .Cmd ":one"}}
{{- if $.EmitPointer}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (*{{.Ret.Type}}, error)
{{- else}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Type}}, error)
{{- end}}
{{- end}}
{{- if eq .Cmd ":many"}}
{{- if $.EmitPointer}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]*{{.Ret.Type}}, error)
{{- else}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error)
{{- end}}
{{- end}}
{{- if eq .Cmd ":exec"}}
{{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) error
{{- end}}
Expand Down Expand Up @@ -258,22 +266,34 @@ type {{.Ret.Type}} struct { {{- range .Ret.Struct.Fields}}
{{if eq .Cmd ":one"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- if $.EmitPointer}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) (*{{.Ret.Type}}, error) {
{{- else}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ({{.Ret.Type}}, error) {
{{- end}}
{{- if $.EmitPreparedQueries}}
row := q.queryRow(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else}}
row := q.db.QueryRowContext(ctx, {{.ConstantName}}, {{.Arg.Params}})
{{- end}}
var {{.Ret.Name}} {{.Ret.Type}}
err := row.Scan({{.Ret.Scan}})
{{- if $.EmitPointer}}
return &{{.Ret.Name}}, err
{{- else}}
return {{.Ret.Name}}, err
{{- end}}
}
{{end}}

{{if eq .Cmd ":many"}}
{{range .Comments}}//{{.}}
{{end -}}
{{- if $.EmitPointer}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]*{{.Ret.Type}}, error) {
{{- else}}
func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.Type}}, error) {
{{- end}}
{{- if $.EmitPreparedQueries}}
rows, err := q.query(ctx, q.{{.FieldName}}, {{.ConstantName}}, {{.Arg.Params}})
{{- else}}
Expand All @@ -284,16 +304,28 @@ func (q *Queries) {{.MethodName}}(ctx context.Context, {{.Arg.Pair}}) ([]{{.Ret.
}
defer rows.Close()
{{- if $.EmitEmptySlices}}
{{- if $.EmitPointer}}
items := []*{{.Ret.Type}}{}
{{- else}}
items := []{{.Ret.Type}}{}
{{- end}}
{{else}}
{{- if $.EmitPointer}}
var items []*{{.Ret.Type}}
{{- else}}
var items []{{.Ret.Type}}
{{- end}}
{{end -}}
for rows.Next() {
var {{.Ret.Name}} {{.Ret.Type}}
if err := rows.Scan({{.Ret.Scan}}); err != nil {
return nil, err
}
{{- if $.EmitPointer}}
items = append(items, &{{.Ret.Name}})
{{else}}
items = append(items, {{.Ret.Name}})
{{end -}}
}
if err := rows.Close(); err != nil {
return nil, err
Expand Down Expand Up @@ -366,6 +398,7 @@ type tmplCtx struct {
EmitPreparedQueries bool
EmitInterface bool
EmitEmptySlices bool
EmitPointer bool
}

func (t *tmplCtx) OutputQuery(sourceName string) bool {
Expand Down Expand Up @@ -404,6 +437,7 @@ func generate(settings config.CombinedSettings, enums []Enum, structs []Struct,
EmitDBTags: golang.EmitDBTags,
EmitPreparedQueries: golang.EmitPreparedQueries,
EmitEmptySlices: golang.EmitEmptySlices,
EmitPointer: golang.EmitPointer,
Q: "`",
Package: golang.Package,
GoQueries: queries,
Expand Down
1 change: 1 addition & 0 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ type SQLGo struct {
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitPointer bool `json:"emit_pointer,omitempty" yaml:"emit_pointer"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Package string `json:"package" yaml:"package"`
Out string `json:"out" yaml:"out"`
Expand Down
2 changes: 2 additions & 0 deletions internal/config/v_one.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type v1PackageSettings struct {
EmitPreparedQueries bool `json:"emit_prepared_queries" yaml:"emit_prepared_queries"`
EmitExactTableNames bool `json:"emit_exact_table_names,omitempty" yaml:"emit_exact_table_names"`
EmitEmptySlices bool `json:"emit_empty_slices,omitempty" yaml:"emit_empty_slices"`
EmitPointer bool `json:"emit_pointer,omitempty" yaml:"emit_pointer"`
JSONTagsCaseStyle string `json:"json_tags_case_style,omitempty" yaml:"json_tags_case_style"`
Overrides []Override `json:"overrides" yaml:"overrides"`
}
Expand Down Expand Up @@ -110,6 +111,7 @@ func (c *V1GenerateSettings) Translate() Config {
EmitPreparedQueries: pkg.EmitPreparedQueries,
EmitExactTableNames: pkg.EmitExactTableNames,
EmitEmptySlices: pkg.EmitEmptySlices,
EmitPointer: pkg.EmitPointer,
Package: pkg.Name,
Out: pkg.Path,
Overrides: pkg.Overrides,
Expand Down