1
2
3
4
5
6
7 package bsoncore
8
9 import (
10 "go.mongodb.org/mongo-driver/bson/bsontype"
11 "go.mongodb.org/mongo-driver/bson/primitive"
12 )
13
14
15 type DocumentBuilder struct {
16 doc []byte
17 indexes []int32
18 }
19
20
21 func (db *DocumentBuilder) startDocument() *DocumentBuilder {
22 var index int32
23 index, db.doc = AppendDocumentStart(db.doc)
24 db.indexes = append(db.indexes, index)
25 return db
26 }
27
28
29 func NewDocumentBuilder() *DocumentBuilder {
30 return (&DocumentBuilder{}).startDocument()
31 }
32
33
34
35 func (db *DocumentBuilder) Build() Document {
36 last := len(db.indexes) - 1
37 db.doc, _ = AppendDocumentEnd(db.doc, db.indexes[last])
38 db.indexes = db.indexes[:last]
39 return db.doc
40 }
41
42
43 func (db *DocumentBuilder) AppendInt32(key string, i32 int32) *DocumentBuilder {
44 db.doc = AppendInt32Element(db.doc, key, i32)
45 return db
46 }
47
48
49
50 func (db *DocumentBuilder) AppendDocument(key string, doc []byte) *DocumentBuilder {
51 db.doc = AppendDocumentElement(db.doc, key, doc)
52 return db
53 }
54
55
56 func (db *DocumentBuilder) AppendArray(key string, arr []byte) *DocumentBuilder {
57 db.doc = AppendHeader(db.doc, bsontype.Array, key)
58 db.doc = AppendArray(db.doc, arr)
59 return db
60 }
61
62
63 func (db *DocumentBuilder) AppendDouble(key string, f float64) *DocumentBuilder {
64 db.doc = AppendDoubleElement(db.doc, key, f)
65 return db
66 }
67
68
69 func (db *DocumentBuilder) AppendString(key string, str string) *DocumentBuilder {
70 db.doc = AppendStringElement(db.doc, key, str)
71 return db
72 }
73
74
75 func (db *DocumentBuilder) AppendObjectID(key string, oid primitive.ObjectID) *DocumentBuilder {
76 db.doc = AppendObjectIDElement(db.doc, key, oid)
77 return db
78 }
79
80
81
82 func (db *DocumentBuilder) AppendBinary(key string, subtype byte, b []byte) *DocumentBuilder {
83 db.doc = AppendBinaryElement(db.doc, key, subtype, b)
84 return db
85 }
86
87
88 func (db *DocumentBuilder) AppendUndefined(key string) *DocumentBuilder {
89 db.doc = AppendUndefinedElement(db.doc, key)
90 return db
91 }
92
93
94 func (db *DocumentBuilder) AppendBoolean(key string, b bool) *DocumentBuilder {
95 db.doc = AppendBooleanElement(db.doc, key, b)
96 return db
97 }
98
99
100 func (db *DocumentBuilder) AppendDateTime(key string, dt int64) *DocumentBuilder {
101 db.doc = AppendDateTimeElement(db.doc, key, dt)
102 return db
103 }
104
105
106 func (db *DocumentBuilder) AppendNull(key string) *DocumentBuilder {
107 db.doc = AppendNullElement(db.doc, key)
108 return db
109 }
110
111
112 func (db *DocumentBuilder) AppendRegex(key, pattern, options string) *DocumentBuilder {
113 db.doc = AppendRegexElement(db.doc, key, pattern, options)
114 return db
115 }
116
117
118 func (db *DocumentBuilder) AppendDBPointer(key string, ns string, oid primitive.ObjectID) *DocumentBuilder {
119 db.doc = AppendDBPointerElement(db.doc, key, ns, oid)
120 return db
121 }
122
123
124 func (db *DocumentBuilder) AppendJavaScript(key, js string) *DocumentBuilder {
125 db.doc = AppendJavaScriptElement(db.doc, key, js)
126 return db
127 }
128
129
130 func (db *DocumentBuilder) AppendSymbol(key, symbol string) *DocumentBuilder {
131 db.doc = AppendSymbolElement(db.doc, key, symbol)
132 return db
133 }
134
135
136 func (db *DocumentBuilder) AppendCodeWithScope(key string, code string, scope Document) *DocumentBuilder {
137 db.doc = AppendCodeWithScopeElement(db.doc, key, code, scope)
138 return db
139 }
140
141
142 func (db *DocumentBuilder) AppendTimestamp(key string, t, i uint32) *DocumentBuilder {
143 db.doc = AppendTimestampElement(db.doc, key, t, i)
144 return db
145 }
146
147
148 func (db *DocumentBuilder) AppendInt64(key string, i64 int64) *DocumentBuilder {
149 db.doc = AppendInt64Element(db.doc, key, i64)
150 return db
151 }
152
153
154 func (db *DocumentBuilder) AppendDecimal128(key string, d128 primitive.Decimal128) *DocumentBuilder {
155 db.doc = AppendDecimal128Element(db.doc, key, d128)
156 return db
157 }
158
159
160 func (db *DocumentBuilder) AppendMaxKey(key string) *DocumentBuilder {
161 db.doc = AppendMaxKeyElement(db.doc, key)
162 return db
163 }
164
165
166 func (db *DocumentBuilder) AppendMinKey(key string) *DocumentBuilder {
167 db.doc = AppendMinKeyElement(db.doc, key)
168 return db
169 }
170
171
172 func (db *DocumentBuilder) AppendValue(key string, val Value) *DocumentBuilder {
173 db.doc = AppendValueElement(db.doc, key, val)
174 return db
175 }
176
177
178
179 func (db *DocumentBuilder) StartDocument(key string) *DocumentBuilder {
180 db.doc = AppendHeader(db.doc, bsontype.EmbeddedDocument, key)
181 db = db.startDocument()
182 return db
183 }
184
185
186 func (db *DocumentBuilder) FinishDocument() *DocumentBuilder {
187 db.doc = db.Build()
188 return db
189 }
190
View as plain text