Skip to content

Commit 0d76435

Browse files
CDRIVER-3918 expose server api options as mongoc_optional_t
1 parent 33feb5c commit 0d76435

File tree

7 files changed

+133
-12
lines changed

7 files changed

+133
-12
lines changed

src/libmongoc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -597,6 +597,7 @@ set (HEADERS
597597
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-macros.h
598598
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-matcher.h
599599
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-opcode.h
600+
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-optional.h
600601
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-prelude.h
601602
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-read-concern.h
602603
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-read-prefs.h

src/libmongoc/src/mongoc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ set (src_libmongoc_src_mongoc_DIST_hs
4141
mongoc-macros.h
4242
mongoc-matcher.h
4343
mongoc-opcode.h
44+
mongoc-optional.h
4445
mongoc-prelude.h
4546
mongoc-rand.h
4647
mongoc-read-concern.h
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2021 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mongoc-optional.h"
18+
19+
void
20+
mongoc_optional_init (mongoc_optional_t *opt)
21+
{
22+
opt->is_set = false;
23+
opt->value = false;
24+
}
25+
26+
bool
27+
mongoc_optional_set (mongoc_optional_t *opt)
28+
{
29+
BSON_ASSERT (opt);
30+
return opt->is_set;
31+
}
32+
33+
bool
34+
mongoc_optional_value (mongoc_optional_t *opt)
35+
{
36+
BSON_ASSERT (opt);
37+
return opt->value;
38+
}
39+
40+
void
41+
mongoc_optional_set_value (mongoc_optional_t *opt, bool val)
42+
{
43+
BSON_ASSERT (opt);
44+
opt->value = val;
45+
opt->is_set = true;
46+
}
47+
48+
void
49+
mongoc_optional_copy (const mongoc_optional_t *source, mongoc_optional_t *copy) {
50+
copy->value = source->value;
51+
copy->is_set = source->is_set;
52+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Copyright 2021 MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mongoc-prelude.h"
18+
19+
#ifndef MONGOC_OPTIONAL_H
20+
#define MONGOC_OPTIONAL_H
21+
22+
#include <bson/bson.h>
23+
24+
#include "mongoc-macros.h"
25+
26+
BSON_BEGIN_DECLS
27+
28+
typedef struct {
29+
bool value;
30+
bool is_set;
31+
} mongoc_optional_t;
32+
33+
MONGOC_EXPORT (void)
34+
mongoc_optional_init (mongoc_optional_t *opt);
35+
36+
MONGOC_EXPORT (bool)
37+
mongoc_optional_set (mongoc_optional_t *opt);
38+
39+
MONGOC_EXPORT (bool)
40+
mongoc_optional_value (mongoc_optional_t *opt);
41+
42+
MONGOC_EXPORT (void)
43+
mongoc_optional_set_value (mongoc_optional_t *opt, bool val);
44+
45+
MONGOC_EXPORT (void)
46+
mongoc_optional_copy (const mongoc_optional_t *source, mongoc_optional_t *copy);
47+
48+
BSON_END_DECLS
49+
50+
#endif /* MONGOC_OPTIONAL_H */

src/libmongoc/src/mongoc/mongoc-server-api-private.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,8 @@
2323

2424
struct _mongoc_server_api_t {
2525
mongoc_server_api_version_t version;
26-
bool strict;
27-
bool strict_set;
28-
bool deprecation_errors;
29-
bool deprecation_errors_set;
26+
mongoc_optional_t *strict;
27+
mongoc_optional_t *deprecation_errors;
3028
};
3129

3230
#endif /* MONGOC_SERVER_API_PRIVATE_H */

src/libmongoc/src/mongoc/mongoc-server-api.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ mongoc_server_api_new (mongoc_server_api_version_t version)
4848

4949
api = (mongoc_server_api_t *) bson_malloc0 (sizeof (mongoc_server_api_t));
5050
api->version = version;
51+
api->strict = mongoc_optional_new ();
52+
api->deprecation_errors = mongoc_optional_new ();
5153

5254
return api;
5355
}
@@ -63,10 +65,8 @@ mongoc_server_api_copy (const mongoc_server_api_t *api)
6365

6466
copy = (mongoc_server_api_t *) bson_malloc0 (sizeof (mongoc_server_api_t));
6567
copy->version = api->version;
66-
copy->strict_set = api->strict_set;
67-
copy->strict = api->strict;
68-
copy->deprecation_errors_set = api->deprecation_errors_set;
69-
copy->deprecation_errors = api->deprecation_errors;
68+
copy->strict = mongoc_optional_copy (api->strict);
69+
copy->deprecation_errors = mongoc_optional_copy (api->deprecation_errors);
7070

7171
return copy;
7272
}
@@ -85,15 +85,27 @@ void
8585
mongoc_server_api_strict (mongoc_server_api_t *api, bool strict)
8686
{
8787
BSON_ASSERT (api);
88-
api->strict = strict;
89-
api->strict_set = true;
88+
mongoc_optional_set_value (api->strict, strict);
9089
}
9190

9291
void
9392
mongoc_server_api_deprecation_errors (mongoc_server_api_t *api,
9493
bool deprecation_errors)
9594
{
9695
BSON_ASSERT (api);
97-
api->deprecation_errors = deprecation_errors;
98-
api->deprecation_errors_set = true;
96+
mongoc_optional_set_value (api->deprecation_errors, deprecation_errors);
97+
}
98+
99+
const mongoc_optional_t *
100+
mongoc_server_api_get_deprecation_errors (mongoc_server_api_t *api)
101+
{
102+
BSON_ASSERT (api);
103+
return api->deprecation_errors;
104+
}
105+
106+
const mongoc_optional_t *
107+
mongo_server_api_get_strict (mongoc_server_api_t *api)
108+
{
109+
BSON_ASSERT (api);
110+
return api->strict;
99111
}

src/libmongoc/src/mongoc/mongoc-server-api.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
#include <bson/bson.h>
2323

24+
#include "mongoc-optional.h"
2425
#include "mongoc-macros.h"
2526

2627
BSON_BEGIN_DECLS
@@ -52,6 +53,12 @@ MONGOC_EXPORT (void)
5253
mongoc_server_api_deprecation_errors (mongoc_server_api_t *api,
5354
bool deprecation_errors);
5455

56+
MONGOC_EXPORT (const mongoc_optional_t *)
57+
mongoc_server_api_get_deprecation_errors (mongoc_server_api_t *api);
58+
59+
MONGOC_EXPORT (const mongoc_optional_t *)
60+
mongo_server_api_get_strict (mongoc_server_api_t *api);
61+
5562
BSON_END_DECLS
5663

5764
#endif /* MONGOC_SERVER_API_H */

0 commit comments

Comments
 (0)