We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
Would be nice if BeforeAppendModel on embedded types was call so you could do something like the following:
BeforeAppendModel
type AuditFields struct { Created time.Time Updated *time.Time } var _ bun.BeforeAppendModelHook = (* AuditFields)(nil) func (a * AuditFields) BeforeAppendModel(ctx context.Context, query bun.Query) error { if a == nil { a = new(AuditFields) } now := time.Now() switch query.(type) { case *bun.InsertQuery: a.Created = &now case *bun.UpdateQuery: a.Updated = &now } return nil }
type User struct { AuditFields ID int64 ... }
Here's a potential solution:
func (m *structTableModel) BeforeAppendModel(ctx context.Context, query Query) error { if !m.structInited { if err := m.initStruct(); err != nil { return err } } if m.table.HasBeforeAppendModelHook() { if err := m.invokeBeforeAppendModelHook(ctx, query, m.strct); err != nil { return err } } // Check and invoke hooks on embedded models return m.invokeEmbeddedBeforeAppendModelHooks(ctx, query, m.strct) } func (m *structTableModel) invokeBeforeAppendModelHook(ctx context.Context, query Query, value reflect.Value) error { if hook, ok := value.Addr().Interface().(schema.BeforeAppendModelHook); ok { return hook.BeforeAppendModel(ctx, query) } return nil } func (m *structTableModel) invokeEmbeddedBeforeAppendModelHooks(ctx context.Context, query Query, value reflect.Value) error { for i := 0; i < value.NumField(); i++ { field := value.Field(i) fieldType := field.Type() if fieldType.Kind() == reflect.Struct { if err := m.invokeEmbeddedBeforeAppendModelHooks(ctx, query, field); err != nil { return err } } else if fieldType.Kind() == reflect.Ptr && !field.IsNil() && field.Elem().Kind() == reflect.Struct { if err := m.invokeEmbeddedBeforeAppendModelHooks(ctx, query, field.Elem()); err != nil { return err } } // Check if the field itself implements the hook if field.CanAddr() { if err := m.invokeBeforeAppendModelHook(ctx, query, field); err != nil { return err } } } return nil }
The text was updated successfully, but these errors were encountered:
No branches or pull requests
Would be nice if
BeforeAppendModel
on embedded types was call so you could do something like the following:Here's a potential solution:
The text was updated successfully, but these errors were encountered: