Skip to content

Commit d061b31

Browse files
committed
CDRIVER-2665 allow extra opts for bulk ops
1 parent 7ea4163 commit d061b31

File tree

5 files changed

+100
-77
lines changed

5 files changed

+100
-77
lines changed

build/generate-opts.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,11 @@ def __init__(self, items, **defaults):
192192
write_concern_option,
193193
ordered_option,
194194
session_option,
195-
], allow_extra=False, ordered='true')),
195+
], ordered='true')),
196196

197197
('mongoc_bulk_insert_opts_t', Struct([
198198
validate_option,
199-
], validate='_mongoc_default_insert_vflags', allow_extra=False)),
199+
], validate='_mongoc_default_insert_vflags')),
200200

201201
('mongoc_bulk_update_opts_t', Shared([
202202
validate_option,
@@ -214,23 +214,20 @@ def __init__(self, items, **defaults):
214214
array_filters_option,
215215
],
216216
multi='false',
217-
validate='_mongoc_default_update_vflags',
218-
allow_extra=False)),
217+
validate='_mongoc_default_update_vflags')),
219218

220219
('mongoc_bulk_update_many_opts_t', Struct(
221220
[
222221
('update', {'type': 'mongoc_bulk_update_opts_t'}),
223222
array_filters_option,
224223
],
225224
multi='true',
226-
validate='_mongoc_default_update_vflags',
227-
allow_extra=False)),
225+
validate='_mongoc_default_update_vflags')),
228226

229227
('mongoc_bulk_replace_one_opts_t', Struct(
230228
[('update', {'type': 'mongoc_bulk_update_opts_t'})],
231229
multi='false',
232-
validate='_mongoc_default_replace_vflags',
233-
allow_extra=False)),
230+
validate='_mongoc_default_replace_vflags')),
234231

235232
('mongoc_bulk_remove_opts_t', Shared([
236233
collation_option,
@@ -239,11 +236,11 @@ def __init__(self, items, **defaults):
239236

240237
('mongoc_bulk_remove_one_opts_t', Struct([
241238
('remove', {'type': 'mongoc_bulk_remove_opts_t'}),
242-
], limit=1, allow_extra=False)),
239+
], limit=1)),
243240

244241
('mongoc_bulk_remove_many_opts_t', Struct([
245242
('remove', {'type': 'mongoc_bulk_remove_opts_t'}),
246-
], limit=0, allow_extra=False)),
243+
], limit=0)),
247244

248245
('mongoc_create_index_opts_t', Struct([
249246
write_concern_option,

build/opts_templates/mongoc-opts-private.h.template

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
#ifndef MONGOC_OPTS_H
22
#define MONGOC_OPTS_H
33

4-
#include <bson.h>
4+
#include <bson/bson.h>
55

6-
#include "mongoc-client-session.h"
7-
#include "mongoc-bulk-operation-private.h"
6+
#include "mongoc/mongoc-client-session.h"
7+
#include "mongoc/mongoc-bulk-operation-private.h"
88

99
{{ header_comment }}
1010

build/opts_templates/mongoc-opts.c.template

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
#include "mongoc-opts-helpers-private.h"
2-
#include "mongoc-opts-private.h"
3-
#include "mongoc-error.h"
4-
#include "mongoc-util-private.h"
5-
#include "mongoc-client-private.h"
1+
#include "mongoc/mongoc-opts-helpers-private.h"
2+
#include "mongoc/mongoc-opts-private.h"
3+
#include "mongoc/mongoc-error.h"
4+
#include "mongoc/mongoc-util-private.h"
5+
#include "mongoc/mongoc-client-private.h"
66

77
{{ header_comment }}
88
{% for struct_type, description in opts_structs.items() %}

src/libmongoc/src/mongoc/mongoc-opts.c

Lines changed: 77 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -827,12 +827,17 @@ _mongoc_bulk_opts_parse (
827827
}
828828
}
829829
else {
830-
bson_set_error (error,
831-
MONGOC_ERROR_COMMAND,
832-
MONGOC_ERROR_COMMAND_INVALID_ARG,
833-
"Invalid option '%s'",
834-
bson_iter_key (&iter));
835-
return false;
830+
/* unrecognized values are copied to "extra" */
831+
if (!BSON_APPEND_VALUE (
832+
&mongoc_bulk_opts->extra,
833+
bson_iter_key (&iter),
834+
bson_iter_value (&iter))) {
835+
bson_set_error (error,
836+
MONGOC_ERROR_BSON,
837+
MONGOC_ERROR_BSON_INVALID,
838+
"Invalid 'opts' parameter.");
839+
return false;
840+
}
836841
}
837842
}
838843

@@ -883,12 +888,17 @@ _mongoc_bulk_insert_opts_parse (
883888
}
884889
}
885890
else {
886-
bson_set_error (error,
887-
MONGOC_ERROR_COMMAND,
888-
MONGOC_ERROR_COMMAND_INVALID_ARG,
889-
"Invalid option '%s'",
890-
bson_iter_key (&iter));
891-
return false;
891+
/* unrecognized values are copied to "extra" */
892+
if (!BSON_APPEND_VALUE (
893+
&mongoc_bulk_insert_opts->extra,
894+
bson_iter_key (&iter),
895+
bson_iter_value (&iter))) {
896+
bson_set_error (error,
897+
MONGOC_ERROR_BSON,
898+
MONGOC_ERROR_BSON_INVALID,
899+
"Invalid 'opts' parameter.");
900+
return false;
901+
}
892902
}
893903
}
894904

@@ -976,12 +986,17 @@ _mongoc_bulk_update_one_opts_parse (
976986
}
977987
}
978988
else {
979-
bson_set_error (error,
980-
MONGOC_ERROR_COMMAND,
981-
MONGOC_ERROR_COMMAND_INVALID_ARG,
982-
"Invalid option '%s'",
983-
bson_iter_key (&iter));
984-
return false;
989+
/* unrecognized values are copied to "extra" */
990+
if (!BSON_APPEND_VALUE (
991+
&mongoc_bulk_update_one_opts->extra,
992+
bson_iter_key (&iter),
993+
bson_iter_value (&iter))) {
994+
bson_set_error (error,
995+
MONGOC_ERROR_BSON,
996+
MONGOC_ERROR_BSON_INVALID,
997+
"Invalid 'opts' parameter.");
998+
return false;
999+
}
9851000
}
9861001
}
9871002

@@ -1071,12 +1086,17 @@ _mongoc_bulk_update_many_opts_parse (
10711086
}
10721087
}
10731088
else {
1074-
bson_set_error (error,
1075-
MONGOC_ERROR_COMMAND,
1076-
MONGOC_ERROR_COMMAND_INVALID_ARG,
1077-
"Invalid option '%s'",
1078-
bson_iter_key (&iter));
1079-
return false;
1089+
/* unrecognized values are copied to "extra" */
1090+
if (!BSON_APPEND_VALUE (
1091+
&mongoc_bulk_update_many_opts->extra,
1092+
bson_iter_key (&iter),
1093+
bson_iter_value (&iter))) {
1094+
bson_set_error (error,
1095+
MONGOC_ERROR_BSON,
1096+
MONGOC_ERROR_BSON_INVALID,
1097+
"Invalid 'opts' parameter.");
1098+
return false;
1099+
}
10801100
}
10811101
}
10821102

@@ -1156,12 +1176,17 @@ _mongoc_bulk_replace_one_opts_parse (
11561176
}
11571177
}
11581178
else {
1159-
bson_set_error (error,
1160-
MONGOC_ERROR_COMMAND,
1161-
MONGOC_ERROR_COMMAND_INVALID_ARG,
1162-
"Invalid option '%s'",
1163-
bson_iter_key (&iter));
1164-
return false;
1179+
/* unrecognized values are copied to "extra" */
1180+
if (!BSON_APPEND_VALUE (
1181+
&mongoc_bulk_replace_one_opts->extra,
1182+
bson_iter_key (&iter),
1183+
bson_iter_value (&iter))) {
1184+
bson_set_error (error,
1185+
MONGOC_ERROR_BSON,
1186+
MONGOC_ERROR_BSON_INVALID,
1187+
"Invalid 'opts' parameter.");
1188+
return false;
1189+
}
11651190
}
11661191
}
11671192

@@ -1220,12 +1245,17 @@ _mongoc_bulk_remove_one_opts_parse (
12201245
}
12211246
}
12221247
else {
1223-
bson_set_error (error,
1224-
MONGOC_ERROR_COMMAND,
1225-
MONGOC_ERROR_COMMAND_INVALID_ARG,
1226-
"Invalid option '%s'",
1227-
bson_iter_key (&iter));
1228-
return false;
1248+
/* unrecognized values are copied to "extra" */
1249+
if (!BSON_APPEND_VALUE (
1250+
&mongoc_bulk_remove_one_opts->extra,
1251+
bson_iter_key (&iter),
1252+
bson_iter_value (&iter))) {
1253+
bson_set_error (error,
1254+
MONGOC_ERROR_BSON,
1255+
MONGOC_ERROR_BSON_INVALID,
1256+
"Invalid 'opts' parameter.");
1257+
return false;
1258+
}
12291259
}
12301260
}
12311261

@@ -1284,12 +1314,17 @@ _mongoc_bulk_remove_many_opts_parse (
12841314
}
12851315
}
12861316
else {
1287-
bson_set_error (error,
1288-
MONGOC_ERROR_COMMAND,
1289-
MONGOC_ERROR_COMMAND_INVALID_ARG,
1290-
"Invalid option '%s'",
1291-
bson_iter_key (&iter));
1292-
return false;
1317+
/* unrecognized values are copied to "extra" */
1318+
if (!BSON_APPEND_VALUE (
1319+
&mongoc_bulk_remove_many_opts->extra,
1320+
bson_iter_key (&iter),
1321+
bson_iter_value (&iter))) {
1322+
bson_set_error (error,
1323+
MONGOC_ERROR_BSON,
1324+
MONGOC_ERROR_BSON_INVALID,
1325+
"Invalid 'opts' parameter.");
1326+
return false;
1327+
}
12931328
}
12941329
}
12951330

src/libmongoc/tests/test-mongoc-bulk.c

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -234,7 +234,6 @@ _test_opt (const char *opts_json, const char *msg)
234234
static void
235235
test_opts (void)
236236
{
237-
_test_opt ("{'foo': 1}", "Invalid option 'foo'");
238237
_test_opt ("{'writeConcern': 1}", "Invalid writeConcern");
239238
_test_opt ("{'writeConcern': {'w': 0, 'j': 1}}", "Invalid writeConcern");
240239
_test_opt ("{'sessionId': 'hi'}", "Invalid sessionId");
@@ -4451,19 +4450,14 @@ test_bulk_opts_parse (void)
44514450
MONGOC_ERROR_COMMAND_INVALID_ARG, \
44524451
"Invalid " _msg)
44534452

4454-
RM_ERR ("option 'foo'", remove_one, tmp_bson ("{'foo': 1}"));
4455-
RM_ERR ("option 'foo'", remove_many, tmp_bson ("{'foo': 1}"));
44564453
RM_ERR ("\"limit\" in opts: 2", remove_one, tmp_bson ("{'limit': 2}"));
44574454
RM_ERR ("\"limit\" in opts: 2", remove_many, tmp_bson ("{'limit': 2}"));
44584455
RM_ERR ("\"limit\" in opts: 0", remove_one, tmp_bson ("{'limit': 0}"));
44594456
RM_ERR ("\"limit\" in opts: 1", remove_many, tmp_bson ("{'limit': 1}"));
44604457

4461-
UPDATE_ERR ("option 'foo'", one, tmp_bson ("{'foo': 1}"));
4462-
UPDATE_ERR ("option 'foo'", many, tmp_bson ("{'foo': 1}"));
44634458
UPDATE_ERR ("\"multi\" in opts: true", one, tmp_bson ("{'multi': true}"));
44644459
UPDATE_ERR ("\"multi\" in opts: false", many, tmp_bson ("{'multi': false}"));
44654460

4466-
REPLACE_ERR ("option 'foo'", tmp_bson ("{'foo': 1}"));
44674461
REPLACE_ERR ("\"multi\": true in opts", tmp_bson ("{'multi': true}"));
44684462

44694463
#define NO_ERR(_fn, ...) \
@@ -4477,6 +4471,11 @@ test_bulk_opts_parse (void)
44774471
NO_ERR (update_one, q, u, tmp_bson ("{'multi': false}"));
44784472
NO_ERR (update_many, q, u, tmp_bson ("{'multi': true}"));
44794473
NO_ERR (replace_one, q, repl, tmp_bson ("{'multi': false}"));
4474+
NO_ERR (remove_one, q, tmp_bson ("{'foo': 1}"));
4475+
NO_ERR (remove_many, q, tmp_bson ("{'foo': 1}"));
4476+
NO_ERR (update_one, q, u, tmp_bson ("{'foo': 1}"));
4477+
NO_ERR (update_many, q, u, tmp_bson ("{'foo': 1}"));
4478+
NO_ERR (replace_one, q, repl, tmp_bson ("{'foo': 1}"));
44804479

44814480
mongoc_bulk_operation_destroy (bulk);
44824481
mongoc_collection_destroy (collection);
@@ -4519,31 +4518,23 @@ test_bulk_bypass_document_validation (void)
45194518
client = test_framework_client_new ();
45204519
collection = get_test_collection (client, "bypass_validation");
45214520

4522-
/* bypassDocumentValidation can't be passed in opts */
4521+
/* bypassDocumentValidation can be passed in opts, but is ignored */
45234522
bulk = mongoc_collection_create_bulk_operation_with_opts (
45244523
collection, tmp_bson ("{'bypassDocumentValidation': true}"));
45254524

45264525
i = mongoc_bulk_operation_execute (bulk, NULL, &error);
45274526
ASSERT_CMPUINT32 (i, ==, (uint32_t) 0);
4528-
ASSERT_ERROR_CONTAINS (error,
4529-
MONGOC_ERROR_COMMAND,
4530-
MONGOC_ERROR_COMMAND_INVALID_ARG,
4531-
"Invalid option 'bypassDocumentValidation'");
45324527

45334528
mongoc_bulk_operation_destroy (bulk);
45344529

4535-
/* not allowed in insert opts either */
4530+
/* same for insert opts */
45364531
bulk = mongoc_collection_create_bulk_operation_with_opts (collection, NULL);
45374532
r = mongoc_bulk_operation_insert_with_opts (
45384533
bulk,
45394534
tmp_bson ("{}"),
45404535
tmp_bson ("{'bypassDocumentValidation': true}"),
45414536
&error);
4542-
BSON_ASSERT (!r);
4543-
ASSERT_ERROR_CONTAINS (error,
4544-
MONGOC_ERROR_COMMAND,
4545-
MONGOC_ERROR_COMMAND_INVALID_ARG,
4546-
"Invalid option 'bypassDocumentValidation'");
4537+
BSON_ASSERT (r);
45474538

45484539
mongoc_bulk_operation_destroy (bulk);
45494540
mongoc_collection_destroy (collection);

0 commit comments

Comments
 (0)