-
-
Notifications
You must be signed in to change notification settings - Fork 243
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow embedding structs for a common columns #351
Comments
It would be nice to encode them using JSON and store encoded text to character type column. |
Is there some documentation available how to use model embedding at all? |
I think I read a doc that says I can use golang's sql scanner to support custom types. I am currently doing this:
But I have to copy & paste the code everywhere since golang doesn't support generics. |
@sunho You're talking about something different than I am. You're talking about putting JSON into a column. You can do that using the I'm talking about struct embedding. In the Go language, it causes a syntax sugar that makes the function and fields on the embedded structs are accessible on the outer type. Those functions can also be used to implement interfaces. The reason I refer to it as syntax sugar, is because the type Common struct {
ID uuid.UUID
DeletedAt nulls.Time
....
OwnerID uuid.UUID
}
type Widgit struct {
Common
Name string
} This is very powerful because I can have the inner struct implement part or all of a common interface between all my models, so they can operate on some of those common fields that almost all tables have. I actually started using GORM because of this, because it does support this, but I find Pop to be a bit cleaner/idiomatic. so I'd love to be able to switch back. |
How could I not consider wrapping them as functions? Thank you. |
I'm trying to implement the feature because I also think it is useful for my project. Gorm supports embedded structs by flattening the struct into a map with string keys and using it as the input to sql driver. But, in pop, this way would require a complete refactoring of columns package. I wonder if there's easier way to implement this. |
I looked into creating a package that would wrap the package reflectx
type Type interface {
// copy/paste exported methods from reflect.Type
}
func FlattenEmbedding(t Type) Type {
// initialize and return &flattenEmbedding{}
...
}
type flattenEmbedding struct {
inner Type
fields map[string] reflect.FieldInfo
}
// implement Type interface It's not as nice as implementing Note: Such a package might be useful for adding embedded behavior in other packages, like Plush, which also doesn't allow you to use the short references on embedded structs. |
I fixed this in #691, sorry for not looking up whether an issue exists already. |
Description
I've got a project where I'm going to need to have a set of 6 database columns that are on every type.
I thought I'd take care of this through embedding. There is some logic I can apply to them too that will save me, but Go's reflection, and thus pop, handle embedded structs as an anonymous field. It would be great if pop would handle the embedded structs's fields as if they were primary fields.
Pop: v4.9.9, using Buffalo v0.14.0-alpha.4
Please precise your OS, the Pop version and if you're using Pop through Buffalo.
The text was updated successfully, but these errors were encountered: