Skip to content

Commit d88f225

Browse files
author
Divjot Arora
authored
Document default encoding/decoding behavior for Go to BSON (#239)
1 parent 7eeaa5b commit d88f225

File tree

2 files changed

+91
-17
lines changed

2 files changed

+91
-17
lines changed

bson/bsoncodec/time_codec.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const (
2222

2323
var defaultTimeCodec = NewTimeCodec()
2424

25-
// TimeCodec is the Codec used for struct values.
25+
// TimeCodec is the Codec used for time.Time values.
2626
type TimeCodec struct {
2727
UseLocalTimeZone bool
2828
}

bson/doc.go

Lines changed: 90 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
// not use this file except in compliance with the License. You may obtain
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

7-
// Package bson is a library for reading, writing, and manipulating BSON. The
8-
// library has two families of types for representing BSON.
7+
// Package bson is a library for reading, writing, and manipulating BSON. BSON is a binary serialization format used to
8+
// store documents and make remote procedure calls in MongoDB. The BSON specification is located at https://bsonspec.org.
9+
//
10+
// Raw BSON
911
//
1012
// The Raw family of types is used to validate and retrieve elements from a slice of bytes. This
1113
// type is most useful when you want do lookups on BSON bytes without unmarshaling it into another
@@ -19,24 +21,96 @@
1921
// i32, ok := val.Int32OK()
2022
// // do something with i32...
2123
//
22-
// The D family of types is used to build concise representations of BSON using native Go types.
23-
// These types do not support automatic lookup.
24+
// Native Go Types
25+
//
26+
// The D and M types defined in this package can be used to build representations of BSON using native Go types. D is a
27+
// slice and M is a map. For more information about the use cases for these types, see the documentation on the type
28+
// definitions.
2429
//
2530
// Example:
2631
// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}}
32+
// bson.M{"foo": "bar", "hello": "world", "pi": 3.14159}
2733
//
34+
// When decoding BSON to a D or M, the following type mappings apply when unmarshalling:
2835
//
29-
// Marshaling and Unmarshaling are handled with the Marshal and Unmarshal family of functions. If
30-
// you need to write or read BSON from a non-slice source, an Encoder or Decoder can be used with a
31-
// bsonrw.ValueWriter or bsonrw.ValueReader.
36+
// 1. BSON int32 unmarshals to an int32.
37+
// 2. BSON int64 unmarshals to an int64.
38+
// 3. BSON double unmarshals to a float64.
39+
// 4. BSON string unmarshals to a string.
40+
// 5. BSON boolean unmarshals to a bool.
41+
// 6. BSON embedded document unmarshals to the parent type (i.e. D for a D, M for an M).
42+
// 7. BSON array unmarshals to a bson.A.
43+
// 8. BSON ObjectId unmarshals to a primitive.ObjectID.
44+
// 9. BSON datetime unmarshals to a primitive.Datetime.
45+
// 10. BSON binary unmarshals to a primitive.Binary.
46+
// 11. BSON regular expression unmarshals to a primitive.Regex.
47+
// 12. BSON JavaScript unmarshals to a primitive.JavaScript.
48+
// 13. BSON code with scope unmarshals to a primitive.CodeWithScope.
49+
// 14. BSON timestamp unmarshals to an primitive.Timestamp.
50+
// 15. BSON 128-bit decimal unmarshals to an primitive.Decimal128.
51+
// 16. BSON min key unmarshals to an primitive.MinKey.
52+
// 17. BSON max key unmarshals to an primitive.MaxKey.
53+
// 18. BSON undefined unmarshals to a primitive.Undefined.
54+
// 19. BSON null unmarshals to a primitive.Null.
55+
// 20. BSON DBPointer unmarshals to a primitive.DBPointer.
56+
// 21. BSON symbol unmarshals to a primitive.Symbol.
3257
//
33-
// Example:
34-
// b, err := bson.Marshal(bson.D{{"foo", "bar"}})
35-
// if err != nil { return err }
36-
// var fooer struct {
37-
// Foo string
38-
// }
39-
// err = bson.Unmarshal(b, &fooer)
40-
// if err != nil { return err }
41-
// // do something with fooer...
58+
// The above mappings also apply when marshalling a D or M to BSON. Some other useful marshalling mappings are:
59+
//
60+
// 1. time.Time marshals to a BSON datetime.
61+
// 2. int8, int16, and int32 marshal to a BSON int32.
62+
// 3. int marshals to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32, inclusive, and a BSON int64
63+
// otherwise.
64+
// 4. int64 marshals to BSON int64.
65+
// 5. uint8 and uint16 marshal to a BSON int32.
66+
// 6. uint, uint32, and uint64 marshal to a BSON int32 if the value is between math.MinInt32 and math.MaxInt32,
67+
// inclusive, and BSON int64 otherwise.
68+
//
69+
// Structs
70+
//
71+
// Structs can be marshalled/unmarshalled to/from BSON. When transforming structs to/from BSON, the following rules
72+
// apply:
73+
//
74+
// 1. Only exported fields in structs will be marshalled or unmarshalled.
75+
//
76+
// 2. When marshalling a struct, each field will be lowercased to generate the key for the corresponding BSON element.
77+
// For example, a struct field named "Foo" will generate key "foo". This can be overriden via a struct tag (e.g.
78+
// `bson:"fooField"` to generate key "fooField" instead).
79+
//
80+
// 3. An embedded struct field is marshalled as a subdocument. The key will be the lowercased name of the field's type.
81+
//
82+
// 4. A pointer field is marshalled as the underlying type if the pointer is non-nil. If the pointer is nil, it is
83+
// marshalled as a BSON null value.
84+
//
85+
// 5. When unmarshalling, a field of type interface{} will follow the D/M type mappings listed above. BSON documents
86+
// unmarshalled into an interface{} field will be unmarshalled as a D.
87+
//
88+
// The following struct tags can be used to configure behavior:
89+
//
90+
// 1. omitempty: If the omitempty struct tag is specified on a field, the field will not be marshalled if it is set to
91+
// the zero value. By default, a struct field is only considered empty if the field's type implements the Zeroer
92+
// interface and the IsZero method returns true. Struct fields of types that do not implement Zeroer are always
93+
// marshalled as embedded documents.
94+
//
95+
// 2. minsize: If the minsize struct tag is specified on a field of type int64, uint, uint32, or uint64 and the value of
96+
// the field can fit in a signed int32, the field will be serialized as a BSON int32 rather than a BSON int64. For other
97+
// types, this tag is ignored.
98+
//
99+
// 3. truncate: If the truncate struct tag is specified on a field with a non-float numeric type, BSON doubles unmarshalled
100+
// into that field will be trucated at the decimal point. For example, if 3.14 is unmarshalled into a field of type int,
101+
// it will be unmarshalled as 3. If this tag is not specified, the decoder will throw an error if the value cannot be
102+
// decoded without losing precision. For float64 or non-numeric types, this tag is ignored.
103+
//
104+
// 4. inline: If the inline struct tag is specified for a struct or map field, the field will be "flattened" when
105+
// marshalling and "un-flattened" when unmarshalling. This means that all of the fields in that struct/map will be
106+
// pulled up one level and will become top-level fields rather than being fields in a nested document. For example, if a
107+
// map field named "Map" with value map[string]interface{}{"foo": "bar"} is inlined, the resulting document will be
108+
// {"foo": "bar"} instead of {"map": {"foo": "bar"}}. There can only be one inlined map field in a struct. If there are
109+
// duplicated fields in the resulting document when an inlined field is marshalled, an error will be returned. This tag
110+
// can be used with fields that are pointers to structs. If an inlined pointer field is nil, it will not be marshalled.
111+
// For fields that are not maps or structs, this tag is ignored.
112+
//
113+
// Marshalling and Unmarshalling
114+
//
115+
// Manually marshalling and unmarshalling can be done with the Marshal and Unmarshal family of functions.
42116
package bson

0 commit comments

Comments
 (0)