|
|
@ -19,17 +19,17 @@ var (
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
type (
|
|
|
|
type (
|
|
|
|
options = mopt.ClientOptions
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Option defines the method to customize a mongo model.
|
|
|
|
// Option defines the method to customize a mongo model.
|
|
|
|
Option func(opts *options)
|
|
|
|
Option func(opts *options)
|
|
|
|
|
|
|
|
|
|
|
|
// RegisterType A struct store With custom type and Encoder/Decoder
|
|
|
|
// TypeCodec is a struct that stores specific type Encoder/Decoder.
|
|
|
|
RegisterType struct {
|
|
|
|
TypeCodec struct {
|
|
|
|
ValueType reflect.Type
|
|
|
|
ValueType reflect.Type
|
|
|
|
Encoder bsoncodec.ValueEncoder
|
|
|
|
Encoder bsoncodec.ValueEncoder
|
|
|
|
Decoder bsoncodec.ValueDecoder
|
|
|
|
Decoder bsoncodec.ValueDecoder
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
options = mopt.ClientOptions
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
// DisableLog disables logging of mongo commands, includes info and slow logs.
|
|
|
|
// DisableLog disables logging of mongo commands, includes info and slow logs.
|
|
|
@ -48,12 +48,6 @@ func SetSlowThreshold(threshold time.Duration) {
|
|
|
|
slowThreshold.Set(threshold)
|
|
|
|
slowThreshold.Set(threshold)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func defaultTimeoutOption() Option {
|
|
|
|
|
|
|
|
return func(opts *options) {
|
|
|
|
|
|
|
|
opts.SetTimeout(defaultTimeout)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// WithTimeout set the mon client operation timeout.
|
|
|
|
// WithTimeout set the mon client operation timeout.
|
|
|
|
func WithTimeout(timeout time.Duration) Option {
|
|
|
|
func WithTimeout(timeout time.Duration) Option {
|
|
|
|
return func(opts *options) {
|
|
|
|
return func(opts *options) {
|
|
|
@ -61,14 +55,20 @@ func WithTimeout(timeout time.Duration) Option {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// WithRegistry set the Registry to convert custom type to mongo primitive type more easily.
|
|
|
|
// WithTypeCodec registers TypeCodecs to convert custom types.
|
|
|
|
func WithRegistry(registerType ...RegisterType) Option {
|
|
|
|
func WithTypeCodec(typeCodecs ...TypeCodec) Option {
|
|
|
|
return func(opts *options) {
|
|
|
|
return func(opts *options) {
|
|
|
|
registry := bson.NewRegistry()
|
|
|
|
registry := bson.NewRegistry()
|
|
|
|
for _, v := range registerType {
|
|
|
|
for _, v := range typeCodecs {
|
|
|
|
registry.RegisterTypeEncoder(v.ValueType, v.Encoder)
|
|
|
|
registry.RegisterTypeEncoder(v.ValueType, v.Encoder)
|
|
|
|
registry.RegisterTypeDecoder(v.ValueType, v.Decoder)
|
|
|
|
registry.RegisterTypeDecoder(v.ValueType, v.Decoder)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opts.SetRegistry(registry)
|
|
|
|
opts.SetRegistry(registry)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
func defaultTimeoutOption() Option {
|
|
|
|
|
|
|
|
return func(opts *options) {
|
|
|
|
|
|
|
|
opts.SetTimeout(defaultTimeout)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|