-
Notifications
You must be signed in to change notification settings - Fork 455
CDRIVER-504 add bson_array_builder_t
#1344
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 14 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
932bbee
format test-bson.c
kevinAlbs 3b8e2b2
document and test `bson_uint32_to_string` behavior when input buffer …
kevinAlbs 9787340
add `ASSERT_BSON_EQUAL_BSON` test macro
kevinAlbs a1f6bfc
prototype `bson_array_builder_t`
kevinAlbs a9f5777
add remaining `bson_array_builder_append_*` functions
kevinAlbs 0ce6ff7
fill in TODOs with filed ticket
kevinAlbs 0e25ef0
remove `bson_array_builder_append_array_begin`
kevinAlbs fc69695
use `bson_array_builder_t`
kevinAlbs 952f591
move tutorial `Appending BSON` example code to separate file
kevinAlbs 3cbd65d
add doc page for `bson_array_builder_t` and update doc examples
kevinAlbs 766e922
link to `bson_array_builder_t` doc page
kevinAlbs 6437801
colocate `BSON_APPEND_*` macros with functions
kevinAlbs 968d91d
add `BSON_APPEND_ITER` and `BSON_APPEND_NOW_UTC`
kevinAlbs d492e15
colocate `bson_append_*` with `bson_array_builder_append_*` functions
kevinAlbs 58e87b8
use indices 9, 8, 7 in example
kevinAlbs 3b8128f
update indices of other example
kevinAlbs eb468cb
Merge branch 'master' into C504
kevinAlbs File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ API Reference | |
:maxdepth: 2 | ||
|
||
bson_t | ||
bson_array_builder_t | ||
bson_context_t | ||
bson_decimal128_t | ||
bson_error_t | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,3 +26,7 @@ Returns | |
------- | ||
|
||
Returns ``true`` if successful. | ||
|
||
.. seealso:: | ||
|
||
| :symbol:`bson_array_builder_t` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,212 @@ | ||
:man_page: bson_array_builder_t | ||
|
||
bson_array_builder_t | ||
==================== | ||
|
||
.. code-block:: c | ||
|
||
typedef struct _bson_array_builder_t bson_array_builder_t; | ||
|
||
``bson_array_builder_t`` may be used to build BSON arrays. ``bson_array_builder_t`` internally tracks and uses the array index as a key ("0", "1", "2", ...) when appending elements. | ||
|
||
Appending an array value | ||
------------------------ | ||
|
||
.. code-block:: c | ||
|
||
typedef struct _bson_array_builder_t bson_array_builder_t; | ||
|
||
bool | ||
bson_append_array_builder_begin (bson_t *bson, | ||
const char *key, | ||
int key_length, | ||
bson_array_builder_t **child); | ||
|
||
bool | ||
bson_append_array_builder_end (bson_t *bson, bson_array_builder_t *child); | ||
|
||
#define BSON_APPEND_ARRAY_BUILDER_BEGIN(b, key, child) \ | ||
bson_append_array_builder_begin (b, key, (int) strlen (key), child) | ||
|
||
``bson_append_array_builder_begin`` may be used to append an array as a value. Example: | ||
|
||
.. literalinclude:: ../examples/creating.c | ||
:language: c | ||
:start-after: // bson_array_builder_t example ... begin | ||
:end-before: // bson_array_builder_t example ... end | ||
:dedent: 6 | ||
|
||
Creating a top-level array | ||
-------------------------- | ||
|
||
.. code-block:: c | ||
|
||
bson_array_builder_t * bson_array_builder_new (void); | ||
|
||
bool | ||
bson_array_builder_build (bson_array_builder_t *bab, bson_t *out); | ||
|
||
BSON_EXPORT (void) | ||
bson_array_builder_destroy (bson_array_builder_t *bab); | ||
|
||
``bson_array_builder_new`` and ``bson_array_builder_build`` may be used to build a top-level BSON array. ``bson_array_builder_build`` initializes and moves BSON data to ``out``. The ``bson_array_builder_t`` may be reused and will start appending a new array at index "0": | ||
|
||
Example: | ||
|
||
.. literalinclude:: ../examples/creating.c | ||
:language: c | ||
:start-after: // bson_array_builder_t top-level example ... begin | ||
:end-before: // bson_array_builder_t top-level example ... end | ||
:dedent: 6 | ||
|
||
Appending values to an array | ||
---------------------------- | ||
|
||
``bson_array_builder_append_*`` functions are provided to append values to a BSON array. The ``bson_array_builder_append_*`` functions internally use ``bson_append_*`` and provide the array index as a key: | ||
|
||
.. code-block:: c | ||
|
||
bool | ||
bson_array_builder_append_value (bson_array_builder_t *bab, | ||
const bson_value_t *value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_array (bson_array_builder_t *bab, | ||
const bson_t *array); | ||
|
||
|
||
bool | ||
bson_array_builder_append_binary (bson_array_builder_t *bab, | ||
bson_subtype_t subtype, | ||
const uint8_t *binary, | ||
uint32_t length); | ||
|
||
bool | ||
bson_array_builder_append_bool (bson_array_builder_t *bab, bool value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_code (bson_array_builder_t *bab, | ||
const char *javascript); | ||
|
||
|
||
bool | ||
bson_array_builder_append_code_with_scope (bson_array_builder_t *bab, | ||
const char *javascript, | ||
const bson_t *scope); | ||
|
||
|
||
bool | ||
bson_array_builder_append_dbpointer (bson_array_builder_t *bab, | ||
const char *collection, | ||
const bson_oid_t *oid); | ||
|
||
|
||
bool | ||
bson_array_builder_append_double (bson_array_builder_t *bab, double value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_document (bson_array_builder_t *bab, | ||
const bson_t *value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_document_begin (bson_array_builder_t *bab, | ||
bson_t *child); | ||
|
||
|
||
bool | ||
bson_array_builder_append_document_end (bson_array_builder_t *bab, | ||
bson_t *child); | ||
|
||
bool | ||
bson_array_builder_append_int32 (bson_array_builder_t *bab, int32_t value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_int64 (bson_array_builder_t *bab, int64_t value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_decimal128 (bson_array_builder_t *bab, | ||
const bson_decimal128_t *value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_iter (bson_array_builder_t *bab, | ||
const bson_iter_t *iter); | ||
|
||
|
||
bool | ||
bson_array_builder_append_minkey (bson_array_builder_t *bab); | ||
|
||
|
||
bool | ||
bson_array_builder_append_maxkey (bson_array_builder_t *bab); | ||
|
||
|
||
bool | ||
bson_array_builder_append_null (bson_array_builder_t *bab); | ||
|
||
|
||
bool | ||
bson_array_builder_append_oid (bson_array_builder_t *bab, | ||
const bson_oid_t *oid); | ||
|
||
|
||
bool | ||
bson_array_builder_append_regex (bson_array_builder_t *bab, | ||
const char *regex, | ||
const char *options); | ||
|
||
|
||
bool | ||
bson_array_builder_append_regex_w_len (bson_array_builder_t *bab, | ||
const char *regex, | ||
int regex_length, | ||
const char *options); | ||
|
||
bool | ||
bson_array_builder_append_utf8 (bson_array_builder_t *bab, | ||
const char *value, | ||
int length); | ||
|
||
bool | ||
bson_array_builder_append_symbol (bson_array_builder_t *bab, | ||
const char *value, | ||
int length); | ||
|
||
bool | ||
bson_array_builder_append_time_t (bson_array_builder_t *bab, time_t value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_timeval (bson_array_builder_t *bab, | ||
struct timeval *value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_date_time (bson_array_builder_t *bab, int64_t value); | ||
|
||
|
||
bool | ||
bson_array_builder_append_now_utc (bson_array_builder_t *bab); | ||
|
||
|
||
bool | ||
bson_array_builder_append_timestamp (bson_array_builder_t *bab, | ||
uint32_t timestamp, | ||
uint32_t increment); | ||
|
||
bool | ||
bson_array_builder_append_undefined (bson_array_builder_t *bab); | ||
|
||
bool | ||
bson_array_builder_append_array_builder_begin (bson_array_builder_t *bab, | ||
bson_array_builder_t **child); | ||
|
||
bool | ||
bson_array_builder_append_array_builder_end (bson_array_builder_t *bab, | ||
bson_array_builder_t *child); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* Copyright 2017 MongoDB Inc. | ||
* | ||
* 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 | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include <stdio.h> | ||
#include <bson/bson.h> | ||
|
||
int | ||
main (int argc, const char **argv) | ||
{ | ||
{ | ||
// bson_append_document_begin example ... begin | ||
bson_t parent = BSON_INITIALIZER; | ||
bson_t child; | ||
|
||
bson_append_document_begin (&parent, "foo", 3, &child); | ||
bson_append_int32 (&child, "baz", 3, 1); | ||
bson_append_document_end (&parent, &child); | ||
|
||
char *str = bson_as_relaxed_extended_json (&parent, NULL); | ||
printf ("%s\n", str); // Prints: { "foo" : { "baz" : 1 } } | ||
bson_free (str); | ||
|
||
bson_destroy (&parent); | ||
// bson_append_document_begin example ... end | ||
} | ||
|
||
{ | ||
// bson_array_builder_t example ... begin | ||
bson_t parent = BSON_INITIALIZER; | ||
bson_array_builder_t *bab; | ||
|
||
bson_append_array_builder_begin (&parent, "foo", 3, &bab); | ||
bson_array_builder_append_int32 (bab, 1); | ||
bson_array_builder_append_int32 (bab, 2); | ||
bson_array_builder_append_int32 (bab, 3); | ||
bson_append_array_builder_end (&parent, bab); | ||
|
||
char *str = bson_as_relaxed_extended_json (&parent, NULL); | ||
printf ("%s\n", str); // Prints: { "foo" : [ 1, 2, 3 ] } | ||
kevinAlbs marked this conversation as resolved.
Show resolved
Hide resolved
|
||
bson_free (str); | ||
|
||
bson_destroy (&parent); | ||
// bson_array_builder_t example ... end | ||
} | ||
|
||
{ | ||
// bson_array_builder_t top-level example ... begin | ||
bson_t out; | ||
bson_array_builder_t *bab = bson_array_builder_new (); | ||
|
||
bson_array_builder_append_int32 (bab, 1); | ||
bson_array_builder_append_int32 (bab, 2); | ||
bson_array_builder_append_int32 (bab, 3); | ||
bson_array_builder_build (bab, &out); | ||
|
||
char *str = bson_array_as_relaxed_extended_json (&out, NULL); | ||
printf ("%s\n", str); // Prints: [ 1, 2, 3 ] | ||
bson_free (str); | ||
|
||
bson_array_builder_destroy (bab); | ||
// bson_array_builder_t top-level example ... end | ||
} | ||
|
||
return 0; | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.