Skip to content

Commit

Permalink
Config: Add a new more user friendly config lookup function (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
Alasdair authored Feb 27, 2025
1 parent b2687c3 commit 895225c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
28 changes: 28 additions & 0 deletions lib/json/sail_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,34 @@ sail_config_json sail_config_get(size_t n, const char *key[])
return (sail_config_json)json;
}

sail_config_json sail_config_lookup(const char *dotted_key)
{
sail_config_json result;
cJSON *json = (cJSON *)sail_config;

size_t start = 0;
size_t len = strlen(dotted_key);

for (size_t i = 0; i <= len; i++) {
if (dotted_key[i] == '.' || dotted_key[i] == '\0') {
char *key = (char *)sail_malloc((i - start) + 1);
strncpy(key, dotted_key + start, i - start);
key[i - start] = '\0';
fprintf(stderr, "'%s'\n", key);
start = i + 1;

if (cJSON_IsObject(json)) {
json = cJSON_GetObjectItemCaseSensitive(json, key);
} else {
json = NULL;
break;
}
}
}

return (sail_config_json)json;
}

int64_t sail_config_list_length(const sail_config_json config)
{
cJSON *json = (cJSON *)config;
Expand Down
7 changes: 7 additions & 0 deletions lib/json/sail_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,13 @@ void sail_config_cleanup(void);
*/
sail_config_json sail_config_get(const size_t n, const_sail_string key[]);

/*
* Get the JSON corresponding to some key. Rather than an array the
* key is a string with '.' characters separating the parts. Returns
* NULL if the key cannot be found.
*/
sail_config_json sail_config_lookup(const char *dotted_key);

/*
* For each Sail type, Sail will generate code that will destructure
* the JSON values using the following function calls.
Expand Down

0 comments on commit 895225c

Please sign in to comment.