Skip to content

Commit 3525304

Browse files
benjirewisBenjamin Rewis
authored andcommitted
GODRIVER-1930 bsoncore Array functions should return Array (#626)
1 parent c317b0c commit 3525304

File tree

8 files changed

+22
-56
lines changed

8 files changed

+22
-56
lines changed

x/bsonx/bsoncore/array_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,7 @@ func TestArray(t *testing.T) {
299299
'\x00', '\x00',
300300
},
301301
`[[null,null]]`,
302-
`Array(19)[[null ,null ]]`,
302+
`Array(19)[Array(11)[null,null]]`,
303303
},
304304
{
305305
"malformed--length too small",

x/bsonx/bsoncore/bsoncore.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ func BuildArrayElement(dst []byte, key string, values ...Value) []byte {
308308

309309
// ReadArray will read an array from src. If there are not enough bytes it
310310
// will return false.
311-
func ReadArray(src []byte) (arr Document, rem []byte, ok bool) { return readLengthBytes(src) }
311+
func ReadArray(src []byte) (arr Array, rem []byte, ok bool) { return readLengthBytes(src) }
312312

313313
// AppendBinary will append subtype and b to dst and return the extended buffer.
314314
func AppendBinary(dst []byte, subtype byte, b []byte) []byte {

x/bsonx/bsoncore/bsoncore_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -548,19 +548,19 @@ func TestRead(t *testing.T) {
548548
"ReadArray/not enough bytes (length)",
549549
ReadArray,
550550
[]byte{},
551-
[]interface{}{Document(nil), []byte{}, false},
551+
[]interface{}{Array(nil), []byte{}, false},
552552
},
553553
{
554554
"ReadArray/not enough bytes (value)",
555555
ReadArray,
556556
[]byte{0x0F, 0x00, 0x00, 0x00},
557-
[]interface{}{Document(nil), []byte{0x0F, 0x00, 0x00, 0x00}, false},
557+
[]interface{}{Array(nil), []byte{0x0F, 0x00, 0x00, 0x00}, false},
558558
},
559559
{
560560
"ReadArray/success",
561561
ReadArray,
562562
[]byte{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00},
563-
[]interface{}{Document{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00}, []byte{}, true},
563+
[]interface{}{Array{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00}, []byte{}, true},
564564
},
565565
{
566566
"ReadBinary/not enough bytes (length)",

x/bsonx/bsoncore/document.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,8 @@ func (d Document) LookupErr(key ...string) (Value, error) {
200200
}
201201
return val, nil
202202
case bsontype.Array:
203-
val, err := elem.Value().Array().LookupErr(key[1:]...)
203+
// Convert to Document to continue Lookup recursion.
204+
val, err := Document(elem.Value().Array()).LookupErr(key[1:]...)
204205
if err != nil {
205206
return Value{}, err
206207
}

x/bsonx/bsoncore/value.go

Lines changed: 4 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ func (v Value) String() string {
250250
if !ok {
251251
return ""
252252
}
253-
return docAsArray(arr, false)
253+
return arr.String()
254254
case bsontype.Binary:
255255
subtype, data, ok := v.BinaryOK()
256256
if !ok {
@@ -366,7 +366,7 @@ func (v Value) DebugString() string {
366366
if !ok {
367367
return "<malformed>"
368368
}
369-
return docAsArray(arr, true)
369+
return arr.DebugString()
370370
case bsontype.CodeWithScope:
371371
code, scope, ok := v.CodeWithScopeOK()
372372
if !ok {
@@ -464,7 +464,7 @@ func (v Value) DocumentOK() (Document, bool) {
464464

465465
// Array returns the BSON array the Value represents as an Array. It panics if the
466466
// value is a BSON type other than array.
467-
func (v Value) Array() Document {
467+
func (v Value) Array() Array {
468468
if v.Type != bsontype.Array {
469469
panic(ElementTypeError{"bsoncore.Value.Array", v.Type})
470470
}
@@ -477,7 +477,7 @@ func (v Value) Array() Document {
477477

478478
// ArrayOK is the same as Array, except it returns a boolean instead
479479
// of panicking.
480-
func (v Value) ArrayOK() (Document, bool) {
480+
func (v Value) ArrayOK() (Array, bool) {
481481
if v.Type != bsontype.Array {
482482
return nil, false
483483
}
@@ -978,38 +978,3 @@ func sortStringAlphebeticAscending(s string) string {
978978
sort.Sort(ss)
979979
return string([]rune(ss))
980980
}
981-
982-
func docAsArray(d Document, debug bool) string {
983-
if len(d) < 5 {
984-
return ""
985-
}
986-
var buf bytes.Buffer
987-
buf.WriteByte('[')
988-
989-
length, rem, _ := ReadLength(d) // We know we have enough bytes to read the length
990-
991-
length -= 4
992-
993-
var elem Element
994-
var ok bool
995-
first := true
996-
for length > 1 {
997-
if !first {
998-
buf.WriteByte(',')
999-
}
1000-
elem, rem, ok = ReadElement(rem)
1001-
length -= int32(len(elem))
1002-
if !ok {
1003-
return ""
1004-
}
1005-
if debug {
1006-
fmt.Fprintf(&buf, "%s ", elem.Value().DebugString())
1007-
} else {
1008-
fmt.Fprintf(&buf, "%s", elem.Value())
1009-
}
1010-
first = false
1011-
}
1012-
buf.WriteByte(']')
1013-
1014-
return buf.String()
1015-
}

x/bsonx/bsoncore/value_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -182,22 +182,22 @@ func TestValue(t *testing.T) {
182182
{
183183
"Array/Success", Value.Array, Value{Type: bsontype.Array, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00}},
184184
nil,
185-
[]interface{}{Document{0x05, 0x00, 0x00, 0x00, 0x00}},
185+
[]interface{}{Array{0x05, 0x00, 0x00, 0x00, 0x00}},
186186
},
187187
{
188188
"ArrayOK/Not Array", Value.ArrayOK, Value{Type: bsontype.String},
189189
nil,
190-
[]interface{}{Document(nil), false},
190+
[]interface{}{Array(nil), false},
191191
},
192192
{
193193
"ArrayOK/Insufficient Bytes", Value.ArrayOK, Value{Type: bsontype.Array, Data: []byte{0x01, 0x02, 0x03, 0x04}},
194194
nil,
195-
[]interface{}{Document(nil), false},
195+
[]interface{}{Array(nil), false},
196196
},
197197
{
198198
"ArrayOK/Success", Value.ArrayOK, Value{Type: bsontype.Array, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00}},
199199
nil,
200-
[]interface{}{Document{0x05, 0x00, 0x00, 0x00, 0x00}, true},
200+
[]interface{}{Array{0x05, 0x00, 0x00, 0x00, 0x00}, true},
201201
},
202202
{
203203
"Binary/Not Binary", Value.Binary, Value{Type: bsontype.String},

x/mongo/driver/errors.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -370,12 +370,12 @@ func extractError(rdr bsoncore.Document) error {
370370
}
371371
case "errorLabels":
372372
if arr, okay := elem.Value().ArrayOK(); okay {
373-
elems, err := arr.Elements()
373+
vals, err := arr.Values()
374374
if err != nil {
375375
continue
376376
}
377-
for _, elem := range elems {
378-
if str, ok := elem.Value().StringValueOK(); ok {
377+
for _, val := range vals {
378+
if str, ok := val.StringValueOK(); ok {
379379
labels = append(labels, str)
380380
}
381381
}
@@ -427,12 +427,12 @@ func extractError(rdr bsoncore.Document) error {
427427
copy(wcError.WriteConcernError.Details, info)
428428
}
429429
if errLabels, exists := doc.Lookup("errorLabels").ArrayOK(); exists {
430-
elems, err := errLabels.Elements()
430+
vals, err := errLabels.Values()
431431
if err != nil {
432432
continue
433433
}
434-
for _, elem := range elems {
435-
if str, ok := elem.Value().StringValueOK(); ok {
434+
for _, val := range vals {
435+
if str, ok := val.StringValueOK(); ok {
436436
labels = append(labels, str)
437437
}
438438
}

x/mongo/driver/operation_legacy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func (op Operation) createLegacyKillCursorsWiremessage(dst []byte, desc descript
339339
}
340340

341341
var collName string
342-
var cursors bsoncore.Document
342+
var cursors bsoncore.Array
343343
for _, elem := range cmdElems {
344344
switch elem.Key() {
345345
case "killCursors":

0 commit comments

Comments
 (0)