@@ -19,17 +19,17 @@ var (
19
19
)
20
20
21
21
type (
22
- options = mopt.ClientOptions
23
-
24
22
// Option defines the method to customize a mongo model.
25
23
Option func (opts * options )
26
24
27
- // RegisterType A struct store With custom type and Encoder/Decoder
28
- RegisterType struct {
25
+ // TypeCodec is a struct that stores specific type Encoder/Decoder.
26
+ TypeCodec struct {
29
27
ValueType reflect.Type
30
28
Encoder bsoncodec.ValueEncoder
31
29
Decoder bsoncodec.ValueDecoder
32
30
}
31
+
32
+ options = mopt.ClientOptions
33
33
)
34
34
35
35
// DisableLog disables logging of mongo commands, includes info and slow logs.
@@ -48,27 +48,27 @@ func SetSlowThreshold(threshold time.Duration) {
48
48
slowThreshold .Set (threshold )
49
49
}
50
50
51
- func defaultTimeoutOption () Option {
52
- return func (opts * options ) {
53
- opts .SetTimeout (defaultTimeout )
54
- }
55
- }
56
-
57
51
// WithTimeout set the mon client operation timeout.
58
52
func WithTimeout (timeout time.Duration ) Option {
59
53
return func (opts * options ) {
60
54
opts .SetTimeout (timeout )
61
55
}
62
56
}
63
57
64
- // WithRegistry set the Registry to convert custom type to mongo primitive type more easily .
65
- func WithRegistry ( registerType ... RegisterType ) Option {
58
+ // WithTypeCodec registers TypeCodecs to convert custom types .
59
+ func WithTypeCodec ( typeCodecs ... TypeCodec ) Option {
66
60
return func (opts * options ) {
67
61
registry := bson .NewRegistry ()
68
- for _ , v := range registerType {
62
+ for _ , v := range typeCodecs {
69
63
registry .RegisterTypeEncoder (v .ValueType , v .Encoder )
70
64
registry .RegisterTypeDecoder (v .ValueType , v .Decoder )
71
65
}
72
66
opts .SetRegistry (registry )
73
67
}
74
68
}
69
+
70
+ func defaultTimeoutOption () Option {
71
+ return func (opts * options ) {
72
+ opts .SetTimeout (defaultTimeout )
73
+ }
74
+ }
0 commit comments