Skip to content

Commit 9f812e1

Browse files
Use our BSON printer in the examples
1 parent d1d1a32 commit 9f812e1

File tree

2 files changed

+5
-67
lines changed

2 files changed

+5
-67
lines changed

docs/how-to/communicate.example.c

Lines changed: 4 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
#include <amongoc/amongoc.h>
22

3-
#include "bson/view.h"
3+
#include <bson/format.h>
44
#include <bson/iterator.h>
55
#include <bson/mut.h>
6-
7-
#include "mlib/str.h"
6+
#include <bson/view.h>
87

98
/**
109
* @brief Shared state for the application. This is passed through the app as pointer stored
@@ -15,11 +14,6 @@ typedef struct app_state {
1514
amongoc_client* client;
1615
} app_state;
1716

18-
/**
19-
* @brief Write the content of a BSON document in JSON-like format to the given output
20-
*/
21-
static void print_bson(FILE* into, bson_view doc, mlib_str_view indent);
22-
2317
/** after_hello()
2418
* @brief Handle the `hello` response from the server
2519
*
@@ -32,7 +26,7 @@ amongoc_box after_hello(amongoc_box state_ptr, amongoc_status*, amongoc_box resp
3226
bson_view resp = bson_view_from(amongoc_box_cast(bson_doc, resp_data));
3327
// Just print the response message
3428
fprintf(stdout, "Got response: ");
35-
print_bson(stdout, resp, mlib_str_view_from(""));
29+
bson_write_repr(stderr, resp);
3630
fputs("\n", stdout);
3731
amongoc_box_destroy(resp_data);
3832
return amongoc_nil;
@@ -101,6 +95,7 @@ int main(int argc, char const* const* argv) {
10195
amongoc_client_delete(state.client);
10296
amongoc_default_loop_destroy(&loop);
10397

98+
// Final status
10499
amongoc_if_error (status, msg) {
105100
fprintf(stderr, "An error occurred: %s\n", msg);
106101
return 2;
@@ -110,60 +105,3 @@ int main(int argc, char const* const* argv) {
110105
}
111106
}
112107
// end.
113-
114-
static void print_bson(FILE* into, bson_view doc, mlib_str_view indent) {
115-
fprintf(into, "{\n");
116-
bson_foreach(it, doc) {
117-
mlib_str_view str = bson_key(it);
118-
fprintf(into, "%*s \"%s\": ", (int)indent.len, indent.data, str.data);
119-
bson_value_ref val = bson_iterator_value(it);
120-
switch (val.type) {
121-
case bson_type_eod:
122-
case bson_type_double:
123-
fprintf(into, "%f,\n", val.double_);
124-
break;
125-
case bson_type_utf8:
126-
fprintf(into, "\"%s\",\n", val.utf8.data);
127-
break;
128-
case bson_type_document:
129-
case bson_type_array: {
130-
mlib_str i2 = mlib_str_append(indent, " ");
131-
bson_view subdoc = bson_iterator_value(it).document;
132-
print_bson(into, subdoc, mlib_str_view_from(i2));
133-
mlib_str_delete(i2);
134-
fprintf(into, ",\n");
135-
break;
136-
}
137-
case bson_type_undefined:
138-
fprintf(into, "[undefined],\n");
139-
break;
140-
case bson_type_bool:
141-
fprintf(into, val.bool_ ? "true,\n" : "false,\n");
142-
break;
143-
case bson_type_null:
144-
fprintf(into, "null,\n");
145-
break;
146-
case bson_type_int32:
147-
fprintf(into, "%d,\n", val.int32);
148-
break;
149-
case bson_type_int64:
150-
fprintf(into, "%ld,\n", val.int64);
151-
break;
152-
case bson_type_timestamp:
153-
case bson_type_decimal128:
154-
case bson_type_maxkey:
155-
case bson_type_minkey:
156-
case bson_type_oid:
157-
case bson_type_binary:
158-
case bson_type_datetime:
159-
case bson_type_regex:
160-
case bson_type_dbpointer:
161-
case bson_type_code:
162-
case bson_type_symbol:
163-
case bson_type_codewscope:
164-
fprintf(into, "[[printing unimplemented for this type]],\n");
165-
break;
166-
}
167-
}
168-
fprintf(into, "%*s}", (int)indent.len, indent.data);
169-
}

docs/how-to/communicate.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ Print the Final Result
240240

241241
.. literalinclude:: communicate.example.c
242242
:lineno-match:
243-
:start-at: if_error
243+
:start-at: // Final status
244244
:end-before: end.
245245

246246
Finally, we inspect the `amongoc_status` that was produced by our operation and

0 commit comments

Comments
 (0)