sqlc icon indicating copy to clipboard operation
sqlc copied to clipboard

Support needed for custom tags on `*Params`-like structs

Open aliml92 opened this issue 2 years ago • 1 comments

What do you want to change?

Let's say we have the following generated code to store a new user

const createUser = `-- name: CreateUser :one
INSERT INTO users (
    username,
    email,
    password 
) VALUES (
    $1,
    $2,
    $3
) 
RETURNING id, username, email, password, bio, image, created_at, updated_at
`

type CreateUserParams struct {
	Username string `json:"username"`
	Email    string `json:"email"`
	Password string `json:"password"`
}

func (q *Queries) CreateUser(ctx context.Context, arg CreateUserParams) (User, error) {
	row := q.db.QueryRow(ctx, createUser, arg.Username, arg.Email, arg.Password)
	var i User
	err := row.Scan(
		&i.ID,
		&i.Username,
		&i.Email,
		&i.Password,
		&i.Bio,
		&i.Image,
		&i.CreatedAt,
		&i.UpdatedAt,
	)
	return i, err
}

Sometimes it might be a straightforward way to use CreateUserParams to validate the incoming requests. Is it possible to add custom tags like the following:

type CreateUserParams struct {
	Username string `json:"username" binding:"required"`
	Email    string `json:"email" binding:"required"`
	Password string `json:"password" binding:"required"`
}

I would appreciate any help

What database engines need to be changed?

PostgreSQL

What programming language backends need to be changed?

Go

aliml92 avatar Mar 15 '23 09:03 aliml92

Does anyone have an alternate way to handle this? Right now it seems too easy to add a required field and forget to set it somewhere in the code as there's no way I can see to validate that struct parameters were set to something in code.

jaunt avatar Apr 30 '25 15:04 jaunt