Skip to content

Proposal: Add option to emit //go:generate annotation #3124

Closed

Description

@ktogo

What do you want to change?

I would like sqlc to provide an option to emitting //go:generate annotation on generated models file in order to let us auto-generate the extended codes using go generate command.

Proposal

I would like to add some option like go_generate_annotations in config that lead sqlc to emit go:generate annotation.

sql:
  - gen:
      go:
        go_generate_annotations:
          - generator: go-genaccessor
            emit_into_models_file: true

Expected result in models.go:

//go:generate go-genaccessor

Background

I was firstly looking for the way to add getter/setter methods to the sqlc-generated models, and found that there is no straightforward option to achieve, nor any plan in future to support it. Instead, there have been some comments to the following issues that encouraging the use of any another tool to extend the code.

Golang already provides an official go generate subcommand (https://go.dev/blog/generate) that let us autogenerate the code, but that command requires //go:generate annotation on the subjected file in order to tell the command which file should be subjected and which plugin should be used for autogeneration.

Goal

I confirmed that I was able to add extra struct tags to the columns of the model struct can be achieved with following config:

sql:
  - gen:
      go:
        overrides:
          - column: items.id
            go_struct_tag: 'getter:"GetID" setter:"SetID"'

Which results to the following code, which seems perfect.

type Item struct {
	ID uuid.UUID `getter:"GetID" setter:"SetID"`
}

However that file doesn't obviously provide the go:generate annotation and I couldn't achieve the generation of getters and setters"

What database engines need to be changed?

No response

What programming language backends need to be changed?

Go

Activity

  1. added
    enhancementNew feature or request
    triageNew issues that hasn't been reviewed
    on Jan 10, 2024
  2. kyleconroy commented on Jan 11, 2024

    @kyleconroy
    Collaborator

    Would you get the same behavior if you created an empty Go file in the same directory that had the go generate directive and nothing else?

    package db
    
    //go:generate go-genaccessor
    

    If so, that feels like a more configurable option.

  3. ktogo commented on Jan 11, 2024

    @ktogo
    Author

    Ah I see. That will surely work! Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requesttriageNew issues that hasn't been reviewed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions