-
Notifications
You must be signed in to change notification settings - Fork 888
/
bsoncodec.go
132 lines (112 loc) · 4.4 KB
/
bsoncodec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package bsoncodec
import (
"fmt"
"strings"
"github.com/mongodb/mongo-go-driver/bson/bsonrw"
"github.com/mongodb/mongo-go-driver/bson/bsontype"
)
// Marshaler is an interface implemented by types that can marshal themselves
// into a BSON document represented as bytes. The bytes returned must be a valid
// BSON document if the error is nil.
type Marshaler interface {
MarshalBSON() ([]byte, error)
}
// ValueMarshaler is an interface implemented by types that can marshal
// themselves into a BSON value as bytes. The type must be the valid type for
// the bytes returned. The bytes and byte type together must be valid if the
// error is nil.
type ValueMarshaler interface {
MarshalBSONValue() (bsontype.Type, []byte, error)
}
// Unmarshaler is an interface implemented by types that can unmarshal a BSON
// document representation of themselves. The BSON bytes can be assumed to be
// valid. UnmarshalBSON must copy the BSON bytes if it wishes to retain the data
// after returning.
type Unmarshaler interface {
UnmarshalBSON([]byte) error
}
// ValueUnmarshaler is an interface implemented by types that can unmarshal a
// BSON value representaiton of themselves. The BSON bytes and type can be
// assumed to be valid. UnmarshalBSONValue must copy the BSON value bytes if it
// wishes to retain the data after returning.
type ValueUnmarshaler interface {
UnmarshalBSONValue(bsontype.Type, []byte) error
}
// ValueEncoderError is an error returned from a ValueEncoder when the provided
// value can't be encoded by the ValueEncoder.
type ValueEncoderError struct {
Name string
Types []interface{}
Received interface{}
}
func (vee ValueEncoderError) Error() string {
types := make([]string, 0, len(vee.Types))
for _, t := range vee.Types {
types = append(types, fmt.Sprintf("%T", t))
}
return fmt.Sprintf("%s can only process %s, but got a %T", vee.Name, strings.Join(types, ", "), vee.Received)
}
// ValueDecoderError is an error returned from a ValueDecoder when the provided
// value can't be decoded by the ValueDecoder.
type ValueDecoderError struct {
Name string
Types []interface{}
Received interface{}
}
func (vde ValueDecoderError) Error() string {
types := make([]string, 0, len(vde.Types))
for _, t := range vde.Types {
types = append(types, fmt.Sprintf("%T", t))
}
return fmt.Sprintf("%s can only process %s, but got a %T", vde.Name, strings.Join(types, ", "), vde.Received)
}
// EncodeContext is the contextual information required for a Codec to encode a
// value.
type EncodeContext struct {
*Registry
MinSize bool
}
// DecodeContext is the contextual information required for a Codec to decode a
// value.
type DecodeContext struct {
*Registry
Truncate bool
}
// ValueCodec is the interface that groups the methods to encode and decode
// values.
type ValueCodec interface {
ValueEncoder
ValueDecoder
}
// ValueEncoder is the interface implemented by types that can handle the
// encoding of a value. Implementations must handle both values and
// pointers to values.
type ValueEncoder interface {
EncodeValue(EncodeContext, bsonrw.ValueWriter, interface{}) error
}
// ValueEncoderFunc is an adapter function that allows a function with the
// correct signature to be used as a ValueEncoder.
type ValueEncoderFunc func(EncodeContext, bsonrw.ValueWriter, interface{}) error
// EncodeValue implements the ValueEncoder interface.
func (fn ValueEncoderFunc) EncodeValue(ec EncodeContext, vw bsonrw.ValueWriter, val interface{}) error {
return fn(ec, vw, val)
}
// ValueDecoder is the interface implemented by types that can handle the
// decoding of a value. Implementations must handle pointers to values,
// including pointers to pointer values. The implementation may create a new
// value and assign it to the pointer if necessary.
type ValueDecoder interface {
DecodeValue(DecodeContext, bsonrw.ValueReader, interface{}) error
}
// ValueDecoderFunc is an adapter function that allows a function with the
// correct signature to be used as a ValueDecoder.
type ValueDecoderFunc func(DecodeContext, bsonrw.ValueReader, interface{}) error
// DecodeValue implements the ValueDecoder interface.
func (fn ValueDecoderFunc) DecodeValue(dc DecodeContext, vr bsonrw.ValueReader, val interface{}) error {
return fn(dc, vr, val)
}
// CodecZeroer is the interface implemented by Codecs that can also determine if
// a value of the type that would be encoded is zero.
type CodecZeroer interface {
IsTypeZero(interface{}) bool
}