Skip to content

Commit

Permalink
fix: update tests & examples
Browse files Browse the repository at this point in the history
  • Loading branch information
jean-roland committed Jun 12, 2024
1 parent 8c55936 commit 9d54437
Show file tree
Hide file tree
Showing 31 changed files with 158 additions and 121 deletions.
9 changes: 4 additions & 5 deletions examples/arduino/z_queryable.ino
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,14 @@ void query_handler(const z_loaned_query_t *query, void *arg) {
Serial.println("'");

// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_string_loan(payload_string)) > 1) {
Serial.print(" with value '");
Serial.print(z_string_data(z_string_loan(&payload_string)));
Serial.println("'");
z_string_drop(z_string_move(&payload_string));
}
z_string_drop(z_string_move(&payload_string));

z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);
Expand Down
7 changes: 4 additions & 3 deletions examples/arduino/z_sub.ino
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,17 @@
void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
std::string val((const char *)z_bytes_data(payload), z_bytes_len(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);

Serial.print(" >> [Subscription listener] Received (");
Serial.print(z_string_data(z_string_loan(&keystr)));
Serial.print(", ");
Serial.print(val.c_str());
Serial.print(z_string_data(z_string_loan(&value)));
Serial.println(")");

z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

void setup() {
Expand Down
11 changes: 6 additions & 5 deletions examples/espidf/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -110,15 +110,16 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}
z_drop(z_move(payload_string));

z_view_keyexpr_t ke;
z_view_keyexpr_from_string_unchecked(&ke, KEYEXPR);

// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_encode_from_string(&reply_payload, VALUE);
Expand Down
8 changes: 5 additions & 3 deletions examples/espidf/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,12 @@ void wifi_init_sta(void) {
void data_handler(const z_loaned_sample_t* sample, void* arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t* payload = z_sample_payload(sample);
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)),
(int)z_bytes_len(payload), z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

void app_main() {
Expand Down
10 changes: 5 additions & 5 deletions examples/freertos_plus_tcp/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}
z_drop(z_move(payload_string));

z_query_reply_options_t options;
z_query_reply_options_default(&options);
// Reply value encoding
Expand Down
7 changes: 4 additions & 3 deletions examples/freertos_plus_tcp/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
}

void app_main(void) {
Expand Down
8 changes: 4 additions & 4 deletions examples/freertos_plus_tcp/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_drop(z_move(keystr));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
msg_nb++;
}

Expand Down
10 changes: 5 additions & 5 deletions examples/mbed/z_queryable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)),
(int)z_view_string_loan(&pred)->len, z_view_string_loan(&pred)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_string_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string)));
z_string_drop(z_string_move(&payload_string));
}
z_string_drop(z_string_move(&payload_string));

// Reply value encoding
z_owned_bytes_t reply_payload;
z_bytes_encode_from_string(&reply_payload, VALUE);
Expand Down
8 changes: 5 additions & 3 deletions examples/mbed/z_sub.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@
void data_handler(const z_loaned_sample_t *sample, void *arg) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(" >> [Subscriber handler] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)),
(int)z_bytes_len(payload), z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(" >> [Subscriber handler] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

int main(int argc, char **argv) {
Expand Down
6 changes: 4 additions & 2 deletions examples/unix/c11/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1
void callback(const z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context);
const z_loaned_bytes_t* payload = z_sample_payload(sample);
z_publisher_put(pub, z_bytes_data(payload), z_bytes_len(payload), NULL);
z_owned_slice_t value;
z_bytes_decode_into_slice(z_sample_payload(sample), &value);
z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL);
z_drop(z_move(value));
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
Expand Down
8 changes: 5 additions & 3 deletions examples/unix/c11/z_pull.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,10 +90,12 @@ int main(int argc, char **argv) {
for (z_call(channel.try_recv, &sample); z_check(sample); z_call(channel.try_recv, &sample)) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(z_loan(sample));
printf(">> [Subscriber] Pulled ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(z_loan(sample)), &value);
printf(">> [Subscriber] Pulled ('%s': '%s')\n", z_string_data(z_loan(keystr)),
z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
z_drop(z_move(sample));
}
printf(">> [Subscriber] Nothing to pull... sleep for %zu ms\n", interval);
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c11/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}
z_drop(z_move(payload_string));

#if Z_FEATURE_ATTACHMENT == 1
z_attachment_t attachment = z_query_attachment(query);
if (z_attachment_check(&attachment)) {
Expand Down
11 changes: 6 additions & 5 deletions examples/unix/c11/z_queryable_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,14 @@ int main(int argc, char **argv) {
z_query_parameters(q, &params);
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)),
(int)z_loan(params)->len, z_loan(params)->val);
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(z_loan(query)));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
// Process value
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(z_loan(query))), &payload_string);
if (z_string_len(z_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}
z_drop(z_move(payload_string));

z_query_reply_options_t options;
z_query_reply_options_default(&options);
// Reply value encoding
Expand Down
7 changes: 4 additions & 3 deletions examples/unix/c11/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
#if Z_FEATURE_ATTACHMENT == 1
z_attachment_t attachment = z_sample_attachment(sample);
if (z_attachment_check(&attachment)) {
Expand All @@ -47,6 +47,7 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
}
#endif
z_drop(z_move(keystr));
z_drop(z_move(value));
msg_nb++;
}

