You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
37 lines
948 B
37 lines
948 B
package middleware
|
|
|
|
import (
|
|
"context"
|
|
"github.com/qiniu/qmgo/field"
|
|
"github.com/qiniu/qmgo/hook"
|
|
"github.com/qiniu/qmgo/operator"
|
|
"github.com/qiniu/qmgo/validator"
|
|
)
|
|
|
|
// callback define the callback function type
|
|
type callback func(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error
|
|
|
|
// middlewareCallback the register callback slice
|
|
// some callbacks initial here without Register() for order
|
|
var middlewareCallback = []callback{
|
|
hook.Do,
|
|
field.Do,
|
|
validator.Do,
|
|
}
|
|
|
|
// Register register callback into middleware
|
|
func Register(cb callback) {
|
|
middlewareCallback = append(middlewareCallback, cb)
|
|
}
|
|
|
|
// Do call every registers
|
|
// The doc is always the document to operate
|
|
func Do(ctx context.Context, doc interface{}, opType operator.OpType, opts ...interface{}) error {
|
|
for _, cb := range middlewareCallback {
|
|
if err := cb(ctx, doc, opType, opts...); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
return nil
|
|
}
|