Documentation
¶
Overview ¶
Package bsontools exposes various BSON-related tools.
For other MongoDB-related tools, see the mongotools package.
Index ¶
- func CompareBinaries(a, b bson.RawValue) (int, error)
- func CompareInt64s(a, b bson.RawValue) (int, error)
- func CompareStrings(a, b bson.RawValue) (int, error)
- func CountRawElements[D ~[]byte](doc D) (int, error)
- func GetComparableTypes() mapset.Set[bson.Type]
- func MarshalA[T ~[]byte](buf T, a bson.A) (bson.RawArray, error)
- func MarshalD[T ~[]byte](buf T, d bson.D) (bson.Raw, error)
- func RawElements[D ~[]byte](doc D) iter.Seq2[bson.RawElement, error]
- func RawLookup[T unmarshalTargets, D ~[]byte](in D, pointer ...string) (T, error)
- func RawValueTo[T unmarshalTargets](in bson.RawValue) (T, error)
- func RawValueToBinary(in bson.RawValue) (bson.Binary, error)
- func RawValueToInt(in bson.RawValue) (int, error)
- func RawValueToInt64(in bson.RawValue) (int64, error)
- func RawValueToString(in bson.RawValue) (string, error)
- func RawValueToStringBytes(in bson.RawValue) ([]byte, error)
- func RawValueToTimestamp(in bson.RawValue) (bson.Timestamp, error)
- func RemoveFromRaw[T ~[]byte](raw T, pointer ...string) (T, bool, error)
- func ReplaceInRaw[T ~[]byte](raw T, newValue bson.RawValue, pointer ...string) (T, bool, error)
- func SortFields(in bson.Raw) error
- func ToRawValue[T alwaysMarshalableTypes](in T) bson.RawValue
- func UnmarshalArray(raw bson.RawArray) (bson.A, error)
- func UnmarshalToD[D ~[]byte](raw D) (bson.D, error)
- type PointerTooDeepError
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CompareBinaries ¶
CompareBinaries compares two BSON binary strings per BSON sort order.
func CompareInt64s ¶
CompareInt64s compares two BSON int64s.
func CompareStrings ¶
CompareStrings compares two BSON strings.
func CountRawElements ¶
CountRawElements returns a count of the fields in the given BSON document.
func GetComparableTypes ¶
GetComparableTypes returns a Set of BSON types that this library can compare.
func MarshalD ¶
MarshalD marshals a bson.D to BSON, yielding the same result as bson.Marshal(). This avoids reflection, though, which significantly reduces CPU load. It can also marshal to a preexisting buffer, which lets you minimize GC churn.
func RawElements ¶
RawElements returns an iterator over a Raw’s elements.
If the iterator returns an error but the caller continues iterating, a panic will ensue.
func RawLookup ¶
RawLookup extracts & unmarshals a referent value from a BSON document. It’s like bson.Raw.LookupErr combined with RawValueTo.
func RawValueTo ¶
RawValueTo is a bit like bson.UnmarshalValue, but it’s much faster because it avoids reflection. The downside is that only certain types are supported.
This usually enforces strict numeric type equivalence. For example, it won’t coerce a float to an int64. If Go’s int type is the target, either int32 or int64 is acceptable.
Example usage:
str, err := RawValueTo[string](rv)
func RawValueToBinary ¶
RawValueToBinary is a more efficient RawValueTo[bson.Binary].
func RawValueToInt ¶
RawValueToInt is a more efficient RawValueTo[int].
func RawValueToInt64 ¶
RawValueToInt64 is a more efficient RawValueTo[int64].
func RawValueToString ¶
RawValueToString is a more efficient RawValueTo[string].
func RawValueToStringBytes ¶
RawValueToStringBytes is like RawValueToString but returns the raw buffer rather than allocating a new string.
func RawValueToTimestamp ¶
RawValueToTimestamp is a more efficient RawValueTo[bson.Timestamp].
func RemoveFromRaw ¶
RemoveFromRaw is like ReplaceInRaw, but it removes the element.
Example usage (replaces /role/title):
rawDoc, found, err = RemoveFromRaw(rawDoc, "role", "title")
func ReplaceInRaw ¶
ReplaceInRaw “surgically” replaces one value in a BSON document with another. Its returned bool indicates whether the value was found.
If any nonfinal node in the pointer is a scalar value (i.e., neither an array nor embedded document), a PointerTooDeepError is returned.
Example usage (replaces /role/title):
rawDoc, found, err = ReplaceInRaw(rawDoc, newRoleTitle, "role", "title")
func SortFields ¶
SortFields sorts a BSON document’s fields recursively. It modifies the provided bson.Raw directly.
func ToRawValue ¶
ToRawValue is a bit like bson.MarshalValue, but: - It’s faster since it avoids reflection. - It always succeeds since it only accepts certain known types.
func UnmarshalArray ¶
UnmarshalArray is like UnmarshalRaw but for an array.
Types ¶
type PointerTooDeepError ¶
type PointerTooDeepError struct {
// contains filtered or unexported fields
}
PointerTooDeepError is like POSIX’s ENOTDIR error: it indicates that a nonfinal element in a document pointer is a scalar value.
func (PointerTooDeepError) Error ¶
func (pe PointerTooDeepError) Error() string