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 bsoncore
8
9import (
10	"go.mongodb.org/mongo-driver/bson/bsontype"
11	"go.mongodb.org/mongo-driver/bson/primitive"
12)
13
14// DocumentBuilder builds a bson document
15type DocumentBuilder struct {
16	doc     []byte
17	indexes []int32
18}
19
20// startDocument reserves the document's length and set the index to where the length begins
21func (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// NewDocumentBuilder creates a new DocumentBuilder
29func NewDocumentBuilder() *DocumentBuilder {
30	return (&DocumentBuilder{}).startDocument()
31}
32
33// Build updates the length of the document and index to the beginning of the documents length
34// bytes, then returns the document (bson bytes)
35func (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// AppendInt32 will append an int32 element using key and i32 to DocumentBuilder.doc
43func (db *DocumentBuilder) AppendInt32(key string, i32 int32) *DocumentBuilder {
44	db.doc = AppendInt32Element(db.doc, key, i32)
45	return db
46}
47
48// AppendDocument will append a bson embeded document element using key
49// and doc to DocumentBuilder.doc
50func (db *DocumentBuilder) AppendDocument(key string, doc []byte) *DocumentBuilder {
51	db.doc = AppendDocumentElement(db.doc, key, doc)
52	return db
53}
54
55// AppendArray will append a bson array using key and arr to DocumentBuilder.doc
56func (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// AppendDouble will append a double element using key and f to DocumentBuilder.doc
63func (db *DocumentBuilder) AppendDouble(key string, f float64) *DocumentBuilder {
64	db.doc = AppendDoubleElement(db.doc, key, f)
65	return db
66}
67
68// AppendString will append str to DocumentBuilder.doc with the given key
69func (db *DocumentBuilder) AppendString(key string, str string) *DocumentBuilder {
70	db.doc = AppendStringElement(db.doc, key, str)
71	return db
72}
73
74// AppendObjectID will append oid to DocumentBuilder.doc with the given key
75func (db *DocumentBuilder) AppendObjectID(key string, oid primitive.ObjectID) *DocumentBuilder {
76	db.doc = AppendObjectIDElement(db.doc, key, oid)
77	return db
78}
79
80// AppendBinary will append a BSON binary element using key, subtype, and
81// b to db.doc
82func (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// AppendUndefined will append a BSON undefined element using key to db.doc
88func (db *DocumentBuilder) AppendUndefined(key string) *DocumentBuilder {
89	db.doc = AppendUndefinedElement(db.doc, key)
90	return db
91}
92
93// AppendBoolean will append a boolean element using key and b to db.doc
94func (db *DocumentBuilder) AppendBoolean(key string, b bool) *DocumentBuilder {
95	db.doc = AppendBooleanElement(db.doc, key, b)
96	return db
97}
98
99// AppendDateTime will append a datetime element using key and dt to db.doc
100func (db *DocumentBuilder) AppendDateTime(key string, dt int64) *DocumentBuilder {
101	db.doc = AppendDateTimeElement(db.doc, key, dt)
102	return db
103}
104
105// AppendNull will append a null element using key to db.doc
106func (db *DocumentBuilder) AppendNull(key string) *DocumentBuilder {
107	db.doc = AppendNullElement(db.doc, key)
108	return db
109}
110
111// AppendRegex will append pattern and options using key to db.doc
112func (db *DocumentBuilder) AppendRegex(key, pattern, options string) *DocumentBuilder {
113	db.doc = AppendRegexElement(db.doc, key, pattern, options)
114	return db
115}
116
117// AppendDBPointer will append ns and oid to using key to db.doc
118func (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// AppendJavaScript will append js using the provided key to db.doc
124func (db *DocumentBuilder) AppendJavaScript(key, js string) *DocumentBuilder {
125	db.doc = AppendJavaScriptElement(db.doc, key, js)
126	return db
127}
128
129// AppendSymbol will append a BSON symbol element using key and symbol db.doc
130func (db *DocumentBuilder) AppendSymbol(key, symbol string) *DocumentBuilder {
131	db.doc = AppendSymbolElement(db.doc, key, symbol)
132	return db
133}
134
135// AppendCodeWithScope will append code and scope using key to db.doc
136func (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// AppendTimestamp will append t and i to db.doc using provided key
142func (db *DocumentBuilder) AppendTimestamp(key string, t, i uint32) *DocumentBuilder {
143	db.doc = AppendTimestampElement(db.doc, key, t, i)
144	return db
145}
146
147// AppendInt64 will append i64 to dst using key to db.doc
148func (db *DocumentBuilder) AppendInt64(key string, i64 int64) *DocumentBuilder {
149	db.doc = AppendInt64Element(db.doc, key, i64)
150	return db
151}
152
153// AppendDecimal128 will append d128 to db.doc using provided key
154func (db *DocumentBuilder) AppendDecimal128(key string, d128 primitive.Decimal128) *DocumentBuilder {
155	db.doc = AppendDecimal128Element(db.doc, key, d128)
156	return db
157}
158
159// AppendMaxKey will append a max key element using key to db.doc
160func (db *DocumentBuilder) AppendMaxKey(key string) *DocumentBuilder {
161	db.doc = AppendMaxKeyElement(db.doc, key)
162	return db
163}
164
165// AppendMinKey will append a min key element using key to db.doc
166func (db *DocumentBuilder) AppendMinKey(key string) *DocumentBuilder {
167	db.doc = AppendMinKeyElement(db.doc, key)
168	return db
169}
170
171// AppendValue will append a BSON element with the provided key and value to the document.
172func (db *DocumentBuilder) AppendValue(key string, val Value) *DocumentBuilder {
173	db.doc = AppendValueElement(db.doc, key, val)
174	return db
175}
176
177// StartDocument starts building an inline document element with the provided key
178// After this document is completed, the user must call finishDocument
179func (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// FinishDocument builds the most recent document created
186func (db *DocumentBuilder) FinishDocument() *DocumentBuilder {
187	db.doc = db.Build()
188	return db
189}
190