diff options
author | Valentin Bartenev <vbart@nginx.com> | 2017-06-26 21:41:58 +0300 |
---|---|---|
committer | Valentin Bartenev <vbart@nginx.com> | 2017-06-26 21:41:58 +0300 |
commit | f86c80309885230df2960ef8e1db337f04b11926 (patch) | |
tree | 9a66939a017d8e11e5119b0df3d5f27812a10ff9 /src | |
parent | 01517e37c1d2b09712a666b7b4870fc932e526a9 (diff) | |
download | unit-f86c80309885230df2960ef8e1db337f04b11926.tar.gz unit-f86c80309885230df2960ef8e1db337f04b11926.tar.bz2 |
Interface for mapping JSON configuration objects to C structures.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_clang.h | 4 | ||||
-rw-r--r-- | src/nxt_conf.h | 27 | ||||
-rw-r--r-- | src/nxt_conf_json.c | 120 |
3 files changed, 148 insertions, 3 deletions
diff --git a/src/nxt_clang.h b/src/nxt_clang.h index 09ccaf4c..b69dd6af 100644 --- a/src/nxt_clang.h +++ b/src/nxt_clang.h @@ -173,6 +173,10 @@ nxt_container_of(p, type, field) \ (type *) ((u_char *) (p) - offsetof(type, field)) +#define nxt_pointer_to(p, offset) \ + ((void *) ((char *) (p) + (offset))) + + #define nxt_value_at(type, p, offset) \ *(type *) ((u_char *) p + offset) diff --git a/src/nxt_conf.h b/src/nxt_conf.h index 91002741..4fe88404 100644 --- a/src/nxt_conf.h +++ b/src/nxt_conf.h @@ -13,9 +13,29 @@ typedef struct nxt_conf_json_value_s nxt_conf_json_value_t; typedef struct nxt_conf_json_op_s nxt_conf_json_op_t; +typedef enum { + NXT_CONF_JSON_MAP_INT8, + NXT_CONF_JSON_MAP_INT32, + NXT_CONF_JSON_MAP_INT64, + NXT_CONF_JSON_MAP_INT, + NXT_CONF_JSON_MAP_SIZE, + NXT_CONF_JSON_MAP_OFF, + NXT_CONF_JSON_MAP_DOUBLE, + NXT_CONF_JSON_MAP_STR, + NXT_CONF_JSON_MAP_PTR, +} nxt_conf_json_map_type_t; + + +typedef struct { + nxt_str_t name; + nxt_conf_json_map_type_t type; + size_t offset; +} nxt_conf_json_object_map_t; + + typedef struct { - uint32_t level; - uint8_t more_space; /* 1 bit. */ + uint32_t level; + uint8_t more_space; /* 1 bit. */ } nxt_conf_json_pretty_t; @@ -24,6 +44,9 @@ nxt_conf_json_value_t *nxt_conf_json_get_value(nxt_conf_json_value_t *value, nxt_conf_json_value_t *nxt_conf_json_object_get_member( nxt_conf_json_value_t *value, nxt_str_t *name, uint32_t *index); +nxt_int_t nxt_conf_json_object_map(nxt_conf_json_value_t *value, + nxt_conf_json_object_map_t *map, void *data); + nxt_int_t nxt_conf_json_op_compile(nxt_mp_t *mp, nxt_conf_json_op_t **ops, nxt_conf_json_value_t *root, nxt_str_t *path, nxt_conf_json_value_t *value); diff --git a/src/nxt_conf_json.c b/src/nxt_conf_json.c index b6deade3..0f4b3271 100644 --- a/src/nxt_conf_json.c +++ b/src/nxt_conf_json.c @@ -44,7 +44,7 @@ struct nxt_conf_json_value_s { union { uint32_t boolean; /* 1 bit. */ int64_t integer; - /* double number; */ + double number; u_char str[1 + NXT_CONF_JSON_STR_SIZE]; nxt_str_t *string; nxt_conf_json_array_t *array; @@ -253,6 +253,124 @@ nxt_conf_json_object_get_member(nxt_conf_json_value_t *value, nxt_str_t *name, nxt_int_t +nxt_conf_json_object_map(nxt_conf_json_value_t *value, + nxt_conf_json_object_map_t *map, void *data) +{ + nxt_uint_t i; + nxt_conf_json_value_t *v; + + union { + uint8_t ui8; + int32_t i32; + int64_t i64; + nxt_int_t i; + ssize_t size; + off_t off; + double dbl; + nxt_str_t str; + void *v; + } *ptr; + + for (i = 0; map[i].name.length != 0; i++) { + + v = nxt_conf_json_object_get_member(value, &map[i].name, NULL); + + if (v == NULL || v->type == NXT_CONF_JSON_NULL) { + continue; + } + + ptr = nxt_pointer_to(data, map[i].offset); + + switch (map[i].type) { + + case NXT_CONF_JSON_MAP_INT8: + + if (v->type != NXT_CONF_JSON_BOOLEAN) { + return NXT_ERROR; + } + + ptr->ui8 = v->u.boolean; + + break; + + case NXT_CONF_JSON_MAP_INT32: + case NXT_CONF_JSON_MAP_INT64: + case NXT_CONF_JSON_MAP_INT: + case NXT_CONF_JSON_MAP_SIZE: + case NXT_CONF_JSON_MAP_OFF: + + if (v->type != NXT_CONF_JSON_INTEGER) { + return NXT_ERROR; + } + + switch (map[i].type) { + + case NXT_CONF_JSON_MAP_INT32: + ptr->ui8 = v->u.integer; + break; + + case NXT_CONF_JSON_MAP_INT64: + ptr->i64 = v->u.integer; + break; + + case NXT_CONF_JSON_MAP_INT: + ptr->i = v->u.integer; + break; + + case NXT_CONF_JSON_MAP_SIZE: + ptr->size = v->u.integer; + break; + + case NXT_CONF_JSON_MAP_OFF: + ptr->off = v->u.integer; + break; + + default: + nxt_unreachable(); + } + + break; + + case NXT_CONF_JSON_MAP_DOUBLE: + + if (v->type == NXT_CONF_JSON_NUMBER) { + ptr->dbl = v->u.number; + + } else if (v->type == NXT_CONF_JSON_INTEGER) { + ptr->dbl = v->u.integer; + + } else { + return NXT_ERROR; + } + + break; + + case NXT_CONF_JSON_MAP_STR: + + if (v->type != NXT_CONF_JSON_SHORT_STRING + && v->type != NXT_CONF_JSON_STRING) + { + return NXT_ERROR; + } + + nxt_conf_json_value_get_string(v, &ptr->str); + + break; + + + case NXT_CONF_JSON_MAP_PTR: + + ptr->v = v; + + break; + } + } + + return NXT_OK; +} + + +nxt_int_t nxt_conf_json_op_compile(nxt_mp_t *mp, nxt_conf_json_op_t **ops, nxt_conf_json_value_t *root, nxt_str_t *path, nxt_conf_json_value_t *value) |