// Copyright (C) MongoDB, Inc. 2017-present.
//
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at [Link]
package bsonx // import "[Link]/mongo-driver/x/bsonx"
import (
"bytes"
"errors"
"fmt"
"strconv"
"[Link]/mongo-driver/bson/bsontype"
"[Link]/mongo-driver/x/bsonx/bsoncore"
)
// ErrNilArray indicates that an operation was attempted on a nil *Array.
var ErrNilArray = [Link]("array is nil")
// Arr represents an array in BSON.
type Arr []Val
// String implements the [Link] interface.
func (a Arr) String() string {
var buf [Link]
[Link]([]byte("[Link]["))
for idx, val := range a {
if idx > 0 {
[Link]([]byte(", "))
}
[Link](&buf, "%s", val)
}
[Link](']')
return [Link]()
}
// MarshalBSONValue implements the [Link] interface.
func (a Arr) MarshalBSONValue() ([Link], []byte, error) {
if a == nil {
// TODO: Should we do this?
return [Link], nil, nil
}
idx, dst := [Link](nil)
for idx, value := range a {
t, data, _ := [Link]() // marshalBSONValue never
returns an error.
dst = append(dst, byte(t))
dst = append(dst, [Link](idx)...)
dst = append(dst, 0x00)
dst = append(dst, data...)
}
dst = append(dst, 0x00)
dst = [Link](dst, idx, int32(len(dst[idx:])))
return [Link], dst, nil
}
// UnmarshalBSONValue implements the [Link] interface.
func (a *Arr) UnmarshalBSONValue(t [Link], data []byte) error {
if a == nil {
return ErrNilArray
}
*a = (*a)[:0]
elements, err := [Link](data).Elements()
if err != nil {
return err
}
for _, elem := range elements {
var val Val
rawval := [Link]()
err = [Link]([Link], [Link])
if err != nil {
return err
}
*a = append(*a, val)
}
return nil
}
// Equal compares this document to another, returning true if they are equal.
func (a Arr) Equal(a2 Arr) bool {
if len(a) != len(a2) {
return false
}
for idx := range a {
if !a[idx].Equal(a2[idx]) {
return false
}
}
return true
}
func (Arr) idoc() {}