Skip to content

Commit 820a361

Browse files
committed
minor updates
1 parent 51ca775 commit 820a361

File tree

10 files changed

+59
-59
lines changed

10 files changed

+59
-59
lines changed

bson/decimal.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"strconv"
1919
"strings"
2020

21-
"go.mongodb.org/mongo-driver/internal/bsonutil/primitive"
21+
"go.mongodb.org/mongo-driver/internal/decimal128"
2222
)
2323

2424
// These constants are the maximum and minimum values for the exponent field in a decimal128 value.
@@ -52,7 +52,7 @@ func (d Decimal128) GetBytes() (uint64, uint64) {
5252

5353
// String returns a string representation of the decimal value.
5454
func (d Decimal128) String() string {
55-
return primitive.Decimal128String(d.h, d.l)
55+
return decimal128.String(d.h, d.l)
5656
}
5757

5858
// BigInt returns significand as big.Int and exponent, bi * 10 ^ exp.

internal/bsonutil/primitive/primitive.go renamed to internal/decimal128/decinal128.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
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
7+
package decimal128
88

99
import (
1010
"strconv"
@@ -33,8 +33,8 @@ func divmod(h, l uint64, div uint32) (qh, ql uint64, rem uint32) {
3333
return (aq<<32 | bq), (cq<<32 | dq), uint32(dr)
3434
}
3535

36-
// Decimal128String returns a string representation of the decimal value.
37-
func Decimal128String(h, l uint64) string {
36+
// String returns a string representation of the decimal value.
37+
func String(h, l uint64) string {
3838
var posSign int // positive sign
3939
var exp int // exponent
4040
var high, low uint64 // significand high/low

x/bsonx/bsoncore/bson_arraybuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ func (a *ArrayBuilder) AppendString(str string) *ArrayBuilder {
8181
}
8282

8383
// AppendObjectID will append oid to ArrayBuilder.doc
84-
func (a *ArrayBuilder) AppendObjectID(oid [idLen]byte) *ArrayBuilder {
84+
func (a *ArrayBuilder) AppendObjectID(oid objectID) *ArrayBuilder {
8585
a.arr = AppendObjectIDElement(a.arr, a.incrementKey(), oid)
8686
return a
8787
}
@@ -124,7 +124,7 @@ func (a *ArrayBuilder) AppendRegex(pattern, options string) *ArrayBuilder {
124124
}
125125

126126
// AppendDBPointer will append ns and oid to a.arr
127-
func (a *ArrayBuilder) AppendDBPointer(ns string, oid [idLen]byte) *ArrayBuilder {
127+
func (a *ArrayBuilder) AppendDBPointer(ns string, oid objectID) *ArrayBuilder {
128128
a.arr = AppendDBPointerElement(a.arr, a.incrementKey(), ns, oid)
129129
return a
130130
}

x/bsonx/bsoncore/bson_arraybuilder_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,10 @@ func TestArrayBuilder(t *testing.T) {
6565
"AppendObjectID",
6666
NewArrayBuilder().AppendObjectID,
6767
[]interface{}{
68-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
68+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
6969
},
7070
BuildDocumentFromElements(nil, AppendObjectIDElement(nil, "0",
71-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
71+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
7272
},
7373
{
7474
"AppendBoolean",
@@ -146,9 +146,9 @@ func TestArrayBuilder(t *testing.T) {
146146
"AppendDBPointer",
147147
NewArrayBuilder().AppendDBPointer,
148148
[]interface{}{"barbaz",
149-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C}},
149+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C}},
150150
BuildDocumentFromElements(nil, AppendDBPointerElement(nil, "0", "barbaz",
151-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
151+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
152152
},
153153
{
154154
"AppendUndefined",

x/bsonx/bsoncore/bson_documentbuilder.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (db *DocumentBuilder) AppendString(key string, str string) *DocumentBuilder
6767
}
6868

6969
// AppendObjectID will append oid to DocumentBuilder.doc with the given key
70-
func (db *DocumentBuilder) AppendObjectID(key string, oid [idLen]byte) *DocumentBuilder {
70+
func (db *DocumentBuilder) AppendObjectID(key string, oid objectID) *DocumentBuilder {
7171
db.doc = AppendObjectIDElement(db.doc, key, oid)
7272
return db
7373
}
@@ -110,7 +110,7 @@ func (db *DocumentBuilder) AppendRegex(key, pattern, options string) *DocumentBu
110110
}
111111

112112
// AppendDBPointer will append ns and oid to using key to db.doc
113-
func (db *DocumentBuilder) AppendDBPointer(key string, ns string, oid [idLen]byte) *DocumentBuilder {
113+
func (db *DocumentBuilder) AppendDBPointer(key string, ns string, oid objectID) *DocumentBuilder {
114114
db.doc = AppendDBPointerElement(db.doc, key, ns, oid)
115115
return db
116116
}

x/bsonx/bsoncore/bson_documentbuilder_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ func TestDocumentBuilder(t *testing.T) {
6666
NewDocumentBuilder().AppendObjectID,
6767
[]interface{}{
6868
"foobar",
69-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
69+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
7070
},
7171
BuildDocumentFromElements(nil, AppendObjectIDElement(nil, "foobar",
72-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
72+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
7373
},
7474
{
7575
"AppendBoolean",
@@ -147,9 +147,9 @@ func TestDocumentBuilder(t *testing.T) {
147147
"AppendDBPointer",
148148
NewDocumentBuilder().AppendDBPointer,
149149
[]interface{}{"foobar", "barbaz",
150-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C}},
150+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C}},
151151
BuildDocumentFromElements(nil, AppendDBPointerElement(nil, "foobar", "barbaz",
152-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
152+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C})),
153153
},
154154
{
155155
"AppendUndefined",

x/bsonx/bsoncore/bsoncore.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,10 @@ const (
2222
nullTerminator = string(byte(0))
2323
invalidKeyPanicMsg = "BSON element keys cannot contain null bytes"
2424
invalidRegexPanicMsg = "BSON regex values cannot contain null bytes"
25-
26-
// idLen is the length of a ObjectID
27-
idLen = 12
2825
)
2926

27+
type objectID = [12]byte
28+
3029
// AppendType will append t to dst and return the extended buffer.
3130
func AppendType(dst []byte, t Type) []byte { return append(dst, byte(t)) }
3231

@@ -339,21 +338,22 @@ func AppendUndefinedElement(dst []byte, key string) []byte {
339338
}
340339

341340
// AppendObjectID will append oid to dst and return the extended buffer.
342-
func AppendObjectID(dst []byte, oid [idLen]byte) []byte { return append(dst, oid[:]...) }
341+
func AppendObjectID(dst []byte, oid objectID) []byte { return append(dst, oid[:]...) }
343342

344343
// AppendObjectIDElement will append a BSON ObjectID element using key and oid to dst
345344
// and return the extended buffer.
346-
func AppendObjectIDElement(dst []byte, key string, oid [idLen]byte) []byte {
345+
func AppendObjectIDElement(dst []byte, key string, oid objectID) []byte {
347346
return AppendObjectID(AppendHeader(dst, TypeObjectID, key), oid)
348347
}
349348

350349
// ReadObjectID will read an ObjectID from src. If there are not enough bytes it
351350
// will return false.
352-
func ReadObjectID(src []byte) ([idLen]byte, []byte, bool) {
353-
if len(src) < 12 {
354-
return [idLen]byte{}, src, false
351+
func ReadObjectID(src []byte) (objectID, []byte, bool) {
352+
var oid objectID
353+
idLen := cap(oid)
354+
if len(src) < idLen {
355+
return oid, src, false
355356
}
356-
var oid [idLen]byte
357357
copy(oid[:], src[0:idLen])
358358
return oid, src[idLen:], true
359359
}
@@ -447,26 +447,26 @@ func ReadRegex(src []byte) (pattern, options string, rem []byte, ok bool) {
447447
}
448448

449449
// AppendDBPointer will append ns and oid to dst and return the extended buffer.
450-
func AppendDBPointer(dst []byte, ns string, oid [idLen]byte) []byte {
450+
func AppendDBPointer(dst []byte, ns string, oid objectID) []byte {
451451
return append(appendstring(dst, ns), oid[:]...)
452452
}
453453

454454
// AppendDBPointerElement will append a BSON DBPointer element using key, ns,
455455
// and oid to dst and return the extended buffer.
456-
func AppendDBPointerElement(dst []byte, key, ns string, oid [idLen]byte) []byte {
456+
func AppendDBPointerElement(dst []byte, key, ns string, oid objectID) []byte {
457457
return AppendDBPointer(AppendHeader(dst, TypeDBPointer, key), ns, oid)
458458
}
459459

460460
// ReadDBPointer will read a ns and oid from src. If there are not enough bytes it
461461
// will return false.
462-
func ReadDBPointer(src []byte) (ns string, oid [idLen]byte, rem []byte, ok bool) {
462+
func ReadDBPointer(src []byte) (ns string, oid objectID, rem []byte, ok bool) {
463463
ns, rem, ok = readstring(src)
464464
if !ok {
465-
return "", [idLen]byte{}, src, false
465+
return "", objectID{}, src, false
466466
}
467467
oid, rem, ok = ReadObjectID(rem)
468468
if !ok {
469-
return "", [idLen]byte{}, src, false
469+
return "", objectID{}, src, false
470470
}
471471
return ns, oid, rem, true
472472
}

x/bsonx/bsoncore/bsoncore_test.go

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ func TestAppend(t *testing.T) {
194194
AppendObjectID,
195195
[]interface{}{
196196
make([]byte, 0),
197-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
197+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
198198
},
199199
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
200200
},
@@ -203,7 +203,7 @@ func TestAppend(t *testing.T) {
203203
AppendObjectIDElement,
204204
[]interface{}{
205205
make([]byte, 0), "foobar",
206-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
206+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
207207
},
208208
[]byte{byte(TypeObjectID),
209209
'f', 'o', 'o', 'b', 'a', 'r', 0x00,
@@ -267,7 +267,7 @@ func TestAppend(t *testing.T) {
267267
[]interface{}{
268268
make([]byte, 0),
269269
"foobar",
270-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
270+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
271271
},
272272
[]byte{
273273
0x07, 0x00, 0x00, 0x00, 'f', 'o', 'o', 'b', 'a', 'r', 0x00,
@@ -280,7 +280,7 @@ func TestAppend(t *testing.T) {
280280
[]interface{}{
281281
make([]byte, 0), "foobar",
282282
"barbaz",
283-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
283+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
284284
},
285285
[]byte{byte(TypeDBPointer),
286286
'f', 'o', 'o', 'b', 'a', 'r', 0x00,
@@ -602,14 +602,14 @@ func TestRead(t *testing.T) {
602602
"ReadObjectID/not enough bytes",
603603
ReadObjectID,
604604
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06},
605-
[]interface{}{[idLen]byte{}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, false},
605+
[]interface{}{[12]byte{}, []byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06}, false},
606606
},
607607
{
608608
"ReadObjectID/success",
609609
ReadObjectID,
610610
[]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
611611
[]interface{}{
612-
[idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
612+
[12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
613613
[]byte{}, true,
614614
},
615615
},
@@ -659,13 +659,13 @@ func TestRead(t *testing.T) {
659659
"ReadDBPointer/not enough bytes (ns)",
660660
ReadDBPointer,
661661
[]byte{},
662-
[]interface{}{"", [idLen]byte{}, []byte{}, false},
662+
[]interface{}{"", [12]byte{}, []byte{}, false},
663663
},
664664
{
665665
"ReadDBPointer/not enough bytes (objectID)",
666666
ReadDBPointer,
667667
[]byte{0x04, 0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00},
668-
[]interface{}{"", [idLen]byte{}, []byte{0x04, 0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00}, false},
668+
[]interface{}{"", [12]byte{}, []byte{0x04, 0x00, 0x00, 0x00, 'f', 'o', 'o', 0x00}, false},
669669
},
670670
{
671671
"ReadDBPointer/success",
@@ -675,7 +675,7 @@ func TestRead(t *testing.T) {
675675
0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C,
676676
},
677677
[]interface{}{
678-
"foo", [idLen]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
678+
"foo", [12]byte{0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C},
679679
[]byte{}, true,
680680
},
681681
},

x/bsonx/bsoncore/value.go

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import (
1818
"time"
1919
"unicode/utf8"
2020

21-
"go.mongodb.org/mongo-driver/internal/bsonutil/primitive"
21+
"go.mongodb.org/mongo-driver/internal/decimal128"
2222
)
2323

2424
// ElementTypeError specifies that a method to obtain a BSON value an incorrect type was called on a bson.Value.
@@ -326,7 +326,7 @@ func (v Value) String() string {
326326
if !ok {
327327
return ""
328328
}
329-
return fmt.Sprintf(`{"$numberDecimal":"%s"}`, primitive.Decimal128String(h, l))
329+
return fmt.Sprintf(`{"$numberDecimal":"%s"}`, decimal128.String(h, l))
330330
case TypeMinKey:
331331
return `{"$minKey":1}`
332332
case TypeMaxKey:
@@ -507,7 +507,7 @@ func (v Value) BinaryOK() (subtype byte, data []byte, ok bool) {
507507

508508
// ObjectID returns the BSON objectid value the Value represents. It panics if the value is a BSON
509509
// type other than objectid.
510-
func (v Value) ObjectID() [idLen]byte {
510+
func (v Value) ObjectID() objectID {
511511
if v.Type != TypeObjectID {
512512
panic(ElementTypeError{"bsoncore.Value.ObjectID", v.Type})
513513
}
@@ -520,13 +520,13 @@ func (v Value) ObjectID() [idLen]byte {
520520

521521
// ObjectIDOK is the same as ObjectID, except it returns a boolean instead of
522522
// panicking.
523-
func (v Value) ObjectIDOK() ([idLen]byte, bool) {
523+
func (v Value) ObjectIDOK() (objectID, bool) {
524524
if v.Type != TypeObjectID {
525-
return [idLen]byte{}, false
525+
return objectID{}, false
526526
}
527527
oid, _, ok := ReadObjectID(v.Data)
528528
if !ok {
529-
return [idLen]byte{}, false
529+
return objectID{}, false
530530
}
531531
return oid, true
532532
}
@@ -637,7 +637,7 @@ func (v Value) RegexOK() (pattern, options string, ok bool) {
637637

638638
// DBPointer returns the BSON dbpointer value the Value represents. It panics if the value is a BSON
639639
// type other than DBPointer.
640-
func (v Value) DBPointer() (string, [idLen]byte) {
640+
func (v Value) DBPointer() (string, objectID) {
641641
if v.Type != TypeDBPointer {
642642
panic(ElementTypeError{"bsoncore.Value.DBPointer", v.Type})
643643
}
@@ -650,13 +650,13 @@ func (v Value) DBPointer() (string, [idLen]byte) {
650650

651651
// DBPointerOK is the same as DBPoitner, except that it returns a boolean
652652
// instead of panicking.
653-
func (v Value) DBPointerOK() (string, [idLen]byte, bool) {
653+
func (v Value) DBPointerOK() (string, objectID, bool) {
654654
if v.Type != TypeDBPointer {
655-
return "", [idLen]byte{}, false
655+
return "", objectID{}, false
656656
}
657657
ns, pointer, _, ok := ReadDBPointer(v.Data)
658658
if !ok {
659-
return "", [idLen]byte{}, false
659+
return "", objectID{}, false
660660
}
661661
return ns, pointer, true
662662
}

0 commit comments

Comments
 (0)