Permalink
Browse files

Simplified multi-item responses.

  • Loading branch information...
1 parent edc6edf commit ba9b85c0eb40c6a1d78586e7a4c18ed41926f9ee @dustin dustin committed Jun 14, 2009
Showing with 22 additions and 30 deletions.
  1. +5 −5 conflate.h
  2. +5 −13 examples/bot.c
  3. +12 −12 xmpp.c
View
@@ -176,15 +176,15 @@ void conflate_add_field_multi(conflate_form_result *r, const char *k,
__attribute__ ((nonnull (1, 2, 3)));
/**
- * Add a set of fields to a multi-valued result.
+ * Create a response container for a multi-set response.
*
- * Do not attempt to mix this with conflate_add_field -- bad things will happen.
+ * This is used for callbacks that want to build tabular results
+ * (i.e. a list of k/v pairs).
*
* @param r the form as handed to the callback
- * @param pair the kvpair to add to the results
*/
-void conflate_add_fieldset(conflate_form_result *r, const kvpair_t *pair)
- __attribute__ ((nonnull (1, 2)));
+void conflate_next_fieldset(conflate_form_result *r)
+ __attribute__ ((nonnull (1)));
/**
* Initialize the result form if it's not already initialized.
View
@@ -43,21 +43,13 @@ static void do_ping_test(void* userdata, conflate_form_result *r,
char **servers = servers_p->values;
for (int i = 0; servers[i]; i++) {
- printf("\t%s\n", servers[i]);
- kvpair_t *data = mk_kvpair("test1", NULL);
- add_kvpair_value(data, "val1");
- add_kvpair_value(data, "val2");
+ conflate_next_fieldset(r);
+ conflate_add_field(r, "-set-", servers[i]);
- kvpair_t *data2 = mk_kvpair("test2", NULL);
- add_kvpair_value(data2, "some val");
- data->next = data2;
+ const char *vals[3] = { "val1", "val2", NULL };
+ conflate_add_field_multi(r, "test1", vals);
- kvpair_t *set_name = mk_kvpair("-set-", NULL);
- add_kvpair_value(set_name, servers[i]);
- data2->next = set_name;
-
- conflate_add_fieldset(r, data);
- free_kvpair(data);
+ conflate_add_field(r, "test2", "some val");
}
}
View
24 xmpp.c
@@ -21,6 +21,7 @@ struct _conflate_form_result {
xmpp_stanza_t *reply;
xmpp_stanza_t *cmd_res;
xmpp_stanza_t *container;
+ xmpp_stanza_t *current;
};
struct command_def {
@@ -363,8 +364,11 @@ void conflate_add_field_multi(conflate_form_result *r, const char *k,
const char **v)
{
conflate_init_form(r);
+ if (!r->current) {
+ r->current = r->container;
+ }
if (k) {
- add_form_values(r->ctx, r->container, k, v);
+ add_form_values(r->ctx, r->current, k, v);
}
}
@@ -417,21 +421,17 @@ static enum conflate_mgmt_cb_result process_reset_stats(void *opaque,
return RV_OK;
}
-void conflate_add_fieldset(conflate_form_result *r, const kvpair_t *pair)
+void conflate_next_fieldset(conflate_form_result *r)
{
- if (pair) {
conflate_init_form(r);
- xmpp_stanza_t *item = xmpp_stanza_new(r->ctx);
- assert(item);
+ /* Fail if some k/v pairs were already added */
+ assert(r->current == NULL || r->current != r->container);
- xmpp_stanza_set_name(item, "item");
- add_and_release(r->container, item);
+ r->current = xmpp_stanza_new(r->ctx);
+ assert(r->current);
- for (; pair; pair = pair->next) {
- add_form_values(r->ctx, item, pair->key,
- (const char**)pair->values);
- }
- }
+ xmpp_stanza_set_name(r->current, "item");
+ add_and_release(r->container, r->current);
}
static enum conflate_mgmt_cb_result process_ping_test(void *opaque,

0 comments on commit ba9b85c

Please sign in to comment.