Skip to content

Commit

Permalink
src: builder: add With prefix for all builder options
Browse files Browse the repository at this point in the history
Signed-off-by: Dmitrii Aleksandrov <[email protected]>
  • Loading branch information
Insei committed Feb 3, 2025
1 parent 68082d9 commit d48311c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
4 changes: 2 additions & 2 deletions builder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand All @@ -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))
}
Expand Down
4 changes: 2 additions & 2 deletions examples/example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}).
Expand Down
14 changes: 7 additions & 7 deletions repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{
Expand Down Expand Up @@ -430,10 +430,10 @@ func TestRepository_Insert(t *testing.T) {
model: &model{Name: "John Doe", Email: "[email protected]"},
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"
},
},
{
Expand All @@ -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" {
Expand All @@ -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
Expand Down Expand Up @@ -534,10 +534,10 @@ func TestRepository_Update(t *testing.T) {
model: &model{ID: 1, Name: "Updated Name", Email: "[email protected]"},
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"
},
},
{
Expand Down
20 changes: 10 additions & 10 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit d48311c

Please sign in to comment.