Debian Codegraph @

build/opts_templates/mongoc-opts.c.template

raw
1 #include <mongoc/mongoc-opts-helpers-private.h>
2 #include <mongoc/mongoc-opts-private.h>
3 #include <mongoc/mongoc-error-private.h>
4 #include <mongoc/mongoc-util-private.h>
5 #include <mongoc/mongoc-client-private.h>
6
7 {{ header_comment }}
8 {% for struct_type, description in opts_structs.items() %}
9 {% if description.is_shared or not description.generate_code %}{% continue %}{% endif %}
10 {% set struct_name = struct_type.split('_t', -1)[0] %}
11
12
13 bool
14 _{{ struct_name }}_parse (
15 mongoc_client_t *client,
16 const bson_t *opts,
17 {{ struct_type }} *{{ struct_name }},
18 bson_error_t *error)
19 {
20 bson_iter_t iter;
21
22 BSON_ASSERT (client || true); // client may be NULL.
23
24 {% for path, opt_name, info in paths(description) %}
25 {% if info['type'] == 'bool' %}
26 {{ struct_name }}->{{ path }} = {{ description.default(opt_name, 'false') }};
27 {% elif info['type'].startswith('int') or info['type'].startswith('uint') %}
28 {{ struct_name }}->{{ path }} = {{ description.default(opt_name, 0) }};
29 {% elif info['type'] == 'bson_validate_flags_t' %}
30 {{ struct_name }}->{{ path }} = {{ description.default(opt_name, 'BSON_VALIDATE_NONE') }};
31 {% elif info['type'] == 'mongoc_write_bypass_document_validation_t' %}
32 {{ struct_name }}->{{ path }} =
33 {{ description.default(opt_name, 'MONGOC_BYPASS_DOCUMENT_VALIDATION_DEFAULT') }};
34 {% elif info['type'] == 'bson_value_t' %}
35 memset (&{{ struct_name }}->{{ path }}, 0, sizeof (bson_value_t));
36 {% elif info['type'] in ('document', 'array') %}
37 bson_init (&{{ struct_name }}->{{ path }});
38 {% elif info['type'] == 'utf8' %}
39 {% set dflt = description.default(opt_name, "") -%}
40 {{ struct_name }}->{{ path }} = {{ "NULL" if dflt is none else (dflt|tojson) }};
41 {% elif info['type'] == 'timestamp' %}
42 memset (&{{ struct_name }}->{{ path }}, 0, sizeof (mongoc_timestamp_t));
43 {% else %}
44 {{ struct_name }}->{{ path }} = {{ description.default(opt_name, 'NULL') }};
45 {% endif %}
46 {% if info['check_set'] %}
47 {{ struct_name }}->{{ opt_name }}_is_set = false;
48 {% endif %}
49 {% endfor %}
50 bson_init (&{{ struct_name }}->extra);
51
52 if (!opts) {
53 return true;
54 }
55
56 if (!bson_iter_init (&iter, opts)) {
57 _mongoc_set_error (error,
58 MONGOC_ERROR_BSON,
59 MONGOC_ERROR_BSON_INVALID,
60 "Invalid 'opts' parameter.");
61 return false;
62 }
63
64 while (bson_iter_next (&iter)) {
65 {% for path, opt_name, info in paths(description) %}
66 {% if info.get('internal') %}
67 {% continue %}
68 {% endif %}
69 {% if info['type'] == 'utf8' %}
70 {% set the_type = 'const char *' %}
71 {% elif info['type'] in ('document', 'array') %}
72 {% set the_type = 'bson_t' %}
73 {% else %}
74 {% set the_type = info['type'] %}
75 {% endif %}
76 {% set the_name = info.get('field', opt_name) %}
77 {% set the_converter = info.get('convert', '_mongoc_convert_' + info['type'] ) %}
78 {% if loop.index > 1 %}else {% endif %}if (!strcmp (bson_iter_key (&iter), "{{ opt_name }}")) {
79 if (!{{ the_converter }} (
80 client,
81 &iter,
82 &{{ struct_name }}->{{ path }},
83 error)) {
84 return false;
85 }
86 {% if the_converter == '_mongoc_convert_write_concern' %}
87
88 {{ struct_name }}->{{ path_to(struct_type, "write_concern_owned") }} = true;
89 {% endif %}
90 {% if info['check_set'] %}
91
92 {{ struct_name }}->{{ the_name }}_is_set = true;
93 {% endif %}
94 }
95 {% endfor %}
96 else {
97 {% if description.allow_extra %}
98 /* unrecognized values are copied to "extra" */
99 if (!BSON_APPEND_VALUE (
100 &{{ struct_name }}->extra,
101 bson_iter_key (&iter),
102 bson_iter_value (&iter))) {
103 _mongoc_set_error (error,
104 MONGOC_ERROR_BSON,
105 MONGOC_ERROR_BSON_INVALID,
106 "Invalid 'opts' parameter.");
107 return false;
108 }
109 {% else %}
110 _mongoc_set_error (error,
111 MONGOC_ERROR_COMMAND,
112 MONGOC_ERROR_COMMAND_INVALID_ARG,
113 "Invalid option '%s'",
114 bson_iter_key (&iter));
115 return false;
116 {% endif %}
117 }
118 }
119
120 return true;
121 }
122
123 void
124 _{{ struct_name }}_cleanup ({{ struct_type }} *{{ struct_name }})
125 {
126 {% for path, opt_name, info in paths(description) %}
127 {% if info['type'] == 'mongoc_write_concern_t *' %}
128 if ({{ struct_name }}->{{ path_to(struct_type, "write_concern_owned") }}) {
129 mongoc_write_concern_destroy ({{ struct_name }}->{{ path }});
130 }
131 {% elif info['type'] == 'bson_value_t' %}
132 bson_value_destroy (&{{ struct_name }}->{{ path }});
133 {% elif info['type'] in ('document', 'array') %}
134 bson_destroy (&{{ struct_name }}->{{ path }});
135 {% elif info['type'] == 'mongoc_read_concern_t *' %}
136 mongoc_read_concern_destroy ({{ struct_name }}->{{ path }});
137 {% endif %}
138 {% endfor %}
139 bson_destroy (&{{struct_name }}->extra);
140 }{% endfor %}