Interface for mapping JSON configuration objects to C structures.

This commit is contained in:
Valentin Bartenev
2017-06-26 21:41:58 +03:00
parent 01517e37c1
commit f86c803098
3 changed files with 148 additions and 3 deletions

View File

@@ -173,6 +173,10 @@ nxt_container_of(p, type, field) \
(type *) ((u_char *) (p) - offsetof(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) \ #define nxt_value_at(type, p, offset) \
*(type *) ((u_char *) p + offset) *(type *) ((u_char *) p + offset)

View File

@@ -13,6 +13,26 @@ 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 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 { typedef struct {
uint32_t level; uint32_t level;
uint8_t more_space; /* 1 bit. */ uint8_t more_space; /* 1 bit. */
@@ -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 *nxt_conf_json_object_get_member(
nxt_conf_json_value_t *value, nxt_str_t *name, uint32_t *index); 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_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 *root, nxt_str_t *path,
nxt_conf_json_value_t *value); nxt_conf_json_value_t *value);

View File

@@ -44,7 +44,7 @@ struct nxt_conf_json_value_s {
union { union {
uint32_t boolean; /* 1 bit. */ uint32_t boolean; /* 1 bit. */
int64_t integer; int64_t integer;
/* double number; */ double number;
u_char str[1 + NXT_CONF_JSON_STR_SIZE]; u_char str[1 + NXT_CONF_JSON_STR_SIZE];
nxt_str_t *string; nxt_str_t *string;
nxt_conf_json_array_t *array; nxt_conf_json_array_t *array;
@@ -252,6 +252,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_int_t
nxt_conf_json_op_compile(nxt_mp_t *mp, nxt_conf_json_op_t **ops, 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 *root, nxt_str_t *path,