Expand Down
7 changes: 4 additions & 3 deletions examples/unix/c11/z_sub_channel.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,11 @@ int main(int argc, char **argv) {
for (z_call(channel.recv, &sample); z_check(sample); z_call(channel.recv, &sample)) {
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(z_loan(sample)), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(z_loan(sample));
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(z_loan(sample)), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
z_drop(z_move(sample));
z_null(&sample);
}
Expand Down
7 changes: 4 additions & 3 deletions examples/unix/c11/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
msg_nb++;
}

Expand Down
6 changes: 4 additions & 2 deletions examples/unix/c99/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@
#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1
void callback(const z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_publisher_loan((z_owned_publisher_t*)context);
const z_loaned_bytes_t* payload = z_sample_payload(sample);
z_publisher_put(pub, z_bytes_data(payload), z_bytes_len(payload), NULL);
z_owned_slice_t value;
z_bytes_decode_into_slice(z_sample_payload(sample), &value);
z_publisher_put(pub, z_slice_data(z_string_loan(value)), z_slice_len(z_string_loan(value)), NULL);
z_slice_drop(z_slice_move(&value));
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
Expand Down
10 changes: 5 additions & 5 deletions examples/unix/c99/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_string_loan(&keystr)),
(int)z_view_string_loan(&params)->len, z_view_string_loan(&params)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_string_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_string_loan(&payload_string)));
z_string_drop(z_string_move(&payload_string));
}
z_string_drop(z_string_move(&payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);
Expand Down
8 changes: 5 additions & 3 deletions examples/unix/c99/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
(void)(arg);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)),
(int)z_bytes_len(payload), z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
}

int main(int argc, char **argv) {
Expand Down
8 changes: 5 additions & 3 deletions examples/unix/c99/z_sub_st.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ void data_handler(const z_loaned_sample_t *sample, void *arg) {
(void)(arg);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_string_loan(&keystr)),
(int)z_bytes_len(payload), z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_string_loan(&keystr)),
z_string_data(z_string_loan(&value)));
z_string_drop(z_string_move(&keystr));
z_string_drop(z_string_move(&value));
msg_nb++;
}

Expand Down
6 changes: 4 additions & 2 deletions examples/windows/z_pong.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,10 @@
#if Z_FEATURE_SUBSCRIPTION == 1 && Z_FEATURE_PUBLICATION == 1
void callback(const z_loaned_sample_t* sample, void* context) {
const z_loaned_publisher_t* pub = z_loan(*(z_owned_publisher_t*)context);
const z_loaned_bytes_t* payload = z_sample_payload(sample);
z_publisher_put(pub, z_bytes_data(payload), z_bytes_len(payload), NULL);
z_owned_slice_t value;
z_bytes_decode_into_slice(z_sample_payload(sample), &value);
z_publisher_put(pub, z_slice_data(z_loan(value)), z_slice_len(z_loan(value)), NULL);
z_drop(z_move(value));
}
void drop(void* context) {
z_owned_publisher_t* pub = (z_owned_publisher_t*)context;
Expand Down
10 changes: 5 additions & 5 deletions examples/windows/z_queryable.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ void query_handler(const z_loaned_query_t *query, void *ctx) {
printf(" >> [Queryable handler] Received Query '%s%.*s'\n", z_string_data(z_loan(keystr)), (int)z_loan(params)->len,
z_loan(params)->val);
// Process value
const z_loaned_bytes_t *payload = z_value_payload(z_query_value(query));
if (z_bytes_len(payload) > 0) {
z_owned_string_t payload_string;
z_bytes_decode_into_string(payload, &payload_string);
z_owned_string_t payload_string;
z_bytes_decode_into_string(z_value_payload(z_query_value(query)), &payload_string);
if (z_string_len(z_loan(payload_string)) > 1) {
printf(" with value '%s'\n", z_string_data(z_loan(payload_string)));
z_drop(z_move(payload_string));
}
z_drop(z_move(payload_string));

// Create encoding
z_owned_encoding_t encoding;
zp_encoding_make(&encoding, Z_ENCODING_ID_TEXT_PLAIN, NULL);
Expand Down
7 changes: 4 additions & 3 deletions examples/windows/z_sub.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,11 @@ void data_handler(const z_loaned_sample_t *sample, void *ctx) {
(void)(ctx);
z_owned_string_t keystr;
z_keyexpr_to_string(z_sample_keyexpr(sample), &keystr);
const z_loaned_bytes_t *payload = z_sample_payload(sample);
printf(">> [Subscriber] Received ('%s': '%.*s')\n", z_string_data(z_loan(keystr)), (int)z_bytes_len(payload),
z_bytes_data(payload));
z_owned_string_t value;
z_bytes_decode_into_string(z_sample_payload(sample), &value);
printf(">> [Subscriber] Received ('%s': '%s')\n", z_string_data(z_loan(keystr)), z_string_data(z_loan(value)));
z_drop(z_move(keystr));
z_drop(z_move(value));
}

int main(int argc, char **argv) {
Expand Down
Loading

0 comments on commit 9d54437

Please sign in to comment.