Skip to content
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

Call BeforeAppendModel on embedded types #982

Open
jeffreydwalter opened this issue Apr 24, 2024 · 0 comments
Open

Call BeforeAppendModel on embedded types #982

jeffreydwalter opened this issue Apr 24, 2024 · 0 comments

Comments

@jeffreydwalter
Copy link

Would be nice if BeforeAppendModel on embedded types was call so you could do something like the following:

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
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant