Skip to content

Commit da4dc67

Browse files
Merge pull request #34 from chenmingyong0423/feature/refactor
refactor
2 parents e36471f + 0450cff commit da4dc67

26 files changed

+1181
-154
lines changed

.github/workflows/go-fmt.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ jobs:
2626
steps:
2727
- uses: actions/checkout@v4
2828
- name: Set up Go
29-
uses: actions/setup-go@v4
29+
uses: actions/setup-go@v5
3030
with:
31-
go-version: ">=1.21.0"
31+
go-version: ">=1.18.0"
3232

3333
- name: Install goimports
3434
run: go install golang.org/x/tools/cmd/goimports@latest

.github/workflows/go.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ jobs:
3131
- uses: actions/checkout@v4
3232

3333
- name: Set up Go
34-
uses: actions/setup-go@v4
34+
uses: actions/setup-go@v5
3535
with:
36-
go-version: '1.21'
36+
go-version: '1.18.0'
3737

3838
- name: Build
3939
run: go build -v ./...
@@ -42,4 +42,4 @@ jobs:
4242
run: go test -race -coverprofile=cover.out -v ./...
4343

4444
- name: Post Coverage
45-
uses: codecov/codecov-action@v2
45+
uses: codecov/codecov-action@v4

.github/workflows/golangci-lint.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ jobs:
3131
name: lint
3232
runs-on: ubuntu-latest
3333
steps:
34-
- uses: actions/setup-go@v4
34+
- uses: actions/setup-go@v5
3535
with:
36-
go-version: '1.21'
36+
go-version: '1.18.0'
3737
- uses: actions/checkout@v4
3838
- name: golangci-lint
39-
uses: golangci/golangci-lint-action@v3
39+
uses: golangci/golangci-lint-action@v6
4040
with:
4141
# Optional: version of golangci-lint to use in form of v1.2 or v1.2.3 or `latest` to use the latest version
4242
version: latest

.github/workflows/integration_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ jobs:
2828
- name: Set up Go
2929
uses: actions/setup-go@v4
3030
with:
31-
go-version: '>=1.21.0'
31+
go-version: '>=1.18.0'
3232

3333
- name: Test
3434
run: sudo sh ./script/integrate_test.sh

.github/workflows/stale.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ jobs:
2424
runs-on: ubuntu-latest
2525

2626
steps:
27-
- uses: actions/stale@v4
27+
- uses: actions/stale@v9
2828
with:
2929
repo-token: ${{ secrets.GITHUB_TOKEN }}
3030
stale-issue-message: 'This issue is inactive for a long time.'

builder/query/evaluation_query_builder.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package query
1616

1717
import (
18-
"github.com/chenmingyong0423/go-mongox/pkg/utils"
18+
"github.com/chenmingyong0423/go-mongox/internal/pkg/utils"
1919
"go.mongodb.org/mongo-driver/bson"
2020
)
2121

builder/update/array_update_builder_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ package update
1717
import (
1818
"testing"
1919

20-
"github.com/chenmingyong0423/go-mongox/bsonx"
20+
"github.com/chenmingyong0423/go-mongox/internal/pkg/utils"
2121

22-
"github.com/chenmingyong0423/go-mongox/pkg/utils"
22+
"github.com/chenmingyong0423/go-mongox/bsonx"
2323

2424
"github.com/stretchr/testify/assert"
2525
"go.mongodb.org/mongo-driver/bson"

callback.go

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,16 @@
1515
package mongox
1616

1717
import (
18+
"context"
19+
20+
validator2 "github.com/go-playground/validator/v10"
21+
22+
"github.com/chenmingyong0423/go-mongox/hook/validator"
23+
24+
"github.com/chenmingyong0423/go-mongox/hook/model"
25+
1826
"github.com/chenmingyong0423/go-mongox/callback"
27+
"github.com/chenmingyong0423/go-mongox/hook/field"
1928
"github.com/chenmingyong0423/go-mongox/operation"
2029
)
2130

@@ -26,3 +35,46 @@ func RegisterPlugin(name string, cb callback.CbFn, opType operation.OpType) {
2635
func RemovePlugin(name string, opType operation.OpType) {
2736
callback.Callbacks.Remove(opType, name)
2837
}
38+
39+
type PluginConfig struct {
40+
EnableDefaultFieldHook bool
41+
EnableModelHook bool
42+
EnableValidationHook bool
43+
// use to replace to the default validate instance
44+
Validate *validator2.Validate
45+
}
46+
47+
func InitPlugin(config *PluginConfig) {
48+
if config.EnableDefaultFieldHook {
49+
opTypes := []operation.OpType{operation.OpTypeBeforeInsert, operation.OpTypeBeforeUpsert}
50+
for _, opType := range opTypes {
51+
typ := opType
52+
RegisterPlugin("mongox:default_field", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
53+
return field.Execute(ctx, opCtx, typ, opts...)
54+
}, typ)
55+
}
56+
}
57+
if config.EnableModelHook {
58+
opTypes := []operation.OpType{
59+
operation.OpTypeBeforeInsert, operation.OpTypeAfterInsert,
60+
operation.OpTypeBeforeUpsert, operation.OpTypeAfterUpsert,
61+
operation.OpTypeAfterFind,
62+
}
63+
for _, opType := range opTypes {
64+
typ := opType
65+
RegisterPlugin("mongox:model", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
66+
return model.Execute(ctx, opCtx, typ, opts...)
67+
}, typ)
68+
}
69+
}
70+
if config.EnableValidationHook {
71+
validator.SetValidate(config.Validate)
72+
opTypes := []operation.OpType{operation.OpTypeBeforeInsert, operation.OpTypeBeforeUpsert}
73+
for _, opType := range opTypes {
74+
typ := opType
75+
RegisterPlugin("mongox:validation", func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
76+
return validator.Execute(ctx, opCtx, typ, opts...)
77+
}, typ)
78+
}
79+
}
80+
}

