Skip to content
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

Emit query method arg params struct #2329

Conversation

jwc-clinnection
Copy link
Contributor

Add configuration option "emit_methods_arg_with_struct" where if true, generated methods will always have struct argument even if number of arguments is 1. Defaults to false.

Example when emit_methods_arg_with_struct: false (default behavior):

const createBlog = `-- name: CreateBlog :one
insert into blog (name)
values ($1)
RETURNING id, name
`

func (q *Queries) CreateBlog(ctx context.Context, name string) (Blog, error) {
	row := q.db.QueryRow(ctx, createBlog, name)
	var i Blog
	err := row.Scan(&i.ID, &i.Name)
	return i, err
}

Example when emit_methods_arg_with_struct: true

const createBlog = `-- name: CreateBlog :one
insert into blog (name)
values ($1)
RETURNING id, name
`

type CreateBlogParams struct {
	Name string `json:"name"`
}

func (q *Queries) CreateBlog(ctx context.Context, arg CreateBlogParams) (Blog, error) {
	row := q.db.QueryRow(ctx, createBlog, arg.Name)
	var i Blog
	err := row.Scan(&i.ID, &i.Name)
	return i, err
}

jwc-clinnection and others added 4 commits June 14, 2023 00:32
If json_tags_id_camelcase is true, "ID" in json tags will be camelcase.
If false, will be uppercase. Defaults to `false`
Add json_tags_id_camelcase configuration option
Add configuration changes to support emit_methods_arg_with_struct
Implement functionality to optionally generate method argument 
struct even when number of arguments is one.
@kyleconroy
Copy link
Collaborator

As usual, thanks for the pull request. This option isn't needed because we already have this functionality using the query_parameter_limit option. Setting this to 0 will always generate a struct.

https://play.sqlc.dev/p/e95df5c9a6b538ce5d55846d9dc0c1e4801521c8c4e987418863a34daeb86e57

@jwc-clinnection
Copy link
Contributor Author

Thank you!

@jwc-clinnection jwc-clinnection deleted the EmitQueryMethodArgParamsStruct branch June 20, 2023 17:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants