Skip to content

Commit

Permalink
lib-dict: Add support for lookup to return multiple values
Browse files Browse the repository at this point in the history
Implements only the initial stubs to the drivers.
  • Loading branch information
sirainen committed Jan 15, 2017
1 parent 25d2704 commit b130584
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/lib-dict/dict-client.c
Expand Up @@ -927,6 +927,7 @@ client_dict_lookup_async_callback(struct client_dict_cmd *cmd,
{
struct client_dict *dict = cmd->dict;
struct dict_lookup_result result;
const char *const values[] = { value, NULL };

i_zero(&result);
if (error != NULL) {
Expand All @@ -935,6 +936,7 @@ client_dict_lookup_async_callback(struct client_dict_cmd *cmd,
} else switch (reply) {
case DICT_PROTOCOL_REPLY_OK:
result.value = value;
result.values = values;
result.ret = 1;
break;
case DICT_PROTOCOL_REPLY_NOTFOUND:
Expand Down
4 changes: 4 additions & 0 deletions src/lib-dict/dict-sql.c
Expand Up @@ -479,6 +479,7 @@ sql_dict_lookup_async_callback(struct sql_result *sql_result,
struct sql_dict_lookup_context *ctx)
{
struct dict_lookup_result result;
const char *values[2] = { NULL, NULL };

i_zero(&result);
result.ret = sql_result_next_row(sql_result);
Expand All @@ -492,6 +493,9 @@ sql_dict_lookup_async_callback(struct sql_result *sql_result,
"not found", which is probably what is usually
wanted. */
result.ret = 0;
} else {
values[0] = result.value;
result.values = values;
}
}
ctx->callback(&result, ctx->context);
Expand Down
2 changes: 2 additions & 0 deletions src/lib-dict/dict.c
Expand Up @@ -138,6 +138,8 @@ void dict_lookup_async(struct dict *dict, const char *key,
key, &result.value);
if (result.ret < 0)
result.error = "Lookup failed";
const char *const values[] = { result.value, NULL };
result.values = values;
callback(&result, context);
return;
}
Expand Down
6 changes: 6 additions & 0 deletions src/lib-dict/dict.h
Expand Up @@ -40,7 +40,13 @@ struct dict_settings {

struct dict_lookup_result {
int ret;

/* First returned value (ret > 0) */
const char *value;
/* NULL-terminated list of all returned values (ret > 0) */
const char *const *values;

/* Error message for a failed lookup (ret < 0) */
const char *error;
};

Expand Down
7 changes: 6 additions & 1 deletion src/plugins/dict-ldap/dict-ldap.c
Expand Up @@ -297,10 +297,15 @@ ldap_dict_lookup_callback(struct ldap_result *result, struct dict_ldap_op *op)
/* try extract value */
const char *const *values = ldap_entry_get_attribute(entry, op->map->value_attribute);
if (values != NULL) {
const char **new_values;

if (op->dict->set->debug > 0)
i_debug("ldap_dict_lookup_callback got attribute %s", op->map->value_attribute);
op->res.ret = 1;
op->res.value = p_strdup(op->pool, values[0]);
new_values = p_new(op->pool, const char *, 2);
new_values[0] = p_strdup(op->pool, values[0]);
op->res.values = new_values;
op->res.value = op->res.values[0];
} else {
if (op->dict->set->debug > 0)
i_debug("ldap_dict_lookup_callback dit not get attribute %s", op->map->value_attribute);
Expand Down

0 comments on commit b130584

Please sign in to comment.