feature: add a mongo registry option to convert type easier. (#3780)
Co-authored-by: Kevin Wan <wanjunfeng@gmail.com>
This commit is contained in:
@@ -1,9 +1,12 @@
|
||||
package mon
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"time"
|
||||
|
||||
"github.com/zeromicro/go-zero/core/syncx"
|
||||
"go.mongodb.org/mongo-driver/bson"
|
||||
"go.mongodb.org/mongo-driver/bson/bsoncodec"
|
||||
mopt "go.mongodb.org/mongo-driver/mongo/options"
|
||||
)
|
||||
|
||||
@@ -20,6 +23,13 @@ type (
|
||||
|
||||
// Option defines the method to customize a mongo model.
|
||||
Option func(opts *options)
|
||||
|
||||
// RegisterType A struct store With custom type and Encoder/Decoder
|
||||
RegisterType struct {
|
||||
ValueType reflect.Type
|
||||
Encoder bsoncodec.ValueEncoder
|
||||
Decoder bsoncodec.ValueDecoder
|
||||
}
|
||||
)
|
||||
|
||||
// DisableLog disables logging of mongo commands, includes info and slow logs.
|
||||
@@ -50,3 +60,15 @@ func WithTimeout(timeout time.Duration) Option {
|
||||
opts.SetTimeout(timeout)
|
||||
}
|
||||
}
|
||||
|
||||
// WithRegistry set the Registry to convert custom type to mongo primitive type more easily.
|
||||
func WithRegistry(registerType ...RegisterType) Option {
|
||||
return func(opts *options) {
|
||||
registry := bson.NewRegistry()
|
||||
for _, v := range registerType {
|
||||
registry.RegisterTypeEncoder(v.ValueType, v.Encoder)
|
||||
registry.RegisterTypeDecoder(v.ValueType, v.Decoder)
|
||||
}
|
||||
opts.SetRegistry(registry)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user