Skip to content

Support emitting accessor methods to enable Go interfaces? #2746

Closed

Description

@evankanderson

What do you want to change?

It would sometimes be handy to be able to handle multiple database rows with common field names using some Go code which implements generic operations (for example, checking that a user token contains a group claim that matches the group_id of some database object).

It would be handy in these cases if the objects all implemented a GetGroupID() method, which could be used to match the types to an interface. For example:

type List struct {
  ID int64 `json:"id"`
  GroupID int64 `json:"group_id"`
}

func (l List) GetID() int64 {
  return l.ID
}

func (l List) GetGroupID() int64 {
  return l.GroupID
}

And then elsewhere (which would define the database.GroupIDer interface):

func IsAuthorized(claims []int64, obj database.GroupIDer) bool {
  return slices.Contains(claims, obj.GetGroupID())
}

What database engines need to be changed?

None

What programming language backends need to be changed?

Go

Activity

  1. removed
    triageNew issues that hasn't been reviewed
    on Sep 20, 2023
  2. kyleconroy commented on Sep 20, 2023

    @kyleconroy
    Collaborator

    @evankanderson This isn't something we're planning on adding to sqlc, as we think it's better served by using a separate tool to generate these interfaces from structs. See my comment here #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 request

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions