Mercurial > sci
changeset 43:6854efe15210
scid: push json recursively
author | David Demelier <markand@malikania.fr> |
---|---|
date | Thu, 11 Aug 2022 11:34:32 +0200 |
parents | 4076b07c7a6f |
children | 576f4b1ec79f |
files | scid/page-index.c scid/page-jobresults.c scid/page-jobresults.d scid/page-jobresults.o scid/theme.c scid/theme.h |
diffstat | 6 files changed, 87 insertions(+), 31 deletions(-) [+] |
line wrap: on
line diff
--- a/scid/page-index.c Tue Aug 09 14:54:59 2022 +0200 +++ b/scid/page-index.c Thu Aug 11 11:34:32 2022 +0200 @@ -108,17 +108,17 @@ static void get(struct kreq *req) { - json_t *projects, *root; + json_t *projects; char *data; /* First, fetch all projects. */ if ((projects = db_project_list())) { update_projects(projects); - root = json_pack("{so}", "projects", projects); - data = theme_page_index(root); + data = theme_page_index(util_json_pack("{so}", + "projects", projects + )); pageutil_render(req, KHTTP_200, KMIME_TEXT_HTML, data); free(data); - json_decref(root); } else pageutil_status(req, KHTTP_500); }
--- a/scid/page-jobresults.c Tue Aug 09 14:54:59 2022 +0200 +++ b/scid/page-jobresults.c Thu Aug 11 11:34:32 2022 +0200 @@ -28,19 +28,17 @@ static void list(struct kreq *r, intmax_t id) { - json_t *results, *doc; + json_t *results; char *data; if (!(results = db_jobresult_list_by_job(id))) pageutil_status(r, KHTTP_404); else { - doc = util_json_pack("{sI so}", - "job_id", (json_int_t)id, - "jobresults", results - ); - data = theme_page_jobresults(doc); + data = theme_page_jobresults(util_json_pack("{sI so}", + "job_id", (json_int_t)id, + "jobresults", results + )); pageutil_render(r, KHTTP_200, KMIME_TEXT_HTML, data); - json_decref(doc); free(data); } }
--- a/scid/page-jobresults.d Tue Aug 09 14:54:59 2022 +0200 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,6 +0,0 @@ -scid/page-jobresults.o: scid/page-jobresults.c scid/db.h \ - /usr/local/Cellar/jansson/2.14/include/jansson.h \ - /usr/local/Cellar/jansson/2.14/include/jansson_config.h \ - scid/page-jobresults.h scid/pageutil.h \ - /usr/local/Cellar/kcgi/0.13.0/include/kcgi.h scid/theme.h \ - libsci/util.h
--- a/scid/theme.c Tue Aug 09 14:54:59 2022 +0200 +++ b/scid/theme.c Thu Aug 11 11:34:32 2022 +0200 @@ -280,34 +280,98 @@ /* }}} */ +static inline void push_object(duk_context *, json_t *); +static inline void push_array(duk_context *, json_t *); + +static inline void +push_value(duk_context *ctx, json_t *val) +{ + switch (json_typeof(val)) { + case JSON_STRING: + duk_push_string(ctx, json_string_value(val)); + break; + case JSON_INTEGER: + duk_push_number(ctx, json_integer_value(val)); + break; + case JSON_REAL: + duk_push_number(ctx, json_real_value(val)); + break; + case JSON_TRUE: + duk_push_true(ctx); + break; + case JSON_FALSE: + duk_push_false(ctx); + break; + case JSON_NULL: + duk_push_null(ctx); + break; + case JSON_OBJECT: + push_object(ctx, val); + break; + case JSON_ARRAY: + push_array(ctx, val); + break; + } +} + +static inline void +push_object(duk_context *ctx, json_t *object) +{ + assert(json_is_object(object)); + + json_t *val; + const char *key; + + duk_push_object(ctx); + + json_object_foreach(object, key, val) { + push_value(ctx, val); + duk_put_prop_string(ctx, -2, key); + } +} + +static inline void +push_array(duk_context *ctx, json_t *array) +{ + assert(json_is_array(array)); + + json_t *val; + size_t i; + + duk_push_array(ctx); + + json_array_foreach(array, i, val) { + push_value(ctx, val); + duk_put_prop_index(ctx, -2, i); + } +} + static char * -call(const json_t *json, const char *function) +call(json_t *json, const char *function) { - char *out = NULL, *dump; + char *out = NULL; size_t outsz = 0; FILE *fp; duk_get_global_string(context, function); if (duk_is_callable(context, -1)) { - fp = util_open_memstream(&out, &outsz); - dump = util_json_dump(json); - + fp = util_open_memstream(&out, &outsz); duk_push_pointer(context, fp); - duk_push_string(context, dump); - duk_json_decode(context, -1); + push_value(context, json); if (duk_pcall(context, 2) != 0) log_warn("theme: %s", duk_safe_to_string(context, -1)); duk_pop(context); fclose(fp); - free(dump); } else duk_pop(context); if (!out) out = util_strdup(""); + if (json) + json_decref(json); return out; } @@ -352,7 +416,7 @@ } char * -theme_page_index(const json_t *json) +theme_page_index(json_t *json) { assert(json); @@ -360,7 +424,7 @@ } char * -theme_page_jobresults(const json_t *json) +theme_page_jobresults(json_t *json) { assert(json);
--- a/scid/theme.h Tue Aug 09 14:54:59 2022 +0200 +++ b/scid/theme.h Thu Aug 11 11:34:32 2022 +0200 @@ -91,12 +91,12 @@ * ``` * * \pre doc != NULL - * \param doc the page document + * \param doc the page document (borrowed) * \return a newly allocated rendered string * \note You must free the return value */ char * -theme_page_index(const json_t *doc); +theme_page_index(json_t *doc); /** * Render the jobresults page. @@ -120,12 +120,12 @@ * ``` * * \pre doc != NULL - * \param doc the page document + * \param doc the page document (borrowed) * \return a newly allocated rendered string * \note You must free the return value */ char * -theme_page_jobresults(const json_t *doc); +theme_page_jobresults(json_t *doc); /** * Render the status page (for error code).