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

Closed
jeffreydwalter opened this issue Apr 24, 2024 · 1 comment
Closed

Call BeforeAppendModel on embedded types #982

jeffreydwalter opened this issue Apr 24, 2024 · 1 comment
Labels

Comments

@jeffreydwalter
Copy link
Contributor

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
}
Copy link

This issue has been automatically marked as stale because it has not had activity in the last 30 days. If there is no update within the next 7 days, this issue will be closed.

@github-actions github-actions bot added the stale label Nov 17, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Nov 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant