-
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
Conversation
There may no use case. It adds risk users may use incorrect keys. The `bson_array_builder_append_array_builder_begin` may be preferable.
Replace existing use of `bson_append_array_begin`. bcon-speed.c was deliberately not modified to use `bson_array_builder_append_array_begin` to avoid changing performance.
Intends to make it easier to spot if a macro is missing.
Intends to make it easier to spot if a function is missing.
Co-authored-by: Adrian Dole <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Trying to append an array that already exists results in duplicate keys. static void
test (void)
{
bson_t *doc = tmp_bson ("{'a': 1, 'b': [3,2,1]}");
bson_array_builder_t *bab;
bson_append_array_builder_begin (doc, "b", 1, &bab);
bson_array_builder_append_int32 (bab, 9);
bson_array_builder_append_int32 (bab, 8);
bson_append_array_builder_end (doc, bab);
const char *s = bson_as_json (doc, NULL);
printf ("%s\n", s); // { "a" : 1, "b" : [ 3, 2, 1 ], "b" : [ 9, 8 ] }
} I see that we generally don't try to stop users from creating duplicate keys, but I feel like this one is particularly easy to do accidentally. Not sure if that warrants an assert. |
libbson does not attempt to stop users from creating duplicate keys. CDRIVER-2982 was resolved to document this as a warning to users. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Summary
bson_array_builder_t
to build BSON arrays without specifying keys.Verified with this patch build: https://spruce.mongodb.com/version/64b146ad3627e06d3165c766
Other improvements
BSON_APPEND_ITER
andBSON_APPEND_NOW_UTC
.bson_uint32_to_string
behavior when input buffer is too small.Background & Motivation
https://bsonspec.org/spec.html describes the expectation of BSON array keys:
bson_append_*
functions require the caller to specify the keys. Here is an example from the Tutorial:SERVER-21946 describes a case where misuse of the libbson API resulted in building a BSON array with repeated "0" keys, resulting in unexpected server behavior.
This PR provides an alternative API to build BSON arrays with
bson_array_builder_t
which does not require keys be specified:Rejected idea: reuse
bson_t
This alternative was considered: modify
bson_t
to support tracking the last array index:This idea was rejected due in favor of adding a separate
bson_array_builder_type
. Reusingbson_t
may risk users more easily misuse the API.