Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Structs of the Same Type #3312

Unanswered
tris203 asked this question in Q&A
Discussion options

If you have two queries, that return the same struct of the same type is there way to coerce them into returning the same object? Or is the best way to use a go interface after the fact?

https://play.sqlc.dev/p/5633c21e64ed689afc5e86c84da8a5e03d9015c921677a3d480594cc9b43094c

As you can see in this example, I have one function that returns

type GetUserTimeLineRow struct {
	Tweet Tweet
	Likedbyself interface{}
	Bookmarkedbyself interface{}
}

and one that returns

type GetTimeLineRow struct {
	Tweet Tweet
	Likedbyself interface{}
	Bookmarkedbyself interface{}
}

Is there a way to coerce them into one shape for passing on to a parseTimeLine() function without having to use interfaces to a do a bit of a type dance?
Or perhaps there is a better way to do it with my schema. I already have a trigger updating the counts so open to other ideas with views, triggers etc.

Thank you in advance

You must be logged in to vote

Replies: 2 comments

Comment options

You won't be able to change the return type of the function, but as the struct fields in GetUserTimeLineRow and GetTimeLineRow are identical, you could simply convert either of the types to the other, e.g.

foo := GetUserTimeLineRow{}
bar := GetTimeLineRow(foo)
You must be logged in to vote
0 replies
Comment options

@tris203 — Seems #3346 that I just posted is related...

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet

AltStyle によって変換されたページ (->オリジナル) /