Skip to content

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

Closed

Description

@blwsh

What do you want to change?

Selecting all fields and a subset of fields for the same table yields different structs. Currently, we're required to write a mapper function which transforms the row(s) in to an abstraction our applications can understand - however, if sqlc was to provide getters and setters, we'd be able to define interfaces which the results conform to and leave a lot of the heavy lifting to go.

Please see the example below:

CREATE TABLE `products`
(
    `name`  varchar(255) NOT NULL,
    `price` int(11)      NOT NULL
)
// generated models/queries

// 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

For this particular problem extending the template for structs to include getters and setters would be sufficient, however - it may be a better idea to allow the extension of the template via the config file instead?

What database engines need to be changed?

PostgreSQL, MySQL

What programming language backends need to be changed?

Go

Activity

  1. added
    enhancementNew feature or request
    triageNew issues that hasn't been reviewed
    on Jul 15, 2023
  2. changed the title [-]Include getters and setters with models so methods can work with interfaces instead of specific types[/-] [+]Include getters and setters with models so we can work with interfaces instead of specific types[/+] on Jul 15, 2023
  3. changed the title [-]Include getters and setters with models so we can work with interfaces instead of specific types[/-] [+]Include getters and setters with models so we may work with interfaces instead of specific structs[/+] on Jul 16, 2023
  4. kyleconroy commented on Sep 20, 2023

    @kyleconroy
    Collaborator

    Closed via #2471 (comment)

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