callback/callback.go

Lines changed: 6 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,6 @@ package callback
1717
import (
1818
"context"
1919

20-
"github.com/chenmingyong0423/go-mongox/hook/validator"
21-
22-
"github.com/chenmingyong0423/go-mongox/hook/model"
23-
24-
"github.com/chenmingyong0423/go-mongox/hook/field"
2520
"github.com/chenmingyong0423/go-mongox/operation"
2621
)
2722

@@ -31,75 +26,16 @@ var Callbacks = initializeCallbacks()
3126

3227
func initializeCallbacks() *Callback {
3328
return &Callback{
34-
beforeInsert: []callbackHandler{
35-
{
36-
name: "mongox:default_field",
37-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
38-
return field.Execute(ctx, opCtx, operation.OpTypeBeforeInsert, opts...)
39-
},
40-
},
41-
{
42-
name: "mongox:model",
43-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
44-
return model.Execute(ctx, opCtx, operation.OpTypeBeforeInsert, opts...)
45-
},
46-
},
47-
{
48-
name: "mongox:validation",
49-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
50-
return validator.Execute(ctx, opCtx, operation.OpTypeBeforeInsert, opts...)
51-
},
52-
},
53-
},
54-
afterInsert: []callbackHandler{
55-
{
56-
name: "mongox:model",
57-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
58-
return model.Execute(ctx, opCtx, operation.OpTypeAfterInsert, opts...)
59-
},
60-
},
61-
},
29+
beforeInsert: make([]callbackHandler, 0),
30+
afterInsert: make([]callbackHandler, 0),
6231
beforeUpdate: make([]callbackHandler, 0),
6332
afterUpdate: make([]callbackHandler, 0),
6433
beforeDelete: make([]callbackHandler, 0),
6534
afterDelete: make([]callbackHandler, 0),
66-
beforeUpsert: []callbackHandler{
67-
{
68-
name: "mongox:default_field",
69-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
70-
return field.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
71-
},
72-
},
73-
{
74-
name: "mongox:model",
75-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
76-
return model.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
77-
},
78-
},
79-
{
80-
name: "mongox:validation",
81-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
82-
return validator.Execute(ctx, opCtx, operation.OpTypeBeforeUpsert, opts...)
83-
},
84-
},
85-
},
86-
afterUpsert: []callbackHandler{
87-
{
88-
name: "mongox:model",
89-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
90-
return model.Execute(ctx, opCtx, operation.OpTypeAfterUpsert, opts...)
91-
},
92-
},
93-
},
94-
beforeFind: make([]callbackHandler, 0),
95-
afterFind: []callbackHandler{
96-
{
97-
name: "mongox:model",
98-
fn: func(ctx context.Context, opCtx *operation.OpContext, opts ...any) error {
99-
return model.Execute(ctx, opCtx, operation.OpTypeAfterFind, opts...)
100-
},
101-
},
102-
},
35+
beforeUpsert: make([]callbackHandler, 0),
36+
afterUpsert: make([]callbackHandler, 0),
37+
beforeFind: make([]callbackHandler, 0),
38+
afterFind: make([]callbackHandler, 0),
10339
}
10440
}
10541

0 commit comments

Comments
 (0)