Skip to content

feat(generation): Add new emit_getters option (Go Only) - #2471

Closed
blwsh wants to merge 3 commits into
sqlc-dev:mainfrom
blwsh:blwsh/emit_getters
Closed

blwsh wants to merge 3 commits into
sqlc-dev:mainfrom
blwsh:blwsh/emit_getters

Conversation

@blwsh

@blwsh blwsh commented Jul 17, 2023 •

Copy link
Copy Markdown

Includes getters with Go Lang structs so they can be consumed as interfaces. Enabled with the flag emit_getters (false by default). Available in config versions v1 and v2.

Example:

// sqlc generated

// select * from products
type Product struct {
  Name string
  Price int
}
func (p Product) GetName() string { ... }
func (p Product) GetPrice() int { ... }

// select `name` from products
type ProductNameRow struct {
  Name string
}
func (p ProductNameRow) GetName() string { ... }

// app

type ProductWithName interface {
  GetName() string
}

func DoesProductHaveCoolName[T ProductWithName](product T) bool { ... }

DoesProductHaveCoolName(&ProductNameRow{}) // good
DoesProductHaveCoolName(&Product{}) // also good

Resolves #2466

@kyleconroy

Copy link
Copy Markdown
Collaborator

@blwsh Appreciate opening this up. This isn't something we're looking to support at the time. We're hoping to solve this specific issue by allowing you to bring your own models to sqlc, where you can specify getters or any other methods you'd like to have on your structs.

To solve this issue, another sqlc user @Jille has written https://github.com/Jille/go_get_set_generator which will generate Getter methods for you from Go source.

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.

Include getters and setters with models so we may work with interfaces instead of specific structs

2 participants