GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Generate getter methods for query models by Cyberax · Pull Request #387 · sqlc-dev/sqlc

Conversation

@Cyberax

@Cyberax Cyberax commented Mar 9, 2020 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This change allows the use of face interfaces to write somewhat generic
mapping methods.

Example:

// An example of an application-level structure
type AppAuthor struct {
	ID   int64
	Name string
	Bio  string
}

// A mapper that uses a face interface
func MapToAuthor(a interface {
	GetID() int64
	GetName() string
	GetBio() sql.NullString
}) AppAuthor {
	return AppAuthor{
		ID:   a.GetID(),
		Name: a.GetName(),
		Bio:  a.GetBio().String,
	}
}

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Cyberax
Generate getter methods for query models
feeac9f
This change allows the use of face interfaces to write somewhat generic
mapping methods.

Example:

// An example of an application-level structure
type AppAuthor struct {
	ID   int64
	Name string
	Bio  string
}

// A mapper that uses a face interface
func MapToAuthor(a interface {
	GetID() int64
	GetName() string
	GetBio() sql.NullString
}) AppAuthor {
	return AppAuthor{
		ID:   a.GetID(),
		Name: a.GetName(),
		Bio:  a.GetBio().String,
	}
}
@kyleconroy
kyleconroy self-requested a review March 9, 2020 20:40
@Cyberax
Merge branch 'master' into master
5519cbf
kyleconroy
kyleconroy reviewed Apr 2, 2020
View reviewed changes

@kyleconroy kyleconroy left a comment

Copy link
Copy Markdown
Collaborator

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Cyberax I'm so sorry this PR has languished with a review. I was going to review it and then things got a bit crazy.

I'm not sold on the utility of these Getter methods. They also conflict other fields on the structs themselves. You can imagine a database table with a foo and get_foo column.

If you're looking for these type of interfaces, I'd suggest using a small amount of codegen to inspect the struct and generate the methods from that.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@Cyberax

Cyberax commented Apr 2, 2020

Copy link
Copy Markdown
Contributor Author

These "face interfaces" actually saved me quite a bit of code in mappers, because I have a fair amount of almost but not exactly the same queries.

I don't see any problems with foo and get_foo, you'll have methods GetFoo and GetGetFoo. This is admittedly ugly, but won't cause any issues.

I guess custom codegen is an option, but at least can you tags to generated classes so that they can be easily discovered?

ekovacs reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@tschaub tschaub mentioned this pull request Apr 13, 2020
Generate types with methods that can be used as interfaces #444
Closed
@Cyberax

Cyberax commented May 3, 2020

Copy link
Copy Markdown
Contributor Author

Would you mind revisiting this PR? It would really help us (we're maintaining an internal fork for now).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@rf

rf commented May 18, 2020

Copy link
Copy Markdown

+1, this would be particularly useful for us as well for a similar reason!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@akshayjshah

akshayjshah commented May 31, 2020

Copy link
Copy Markdown
Contributor

I'd also find this convenient, but I think it's reasonable to do with a separate code gen tool. Seems to me that there's enough complexity here already, especially for a personal project - cutting scope to keep the project semi-maintainable seems like a good long-term decision.

I don't see any problems with foo and get_foo, you'll have methods GetFoo and GetGetFoo. This is admittedly ugly, but won't cause any issues.

The problem is that the get_foo column will (by default) correspond to a GetFoo struct field, which conflicts with the GetFoo accessor for the foo column. See "Method declarations" in the Go spec: "If the base type is a struct type, the non-blank method and field names must be distinct." This exact issue creates some complexity in the protobuf and Apache Thrift compilers, both of which generate accessors.

(Also, 👋 @rf!)

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Cyberax Cyberax closed this Jun 24, 2021
@leg100 leg100 mentioned this pull request Apr 28, 2022
Proposal: Accessor methods for row structs jschaf/pggen#57
Closed
@jamesleeht jamesleeht mentioned this pull request Aug 2, 2023
Support for custom models #755
Open
@ktogo ktogo mentioned this pull request Jan 10, 2024
Proposal: Add option to emit //go:generate annotation #3124
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@kyleconroy kyleconroy kyleconroy left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@Cyberax @rf @akshayjshah @kyleconroy
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.