Skip to content

Commit

Permalink
GODRIVER-1930 bsoncore Array functions should return Array (#626)
Browse files Browse the repository at this point in the history
  • Loading branch information
benjirewis authored and Benjamin Rewis committed Apr 16, 2021
1 parent c317b0c commit 3525304
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 56 deletions.
2 changes: 1 addition & 1 deletion x/bsonx/bsoncore/array_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ func TestArray(t *testing.T) {
'\x00', '\x00',
},
`[[null,null]]`,
`Array(19)[[null ,null ]]`,
`Array(19)[Array(11)[null,null]]`,
},
{
"malformed--length too small",
Expand Down
2 changes: 1 addition & 1 deletion x/bsonx/bsoncore/bsoncore.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ func BuildArrayElement(dst []byte, key string, values ...Value) []byte {

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

// AppendBinary will append subtype and b to dst and return the extended buffer.
func AppendBinary(dst []byte, subtype byte, b []byte) []byte {
Expand Down
6 changes: 3 additions & 3 deletions x/bsonx/bsoncore/bsoncore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -548,19 +548,19 @@ func TestRead(t *testing.T) {
"ReadArray/not enough bytes (length)",
ReadArray,
[]byte{},
[]interface{}{Document(nil), []byte{}, false},
[]interface{}{Array(nil), []byte{}, false},
},
{
"ReadArray/not enough bytes (value)",
ReadArray,
[]byte{0x0F, 0x00, 0x00, 0x00},
[]interface{}{Document(nil), []byte{0x0F, 0x00, 0x00, 0x00}, false},
[]interface{}{Array(nil), []byte{0x0F, 0x00, 0x00, 0x00}, false},
},
{
"ReadArray/success",
ReadArray,
[]byte{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00},
[]interface{}{Document{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00}, []byte{}, true},
[]interface{}{Array{0x08, 0x00, 0x00, 0x00, 0x0A, '0', 0x00, 0x00}, []byte{}, true},
},
{
"ReadBinary/not enough bytes (length)",
Expand Down
3 changes: 2 additions & 1 deletion x/bsonx/bsoncore/document.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ func (d Document) LookupErr(key ...string) (Value, error) {
}
return val, nil
case bsontype.Array:
val, err := elem.Value().Array().LookupErr(key[1:]...)
// Convert to Document to continue Lookup recursion.
val, err := Document(elem.Value().Array()).LookupErr(key[1:]...)
if err != nil {
return Value{}, err
}
Expand Down
43 changes: 4 additions & 39 deletions x/bsonx/bsoncore/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ func (v Value) String() string {
if !ok {
return ""
}
return docAsArray(arr, false)
return arr.String()
case bsontype.Binary:
subtype, data, ok := v.BinaryOK()
if !ok {
Expand Down Expand Up @@ -366,7 +366,7 @@ func (v Value) DebugString() string {
if !ok {
return "<malformed>"
}
return docAsArray(arr, true)
return arr.DebugString()
case bsontype.CodeWithScope:
code, scope, ok := v.CodeWithScopeOK()
if !ok {
Expand Down Expand Up @@ -464,7 +464,7 @@ func (v Value) DocumentOK() (Document, bool) {

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

// ArrayOK is the same as Array, except it returns a boolean instead
// of panicking.
func (v Value) ArrayOK() (Document, bool) {
func (v Value) ArrayOK() (Array, bool) {
if v.Type != bsontype.Array {
return nil, false
}
Expand Down Expand Up @@ -978,38 +978,3 @@ func sortStringAlphebeticAscending(s string) string {
sort.Sort(ss)
return string([]rune(ss))
}

func docAsArray(d Document, debug bool) string {
if len(d) < 5 {
return ""
}
var buf bytes.Buffer
buf.WriteByte('[')

length, rem, _ := ReadLength(d) // We know we have enough bytes to read the length

length -= 4

var elem Element
var ok bool
first := true
for length > 1 {
if !first {
buf.WriteByte(',')
}
elem, rem, ok = ReadElement(rem)
length -= int32(len(elem))
if !ok {
return ""
}
if debug {
fmt.Fprintf(&buf, "%s ", elem.Value().DebugString())
} else {
fmt.Fprintf(&buf, "%s", elem.Value())
}
first = false
}
buf.WriteByte(']')

return buf.String()
}
8 changes: 4 additions & 4 deletions x/bsonx/bsoncore/value_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,22 +182,22 @@ func TestValue(t *testing.T) {
{
"Array/Success", Value.Array, Value{Type: bsontype.Array, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00}},
nil,
[]interface{}{Document{0x05, 0x00, 0x00, 0x00, 0x00}},
[]interface{}{Array{0x05, 0x00, 0x00, 0x00, 0x00}},
},
{
"ArrayOK/Not Array", Value.ArrayOK, Value{Type: bsontype.String},
nil,
[]interface{}{Document(nil), false},
[]interface{}{Array(nil), false},
},
{
"ArrayOK/Insufficient Bytes", Value.ArrayOK, Value{Type: bsontype.Array, Data: []byte{0x01, 0x02, 0x03, 0x04}},
nil,
[]interface{}{Document(nil), false},
[]interface{}{Array(nil), false},
},
{
"ArrayOK/Success", Value.ArrayOK, Value{Type: bsontype.Array, Data: []byte{0x05, 0x00, 0x00, 0x00, 0x00}},
nil,
[]interface{}{Document{0x05, 0x00, 0x00, 0x00, 0x00}, true},
[]interface{}{Array{0x05, 0x00, 0x00, 0x00, 0x00}, true},
},
{
"Binary/Not Binary", Value.Binary, Value{Type: bsontype.String},
Expand Down
12 changes: 6 additions & 6 deletions x/mongo/driver/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,12 +370,12 @@ func extractError(rdr bsoncore.Document) error {
}
case "errorLabels":
if arr, okay := elem.Value().ArrayOK(); okay {
elems, err := arr.Elements()
vals, err := arr.Values()
if err != nil {
continue
}
for _, elem := range elems {
if str, ok := elem.Value().StringValueOK(); ok {
for _, val := range vals {
if str, ok := val.StringValueOK(); ok {
labels = append(labels, str)
}
}
Expand Down Expand Up @@ -427,12 +427,12 @@ func extractError(rdr bsoncore.Document) error {
copy(wcError.WriteConcernError.Details, info)
}
if errLabels, exists := doc.Lookup("errorLabels").ArrayOK(); exists {
elems, err := errLabels.Elements()
vals, err := errLabels.Values()
if err != nil {
continue
}
for _, elem := range elems {
if str, ok := elem.Value().StringValueOK(); ok {
for _, val := range vals {
if str, ok := val.StringValueOK(); ok {
labels = append(labels, str)
}
}
Expand Down
2 changes: 1 addition & 1 deletion x/mongo/driver/operation_legacy.go
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ func (op Operation) createLegacyKillCursorsWiremessage(dst []byte, desc descript
}

var collName string
var cursors bsoncore.Document
var cursors bsoncore.Array
for _, elem := range cmdElems {
switch elem.Key() {
case "killCursors":
Expand Down

0 comments on commit 3525304

Please sign in to comment.