|
5 | 5 | // a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
|
6 | 6 |
|
7 | 7 | // Package bson is a library for reading, writing, and manipulating BSON. The
|
8 |
| -// library has three types for representing BSON. |
9 |
| -// |
10 |
| -// The Reader type is used to validate and retrieve elements from a byte slice. |
11 |
| -// If you are not manipulating the underlying document and just want to validate |
12 |
| -// or ensure the document has certain keys, this is the type you should use. |
13 |
| -// |
14 |
| -// The Document type is a more generic type that can do all the read actions the |
15 |
| -// Reader can do and allows for manipulation of documents. The main usecase for |
16 |
| -// this type is reading some BSON and then adding, changing, or removing |
17 |
| -// elements or to build a document that will need to be manipulated later. If |
18 |
| -// the document can be created in a single pass without the need to do any |
19 |
| -// lookups, the Builder type is might be more appropriate. |
20 |
| -// |
21 |
| -// The Builder type (in the "bson/builder" package) is used to create a BSON |
22 |
| -// document. The type only allows the iterative building of a document, so there |
23 |
| -// is no way to verify the contents outside of writing the document. If you have |
24 |
| -// a Builder and need to conditionally add a field, you can write the document |
25 |
| -// to a byte slice and use the Reader type to lookup the desired document, but |
26 |
| -// in this case you should probably use a Document instead. |
27 |
| -// |
28 |
| -// The Element type represents a BSON element and the Value type represents an |
29 |
| -// individual value for a BSON element. |
30 |
| -// |
31 |
| -// The Encoder and Decoder types can be used to marshal a type to an io.Writer |
32 |
| -// or to unmarshal into a type from an io.Reader. These types will use |
33 |
| -// reflection and evaluate struct tags unless the provided types implements the |
34 |
| -// Marshaler or Unmarshaler interfaces. The Builder and Reader types can be used |
35 |
| -// to implement these interfaces for types. |
36 |
| -// |
37 |
| -// The DocumentEncoder type can be used to encode a type to a Document instead |
38 |
| -// of an io.Writer. This is useful if some additional manipulation is required |
39 |
| -// after encoding a document. This type supports the same encoding behavior as |
40 |
| -// the Encoder type. |
| 8 | +// library has two families of types for representing BSON. |
| 9 | +// |
| 10 | +// The Raw family of types is used to validate and retrieve elements from a slice of bytes. This |
| 11 | +// type is most useful when you want do lookups on BSON bytes without unmarshaling it into another |
| 12 | +// type. |
| 13 | +// |
| 14 | +// Example: |
| 15 | +// var raw bson.Raw = ... // bytes from somewhere |
| 16 | +// err := raw.Validate() |
| 17 | +// if err != nil { return err } |
| 18 | +// val := raw.Lookup("foo") |
| 19 | +// i32, ok := val.Int32OK() |
| 20 | +// // do something with i32... |
| 21 | +// |
| 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 | +// |
| 25 | +// Example: |
| 26 | +// bson.D{{"foo", "bar"}, {"hello", "world"}, {"pi", 3.14159}} |
| 27 | +// |
| 28 | +// |
| 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. |
| 32 | +// |
| 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... |
41 | 42 | package bson
|
0 commit comments