From d48311c2e567c18048ea5073e0642590c21775c6 Mon Sep 17 00:00:00 2001 From: Dmitrii Aleksandrov Date: Mon, 3 Feb 2025 06:01:12 +0300 Subject: [PATCH] src: builder: add With prefix for all builder options Signed-off-by: Dmitrii Aleksandrov --- builder.go | 10 +++++----- builder_test.go | 4 ++-- examples/example/main.go | 4 ++-- repository_test.go | 14 +++++++------- types.go | 20 ++++++++++---------- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/builder.go b/builder.go index af5e0fd..8943bb3 100644 --- a/builder.go +++ b/builder.go @@ -69,31 +69,31 @@ func (b *builder[TModel]) WithQuery(queryFn func(m *TModel, h query.PersistentHe } // BeforeInsert registers a function that is executed before performing an insert operation on the model in the database. -func (b *builder[TModel]) BeforeInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] { +func (b *builder[TModel]) WithBeforeInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] { b.opts = append(b.opts, WithBeforeInsert[TModel](fn)) return b } // BeforeUpdate registers a function to be executed before performing an update operation on the model in the database. -func (b *builder[TModel]) BeforeUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] { +func (b *builder[TModel]) WithBeforeUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] { b.opts = append(b.opts, WithBeforeUpdate[TModel](fn)) return b } // AfterSelect registers a callback function to be executed after models are retrieved through a select operation. -func (b *builder[TModel]) AfterSelect(fn func(ctx context.Context, models []*TModel)) Builder[TModel] { +func (b *builder[TModel]) WithAfterSelect(fn func(ctx context.Context, models []*TModel)) Builder[TModel] { b.opts = append(b.opts, WithAfterSelect[TModel](fn)) return b } // AfterUpdate registers a callback function to be executed after an update operation is performed on the model. -func (b *builder[TModel]) AfterUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] { +func (b *builder[TModel]) WithAfterUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] { b.opts = append(b.opts, WithAfterUpdate[TModel](fn)) return b } // AfterInsert registers a callback function to be executed after an insert operation is performed on the model. -func (b *builder[TModel]) AfterInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] { +func (b *builder[TModel]) WithAfterInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] { b.opts = append(b.opts, WithAfterInsert[TModel](fn)) return b } diff --git a/builder_test.go b/builder_test.go index 74dfe3e..48ba73c 100644 --- a/builder_test.go +++ b/builder_test.go @@ -155,7 +155,7 @@ func TestBuilder_BeforeInsert(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := NewBuilder[mockModel]().(*builder[mockModel]) - b.BeforeInsert(tt.beforeInsertFn) + b.WithBeforeInsert(tt.beforeInsertFn) if len(b.opts) != tt.expected { t.Errorf("expected %v opts, got %v", tt.expected, len(b.opts)) } @@ -176,7 +176,7 @@ func TestBuilder_AfterInsert(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { b := NewBuilder[mockModel]().(*builder[mockModel]) - b.AfterInsert(tt.afterInsertFn) + b.WithAfterInsert(tt.afterInsertFn) if len(b.opts) != tt.expected { t.Errorf("expected %v opts, got %v", tt.expected, len(b.opts)) } diff --git a/examples/example/main.go b/examples/example/main.go index 4851cb3..99b87b2 100644 --- a/examples/example/main.go +++ b/examples/example/main.go @@ -56,10 +56,10 @@ func main() { b.AddTrueSQLFn(func(ctx context.Context) string { return "tests.created_at < now()" }) }) }). - BeforeInsert(func(ctx context.Context, m *test) { + WithBeforeInsert(func(ctx context.Context, m *test) { m.CreatedAt = time.Now() }). - BeforeUpdate(func(ctx context.Context, m *test) { + WithBeforeUpdate(func(ctx context.Context, m *test) { updAt := time.Now() m.UpdatedAt = &updAt }). diff --git a/repository_test.go b/repository_test.go index e449b5b..e9d6cf2 100644 --- a/repository_test.go +++ b/repository_test.go @@ -209,7 +209,7 @@ func TestRepository_GetList(t *testing.T) { expectedErr: ErrTest, }, { - name: "AfterSelect modifies models", + name: "WithAfterSelect modifies models", executor: &MockExecutor[model]{ GetMultipleFunc: func(ctx context.Context, stmt executor.Stmt) ([]*model, error) { return []*model{ @@ -430,10 +430,10 @@ func TestRepository_Insert(t *testing.T) { model: &model{Name: "John Doe", Email: "john@example.com"}, expectedErr: nil, beforeInsert: func(ctx context.Context, model *model) { - model.Name = "BeforeInsert Name" + model.Name = "WithBeforeInsert Name" }, afterInsert: func(ctx context.Context, model *model) { - model.Name = "AfterInsert Name" + model.Name = "WithAfterInsert Name" }, }, { @@ -447,7 +447,7 @@ func TestRepository_Insert(t *testing.T) { expectedErr: ErrTest, }, { - name: "BeforeInsert modifies model", + name: "WithBeforeInsert modifies model", executor: &MockExecutor[model]{ InsertOneFunc: func(ctx context.Context, stmt executor.Stmt, model *model) error { if model.Name != "Modified Name" { @@ -463,7 +463,7 @@ func TestRepository_Insert(t *testing.T) { }, }, { - name: "AfterInsert modifies model", + name: "WithAfterInsert modifies model", executor: &MockExecutor[model]{ InsertOneFunc: func(ctx context.Context, stmt executor.Stmt, model *model) error { return nil @@ -534,10 +534,10 @@ func TestRepository_Update(t *testing.T) { model: &model{ID: 1, Name: "Updated Name", Email: "updated@example.com"}, expectedErr: nil, beforeUpdate: func(ctx context.Context, model *model) { - model.Name = "BeforeUpdate Name" + model.Name = "WithBeforeUpdate Name" }, afterUpdate: func(ctx context.Context, model *model) { - model.Name = "AfterUpdate Name" + model.Name = "WithAfterUpdate Name" }, }, { diff --git a/types.go b/types.go index 0fbfc3c..e10e871 100644 --- a/types.go +++ b/types.go @@ -35,16 +35,16 @@ type Repository[TModel any] interface { type Builder[TModel any] interface { // WithQuery applies a persistent query function to customize the query process for the model. WithQuery(queryFn func(m *TModel, h query.PersistentHelper[TModel])) Builder[TModel] - // BeforeInsert registers a function to modify the model before insert operations. - BeforeInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] - // BeforeUpdate registers a function to modify the model before update operations. - BeforeUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] - // AfterSelect registers a function to process or transform models after selection operations. - AfterSelect(fn func(ctx context.Context, models []*TModel)) Builder[TModel] - // AfterInsert registers a function to process or transform the model after insert operations. - AfterInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] - // AfterUpdate registers a function to process or transform the model after update operations. - AfterUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] + // WithBeforeInsert registers a function to modify the model before insert operations. + WithBeforeInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] + // WithBeforeUpdate registers a function to modify the model before update operations. + WithBeforeUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] + // WithAfterSelect registers a function to process or transform models after selection operations. + WithAfterSelect(fn func(ctx context.Context, models []*TModel)) Builder[TModel] + // WithAfterInsert registers a function to process or transform the model after insert operations. + WithAfterInsert(fn func(ctx context.Context, m *TModel)) Builder[TModel] + // WithAfterUpdate registers a function to process or transform the model after update operations. + WithAfterUpdate(fn func(ctx context.Context, m *TModel)) Builder[TModel] // WithErrorTransformer allows customizing or wrapping errors during repository operations. WithErrorTransformer(fn func(err error) error) Builder[TModel] // Build finalizes and constructs the configured repository for the model.