Skip to content

Commit a4235c0

Browse files
committed
GODRIVER-242 Document the valid struct tags for bson.Encoder and bson.Decoder
Change-Id: Ib33a1ea80a5d68d0299b0bc82b081b9956bbc264
1 parent 8ccaef4 commit a4235c0

File tree

3 files changed

+113
-6
lines changed

3 files changed

+113
-6
lines changed

bson/decode.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,24 @@ func NewDecoder(r io.Reader) *Decoder {
125125
// - any map with string keys
126126
// - a struct (possibly with tags)
127127
//
128-
// In the case of a map or struct, Decode will treat the value as a BSON document with the map keys
129-
// or struct field names as the keys and the map values or struct fields as the values.
128+
// In the case of struct values, only exported fields will be deserialized. The lowercased field
129+
// name is used as the key for each exported field, but this behavior may be changed using a struct
130+
// tag. The tag may also contain flags to adjust the unmarshaling behavior for the field. The tag
131+
// formats accepted are:
132+
//
133+
// "[<key>][,<flag1>[,<flag2>]]"
134+
//
135+
// `(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`
136+
//
137+
// The target field or element types of out may not necessarily match the BSON values of the
138+
// provided data. The following conversions are made automatically:
139+
//
140+
// - Numeric types are converted if at least the integer part of the value would be preserved
141+
// correctly
142+
//
143+
// If the value would not fit the type and cannot be converted, it is silently skipped.
144+
//
145+
// Pointer values are initialized when necessary.
130146
func (d *Decoder) Decode(v interface{}) error {
131147
switch t := v.(type) {
132148
case Unmarshaler:

bson/encode.go

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,35 @@ type Encoder interface {
5656
// - any map with string keys
5757
// - a struct (possibly with tags)
5858
//
59-
// In the case of a map or struct, Encode will interpret the value as a BSON document with the map
60-
// keys or struct field names as the keys and the map values or struct fields as the values.
59+
// In the case of a struct, the lowercased field name is used as the key for each exported
60+
// field but this behavior may be changed using a struct tag. The tag may also contain flags to
61+
// adjust the marshalling behavior for the field. The tag formats accepted are:
62+
//
63+
// "[<key>][,<flag1>[,<flag2>]]"
64+
//
65+
// `(...) bson:"[<key>][,<flag1>[,<flag2>]]" (...)`
66+
//
67+
// The following flags are currently supported:
68+
//
69+
// omitempty Only include the field if it's not set to the zero value for the type or to
70+
// empty slices or maps.
71+
//
72+
// minsize Marshal an integer of a type larger than 32 bits value as an int32, if that's
73+
// feasible while preserving the numeric value.
74+
//
75+
// inline Inline the field, which must be a struct or a map, causing all of its fields
76+
// or keys to be processed as if they were part of the outer struct.
77+
//
78+
// An example:
79+
//
80+
// type T struct {
81+
// A bool
82+
// B int "myb"
83+
// C string "myc,omitempty"
84+
// D string `bson:",omitempty" json:"jsonkey"`
85+
// E int64 ",minsize"
86+
// F int64 "myf,omitempty,minsize"
87+
// }
6188
Encode(interface{}) error
6289
}
6390

bson/marshal.go

Lines changed: 66 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,45 @@ import "bytes"
1010

1111
// Marshal converts a BSON type to bytes.
1212
//
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+
// }
1452
func Marshal(value interface{}) ([]byte, error) {
1553
var out bytes.Buffer
1654

@@ -24,7 +62,33 @@ func Marshal(value interface{}) ([]byte, error) {
2462

2563
// Unmarshal converts bytes into a BSON type.
2664
//
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.
2892
func Unmarshal(in []byte, out interface{}) error {
2993
return NewDecoder(bytes.NewReader(in)).Decode(out)
3094
}

0 commit comments

Comments
 (0)