1
1
#include <amongoc/amongoc.h>
2
2
3
- #include " bson/view.h"
3
+ #include < bson/format.h>
4
4
#include <bson/iterator.h>
5
5
#include <bson/mut.h>
6
-
7
- #include "mlib/str.h"
6
+ #include <bson/view.h>
8
7
9
8
/**
10
9
* @brief Shared state for the application. This is passed through the app as pointer stored
@@ -15,11 +14,6 @@ typedef struct app_state {
15
14
amongoc_client * client ;
16
15
} app_state ;
17
16
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
-
23
17
/** after_hello()
24
18
* @brief Handle the `hello` response from the server
25
19
*
@@ -32,7 +26,7 @@ amongoc_box after_hello(amongoc_box state_ptr, amongoc_status*, amongoc_box resp
32
26
bson_view resp = bson_view_from (amongoc_box_cast (bson_doc , resp_data ));
33
27
// Just print the response message
34
28
fprintf (stdout , "Got response: " );
35
- print_bson ( stdout , resp , mlib_str_view_from ( "" ) );
29
+ bson_write_repr ( stderr , resp );
36
30
fputs ("\n" , stdout );
37
31
amongoc_box_destroy (resp_data );
38
32
return amongoc_nil ;
@@ -101,6 +95,7 @@ int main(int argc, char const* const* argv) {
101
95
amongoc_client_delete (state .client );
102
96
amongoc_default_loop_destroy (& loop );
103
97
98
+ // Final status
104
99
amongoc_if_error (status , msg ) {
105
100
fprintf (stderr , "An error occurred: %s\n" , msg );
106
101
return 2 ;
@@ -110,60 +105,3 @@ int main(int argc, char const* const* argv) {
110
105
}
111
106
}
112
107
// 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
- }
0 commit comments