@@ -10,7 +10,45 @@ import "bytes"
10
10
11
11
// Marshal converts a BSON type to bytes.
12
12
//
13
- // TODO(GODRIVER-257): Document which types are valid for value.
13
+ // The value can be any one of the following types:
14
+ //
15
+ // - bson.Marshaler
16
+ // - io.Reader
17
+ // - []byte
18
+ // - bson.Reader
19
+ // - any map with string keys
20
+ // - a struct (possibly with tags)
21
+ //
22
+ // In the case of a struct, the lowercased field name is used as the key for each exported
23
+ // field but this behavior may be changed using a struct tag. The tag may also contain flags to
24
+ // adjust the marshalling behavior for the field. The tag formats accepted are:
25
+ //
26
+ // "[<key>][,<flag1>[,<flag2>]]"
27
+ //
28
+ // `(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`
29
+ //
30
+ // The following flags are currently supported:
31
+ //
32
+ // omitempty Only include the field if it's not set to the zero value for the type or to
33
+ // empty slices or maps.
34
+ //
35
+ // minsize Marshal an integer of a type larger than 32 bits value as an int32, if that's
36
+ // feasible while preserving the numeric value.
37
+ //
38
+ // inline Inline the field, which must be a struct or a map, causing all of its fields
39
+ // or keys to be processed as if they were part of the outer struct. For maps,
40
+ // keys must not conflict with the bson keys of other struct fields.
41
+ //
42
+ // An example:
43
+ //
44
+ // type T struct {
45
+ // A bool
46
+ // B int "myb"
47
+ // C string "myc,omitempty"
48
+ // D string `bson:",omitempty" json:"jsonkey"`
49
+ // E int64 ",minsize"
50
+ // F int64 "myf,omitempty,minsize"
51
+ // }
14
52
func Marshal (value interface {}) ([]byte , error ) {
15
53
var out bytes.Buffer
16
54
@@ -24,7 +62,33 @@ func Marshal(value interface{}) ([]byte, error) {
24
62
25
63
// Unmarshal converts bytes into a BSON type.
26
64
//
27
- // TODO(GODRIVER-257): Document which types are valid for value.
65
+ // The value can be any one of the following types:
66
+ //
67
+ // - bson.Unmarshaler
68
+ // - io.Writer
69
+ // - []byte
70
+ // - bson.Reader
71
+ // - any map with string keys
72
+ // - a struct (possibly with tags)
73
+ //
74
+ // In the case of struct values, only exported fields will be deserialized. The lowercased field
75
+ // name is used as the key for each exported field, but this behavior may be changed using a struct
76
+ // tag. The tag may also contain flags to adjust the unmarshaling behavior for the field. The tag
77
+ // formats accepted are:
78
+ //
79
+ // "[<key>][,<flag1>[,<flag2>]]"
80
+ //
81
+ // `(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`
82
+ //
83
+ // The target field or element types of out may not necessarily match the BSON values of the
84
+ // provided data. The following conversions are made automatically:
85
+ //
86
+ // - Numeric types are converted if at least the integer part of the value would be preserved
87
+ // correctly
88
+ //
89
+ // If the value would not fit the type and cannot be converted, it is silently skipped.
90
+ //
91
+ // Pointer values are initialized when necessary.
28
92
func Unmarshal (in []byte , out interface {}) error {
29
93
return NewDecoder (bytes .NewReader (in )).Decode (out )
30
94
}
0 commit comments