@@ -56,7 +56,40 @@ import (
5656// --------------------------------------------------------------------------
5757// The public API.
5858
59- // A value implementing the bson.Getter interface will have its GetBSON
59+ // Element types constants from BSON specification.
60+ const (
61+ ElementFloat64 byte = 0x01
62+ ElementString byte = 0x02
63+ ElementDocument byte = 0x03
64+ ElementArray byte = 0x04
65+ ElementBinary byte = 0x05
66+ Element06 byte = 0x06
67+ ElementObjectId byte = 0x07
68+ ElementBool byte = 0x08
69+ ElementDatetime byte = 0x09
70+ ElementNil byte = 0x0A
71+ ElementRegEx byte = 0x0B
72+ ElementDBPointer byte = 0x0C
73+ ElementJavaScriptWithoutScope byte = 0x0D
74+ ElementSymbol byte = 0x0E
75+ ElementJavaScriptWithScope byte = 0x0F
76+ ElementInt32 byte = 0x10
77+ ElementTimestamp byte = 0x11
78+ ElementInt64 byte = 0x12
79+ ElementDecimal128 byte = 0x13
80+ ElementMinKey byte = 0xFF
81+ ElementMaxKey byte = 0x7F
82+
83+ BinaryGeneric byte = 0x00
84+ BinaryFunction byte = 0x01
85+ BinaryBinaryOld byte = 0x02
86+ BinaryUUIDOld byte = 0x03
87+ BinaryUUID byte = 0x04
88+ BinaryMD5 byte = 0x05
89+ BinaryUserDefined byte = 0x80
90+ )
91+
92+ // Getter interface: a value implementing the bson.Getter interface will have its GetBSON
6093// method called when the given value has to be marshalled, and the result
6194// of this method will be marshaled in place of the actual object.
6295//
@@ -66,12 +99,12 @@ type Getter interface {
6699 GetBSON () (interface {}, error )
67100}
68101
69- // A value implementing the bson.Setter interface will receive the BSON
102+ // Setter interface: a value implementing the bson.Setter interface will receive the BSON
70103// value via the SetBSON method during unmarshaling, and the object
71104// itself will not be changed as usual.
72105//
73106// If setting the value works, the method should return nil or alternatively
74- // bson.SetZero to set the respective field to its zero value (nil for
107+ // bson.ErrSetZero to set the respective field to its zero value (nil for
75108// pointer types). If SetBSON returns a value of type bson.TypeError, the
76109// BSON value will be omitted from a map or slice being decoded and the
77110// unmarshalling will continue. If it returns any other non-nil error, the
@@ -97,10 +130,10 @@ type Setter interface {
97130 SetBSON (raw Raw ) error
98131}
99132
100- // SetZero may be returned from a SetBSON method to have the value set to
133+ // ErrSetZero may be returned from a SetBSON method to have the value set to
101134// its respective zero value. When used in pointer values, this will set the
102135// field to nil rather than to the pre-allocated value.
103- var SetZero = errors .New ("set to zero" )
136+ var ErrSetZero = errors .New ("set to zero" )
104137
105138// M is a convenient alias for a map[string]interface{} map, useful for
106139// dealing with BSON in a native way. For instance:
@@ -156,7 +189,7 @@ type Raw struct {
156189// documents in general.
157190type RawD []RawDocElem
158191
159- // See the RawD type.
192+ // RawDocElem elements of RawD type.
160193type RawDocElem struct {
161194 Name string
162195 Value Raw
@@ -166,7 +199,7 @@ type RawDocElem struct {
166199// long. MongoDB objects by default have such a property set in their "_id"
167200// property.
168201//
169- // http://www.mongodb.org/display/DOCS/Object+IDs
202+ // http://www.mongodb.org/display/DOCS/Object+Ids
170203type ObjectId string
171204
172205// ObjectIdHex returns an ObjectId from the provided hex representation.
@@ -192,7 +225,7 @@ func IsObjectIdHex(s string) bool {
192225
193226// objectIdCounter is atomically incremented when generating a new ObjectId
194227// using NewObjectId() function. It's used as a counter part of an id.
195- var objectIdCounter uint32 = readRandomUint32 ()
228+ var objectIdCounter = readRandomUint32 ()
196229
197230// readRandomUint32 returns a random objectIdCounter.
198231func readRandomUint32 () uint32 {
@@ -300,12 +333,12 @@ func (id *ObjectId) UnmarshalJSON(data []byte) error {
300333 return nil
301334 }
302335 if len (data ) != 26 || data [0 ] != '"' || data [25 ] != '"' {
303- return errors . New ( fmt .Sprintf ("invalid ObjectId in JSON: %s" , string (data ) ))
336+ return fmt .Errorf ("invalid ObjectId in JSON: %s" , string (data ))
304337 }
305338 var buf [12 ]byte
306339 _ , err := hex .Decode (buf [:], data [1 :25 ])
307340 if err != nil {
308- return errors . New ( fmt .Sprintf ("invalid ObjectId in JSON: %s (%s)" , string (data ), err ) )
341+ return fmt .Errorf ("invalid ObjectId in JSON: %s (%s)" , string (data ), err )
309342 }
310343 * id = ObjectId (string (buf [:]))
311344 return nil
@@ -571,12 +604,12 @@ func Unmarshal(in []byte, out interface{}) (err error) {
571604 d := newDecoder (in )
572605 d .readDocTo (v )
573606 if d .i < len (d .in ) {
574- return errors .New ("Document is corrupted" )
607+ return errors .New ("document is corrupted" )
575608 }
576609 case reflect .Struct :
577- return errors .New ("Unmarshal can't deal with struct values. Use a pointer. " )
610+ return errors .New ("unmarshal can't deal with struct values. Use a pointer" )
578611 default :
579- return errors .New ("Unmarshal needs a map or a pointer to a struct. " )
612+ return errors .New ("unmarshal needs a map or a pointer to a struct" )
580613 }
581614 return nil
582615}
@@ -600,13 +633,15 @@ func (raw Raw) Unmarshal(out interface{}) (err error) {
600633 return & TypeError {v .Type (), raw .Kind }
601634 }
602635 case reflect .Struct :
603- return errors .New ("Raw Unmarshal can't deal with struct values. Use a pointer. " )
636+ return errors .New ("raw Unmarshal can't deal with struct values. Use a pointer" )
604637 default :
605- return errors .New ("Raw Unmarshal needs a map or a valid pointer. " )
638+ return errors .New ("raw Unmarshal needs a map or a valid pointer" )
606639 }
607640 return nil
608641}
609642
643+ // TypeError store details for type error occuring
644+ // during unmarshaling
610645type TypeError struct {
611646 Type reflect.Type
612647 Kind byte
0 commit comments