podman
78 строк · 2.5 Кб
1// Copyright (C) MongoDB, Inc. 2017-present.
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may
4// not use this file except in compliance with the License. You may obtain
5// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
6
7package bsonrw
8
9import (
10"go.mongodb.org/mongo-driver/bson/bsontype"
11"go.mongodb.org/mongo-driver/bson/primitive"
12)
13
14// ArrayWriter is the interface used to create a BSON or BSON adjacent array.
15// Callers must ensure they call WriteArrayEnd when they have finished creating
16// the array.
17type ArrayWriter interface {
18WriteArrayElement() (ValueWriter, error)
19WriteArrayEnd() error
20}
21
22// DocumentWriter is the interface used to create a BSON or BSON adjacent
23// document. Callers must ensure they call WriteDocumentEnd when they have
24// finished creating the document.
25type DocumentWriter interface {
26WriteDocumentElement(string) (ValueWriter, error)
27WriteDocumentEnd() error
28}
29
30// ValueWriter is the interface used to write BSON values. Implementations of
31// this interface handle creating BSON or BSON adjacent representations of the
32// values.
33type ValueWriter interface {
34WriteArray() (ArrayWriter, error)
35WriteBinary(b []byte) error
36WriteBinaryWithSubtype(b []byte, btype byte) error
37WriteBoolean(bool) error
38WriteCodeWithScope(code string) (DocumentWriter, error)
39WriteDBPointer(ns string, oid primitive.ObjectID) error
40WriteDateTime(dt int64) error
41WriteDecimal128(primitive.Decimal128) error
42WriteDouble(float64) error
43WriteInt32(int32) error
44WriteInt64(int64) error
45WriteJavascript(code string) error
46WriteMaxKey() error
47WriteMinKey() error
48WriteNull() error
49WriteObjectID(primitive.ObjectID) error
50WriteRegex(pattern, options string) error
51WriteString(string) error
52WriteDocument() (DocumentWriter, error)
53WriteSymbol(symbol string) error
54WriteTimestamp(t, i uint32) error
55WriteUndefined() error
56}
57
58// ValueWriterFlusher is a superset of ValueWriter that exposes functionality to flush to the underlying buffer.
59type ValueWriterFlusher interface {
60ValueWriter
61Flush() error
62}
63
64// BytesWriter is the interface used to write BSON bytes to a ValueWriter.
65// This interface is meant to be a superset of ValueWriter, so that types that
66// implement ValueWriter may also implement this interface.
67type BytesWriter interface {
68WriteValueBytes(t bsontype.Type, b []byte) error
69}
70
71// SliceWriter allows a pointer to a slice of bytes to be used as an io.Writer.
72type SliceWriter []byte
73
74func (sw *SliceWriter) Write(p []byte) (int, error) {
75written := len(p)
76*sw = append(*sw, p...)
77return written, nil
78}
79