Skip to content

Commit 4991574

Browse files
committed
Update documentation for stable BSON packages
Change-Id: I2df9c682d0e1237999417216464c785cb54b89a8
1 parent bb96e7b commit 4991574

File tree

9 files changed

+45
-63
lines changed

9 files changed

+45
-63
lines changed

bson/bson.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,6 @@ import (
1515
"strings"
1616
)
1717

18-
// node is a compact representation of an element within a BSON document.
19-
// The first 4 bytes are where the element starts in an underlying []byte. The
20-
// last 4 bytes are where the value for that element begins.
21-
//
22-
// The type of the element can be accessed as `data[n[0]]`. The key of the
23-
// element can be accessed as `data[n[0]+1:n[1]-1]`. This will account for the
24-
// null byte at the end of the c-style string. The value can be accessed as
25-
// `data[n[1]:]`. Since there is no value end byte, an unvalidated document
26-
// could result in parsing errors.
27-
type node [2]uint32
28-
2918
// Zeroer allows custom struct types to implement a report of zero
3019
// state. All struct types that don't implement Zeroer or where IsZero
3120
// returns false are considered to be not zero.

bson/decimal/decimal.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// Based on gopkg.in/mgo.v2/bson by Gustavo Niemeyer
88
// See THIRD-PARTY-NOTICES for original license terms.
99

10+
// Package decimal implements a Decimal128 type.
1011
package decimal
1112

1213
import (

bson/decoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ var decPool = sync.Pool{
2525
},
2626
}
2727

28-
// A Decoder reads and decodes BSON documents from a stream.
28+
// A Decoder reads and decodes BSON documents from a stream. It reads from a bsonrw.ValueReader as
29+
// the source of BSON data.
2930
type Decoder struct {
3031
r *bsoncodec.Registry
3132
vr bsonrw.ValueReader

bson/doc.go

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,38 @@
55
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
66

77
// 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...
4142
package bson

bson/encoder.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ var encPool = sync.Pool{
2424
},
2525
}
2626

27-
// An Encoder writes a serialization format to an output stream.
27+
// An Encoder writes a serialization format to an output stream. It writes to a bsonrw.ValueWriter
28+
// as the destination of BSON data.
2829
type Encoder struct {
2930
r *bsoncodec.Registry
3031
vw bsonrw.ValueWriter

bson/objectid/objectid.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
// Based on gopkg.in/mgo.v2/bson by Gustavo Niemeyer
88
// See THIRD-PARTY-NOTICES for original license terms.
99

10+
// Package objectid contains an implementation of a BSON objectID type functions to create
11+
// objectIDs.
1012
package objectid
1113

1214
import (

bson/primitive/primitive.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
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 primitive contains types similar to Go primitives for BSON types can do not have direct
8+
// Go primitive representations.
79
package primitive
810

911
import (

bson/raw.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ var ErrNilReader = errors.New("nil reader")
1818
var errValidateDone = errors.New("validation loop complete")
1919

2020
// Raw is a wrapper around a byte slice. It will interpret the slice as a
21-
// BSON document. This type is a wrapper around a bsoncore.Document.
21+
// BSON document. This type is a wrapper around a bsoncore.Document. Errors returned from the
22+
// methods on this type and associated types come from the bsoncore package.
2223
type Raw []byte
2324

2425
// NewFromIOReader reads in a document from the given io.Reader and constructs a Raw from
@@ -36,11 +37,6 @@ func (r Raw) Validate() (err error) { return bsoncore.Document(r).Validate() }
3637
// there are multiple keys provided, this method will recurse down, as long as
3738
// the top and intermediate nodes are either documents or arrays. If any key
3839
// except for the last is not a document or an array, an error will be returned.
39-
//
40-
// TODO(skriptble): Implement better error messages.
41-
//
42-
// TODO(skriptble): Determine if this should return an error on empty key and
43-
// key not found.
4440
func (r Raw) Lookup(key ...string) RawValue {
4541
return convertFromCoreValue(bsoncore.Document(r).Lookup(key...))
4642
}

bson/raw_element.go

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,17 +10,6 @@ import (
1010
"github.com/mongodb/mongo-go-driver/x/bsonx/bsoncore"
1111
)
1212

13-
// MalformedElementError represents a class of errors that RawElement methods return.
14-
type MalformedElementError string
15-
16-
func (mee MalformedElementError) Error() string { return string(mee) }
17-
18-
// ErrElementMissingKey is returned when a RawElement is missing a key.
19-
const ErrElementMissingKey MalformedElementError = "element is missing key"
20-
21-
// ErrElementMissingType is returned when a RawElement is missing a type.
22-
const ErrElementMissingType MalformedElementError = "element is missing type"
23-
2413
// RawElement represents a BSON element in byte form. This type provides a simple way to
2514
// transform a slice of bytes into a BSON element and extract information from it.
2615
//

0 commit comments

Comments
 (0)