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 (stdout , resp );
3630 fputs ("\n" , stdout );
3731 amongoc_box_destroy (resp_data );
3832 return amongoc_nil ;
@@ -72,8 +66,12 @@ int main(int argc, char const* const* argv) {
7266 }
7367 const char * const uri = argv [1 ];
7468
75- amongoc_loop loop ;
76- amongoc_default_loop_init (& loop );
69+ amongoc_loop loop ;
70+ amongoc_status status = amongoc_default_loop_init (& loop );
71+ amongoc_if_error (status , msg ) {
72+ fprintf (stderr , "Error setting up the event loop: %s\n" , msg );
73+ return 2 ;
74+ }
7775
7876 struct app_state state = {0 };
7977
@@ -88,8 +86,7 @@ int main(int argc, char const* const* argv) {
8886 amongoc_box_pointer (& state ),
8987 after_connect_say_hello );
9088
91- amongoc_status fin_status = amongoc_okay ;
92- amongoc_operation op = amongoc_tie (em , & fin_status , NULL , mlib_default_allocator );
89+ amongoc_operation op = amongoc_tie (em , & status );
9390 amongoc_start (& op );
9491 amongoc_default_loop_run (& loop );
9592 amongoc_operation_delete (op );
@@ -98,71 +95,13 @@ int main(int argc, char const* const* argv) {
9895 amongoc_client_delete (state .client );
9996 amongoc_default_loop_destroy (& loop );
10097
101- if (amongoc_is_error (fin_status )) {
102- char * m = amongoc_status_strdup_message (fin_status );
103- fprintf (stderr , "An error occurred: %s\n" , m );
104- free (m );
98+ // Final status
99+ amongoc_if_error (status , msg ) {
100+ fprintf (stderr , "An error occurred: %s\n" , msg );
105101 return 2 ;
106102 } else {
107103 printf ("Okay\n" );
108104 return 0 ;
109105 }
110106}
111107// end.
112-
113- static void print_bson (FILE * into , bson_view doc , mlib_str_view indent ) {
114- fprintf (into , "{\n" );
115- bson_foreach (it , doc ) {
116- mlib_str_view str = bson_key (it );
117- fprintf (into , "%*s \"%s\": " , (int )indent .len , indent .data , str .data );
118- bson_value_ref val = bson_iterator_value (it );
119- switch (val .type ) {
120- case bson_type_eod :
121- case bson_type_double :
122- fprintf (into , "%f,\n" , val .double_ );
123- break ;
124- case bson_type_utf8 :
125- fprintf (into , "\"%s\",\n" , val .utf8 .data );
126- break ;
127- case bson_type_document :
128- case bson_type_array : {
129- mlib_str i2 = mlib_str_append (indent , " " );
130- bson_view subdoc = bson_iterator_value (it ).document ;
131- print_bson (into , subdoc , mlib_str_view_from (i2 ));
132- mlib_str_delete (i2 );
133- fprintf (into , ",\n" );
134- break ;
135- }
136- case bson_type_undefined :
137- fprintf (into , "[undefined],\n" );
138- break ;
139- case bson_type_bool :
140- fprintf (into , val .bool_ ? "true,\n" : "false,\n" );
141- break ;
142- case bson_type_null :
143- fprintf (into , "null,\n" );
144- break ;
145- case bson_type_int32 :
146- fprintf (into , "%d,\n" , val .int32 );
147- break ;
148- case bson_type_int64 :
149- fprintf (into , "%ld,\n" , val .int64 );
150- break ;
151- case bson_type_timestamp :
152- case bson_type_decimal128 :
153- case bson_type_maxkey :
154- case bson_type_minkey :
155- case bson_type_oid :
156- case bson_type_binary :
157- case bson_type_datetime :
158- case bson_type_regex :
159- case bson_type_dbpointer :
160- case bson_type_code :
161- case bson_type_symbol :
162- case bson_type_codewscope :
163- fprintf (into , "[[printing unimplemented for this type]],\n" );
164- break ;
165- }
166- }
167- fprintf (into , "%*s}" , (int )indent .len , indent .data );
168- }
0 commit comments