mirror of
https://github.com/systemd/systemd-stable.git
synced 2025-01-05 09:17:44 +03:00
json: minor style fixes
This commit is contained in:
parent
85dbc30747
commit
dde8bb32b1
@ -794,7 +794,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {
|
|||||||
} else if (i->tags_job == j) {
|
} else if (i->tags_job == j) {
|
||||||
const char *url;
|
const char *url;
|
||||||
_cleanup_free_ const char *buf;
|
_cleanup_free_ const char *buf;
|
||||||
_cleanup_jsonunref_ JsonVariant *doc = NULL;
|
_cleanup_json_variant_unref_ JsonVariant *doc = NULL;
|
||||||
JsonVariant *e = NULL;
|
JsonVariant *e = NULL;
|
||||||
|
|
||||||
assert(!i->ancestry_job);
|
assert(!i->ancestry_job);
|
||||||
@ -851,8 +851,7 @@ static void dkr_pull_job_on_finished_v2(PullJob *j) {
|
|||||||
|
|
||||||
} else if (i->ancestry_job == j) {
|
} else if (i->ancestry_job == j) {
|
||||||
|
|
||||||
_cleanup_jsonunref_ JsonVariant *doc = NULL;
|
_cleanup_json_variant_unref_ JsonVariant *doc = NULL, *compat = NULL;
|
||||||
_cleanup_jsonunref_ JsonVariant *compat = NULL;
|
|
||||||
JsonVariant *e = NULL;
|
JsonVariant *e = NULL;
|
||||||
_cleanup_strv_free_ char **ancestry = NULL;
|
_cleanup_strv_free_ char **ancestry = NULL;
|
||||||
size_t allocated = 0, size = 0;
|
size_t allocated = 0, size = 0;
|
||||||
|
@ -27,6 +27,7 @@
|
|||||||
|
|
||||||
int json_variant_new(JsonVariant **ret, JsonVariantType type) {
|
int json_variant_new(JsonVariant **ret, JsonVariantType type) {
|
||||||
JsonVariant *v;
|
JsonVariant *v;
|
||||||
|
|
||||||
v = new0(JsonVariant, 1);
|
v = new0(JsonVariant, 1);
|
||||||
if (!v)
|
if (!v)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
@ -36,6 +37,8 @@ int json_variant_new(JsonVariant **ret, JsonVariantType type) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
|
static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
|
||||||
|
int r;
|
||||||
|
|
||||||
assert(ret);
|
assert(ret);
|
||||||
assert(variant);
|
assert(variant);
|
||||||
|
|
||||||
@ -47,18 +50,18 @@ static int json_variant_deep_copy(JsonVariant *ret, JsonVariant *variant) {
|
|||||||
if (!ret->string)
|
if (!ret->string)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
} else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) {
|
} else if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
ret->objects = new0(JsonVariant, variant->size);
|
ret->objects = new0(JsonVariant, variant->size);
|
||||||
if (!ret->objects)
|
if (!ret->objects)
|
||||||
return -ENOMEM;
|
return -ENOMEM;
|
||||||
|
|
||||||
for (unsigned i = 0; i < variant->size; ++i) {
|
for (i = 0; i < variant->size; ++i) {
|
||||||
int r;
|
|
||||||
r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]);
|
r = json_variant_deep_copy(&ret->objects[i], &variant->objects[i]);
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
}
|
} else
|
||||||
else
|
|
||||||
ret->value = variant->value;
|
ret->value = variant->value;
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
@ -72,7 +75,6 @@ static JsonVariant *json_variant_unref_inner(JsonVariant *variant) {
|
|||||||
|
|
||||||
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
|
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
|
||||||
return json_object_unref(variant);
|
return json_object_unref(variant);
|
||||||
|
|
||||||
else if (variant->type == JSON_VARIANT_STRING)
|
else if (variant->type == JSON_VARIANT_STRING)
|
||||||
free(variant->string);
|
free(variant->string);
|
||||||
|
|
||||||
@ -91,11 +93,14 @@ static JsonVariant *json_raw_unref(JsonVariant *variant, size_t size) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static JsonVariant *json_object_unref(JsonVariant *variant) {
|
static JsonVariant *json_object_unref(JsonVariant *variant) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
assert(variant);
|
assert(variant);
|
||||||
|
|
||||||
if (!variant->objects)
|
if (!variant->objects)
|
||||||
return NULL;
|
return NULL;
|
||||||
|
|
||||||
for (unsigned i = 0; i < variant->size; ++i)
|
for (i = 0; i < variant->size; ++i)
|
||||||
json_variant_unref_inner(&variant->objects[i]);
|
json_variant_unref_inner(&variant->objects[i]);
|
||||||
|
|
||||||
free(variant->objects);
|
free(variant->objects);
|
||||||
@ -119,6 +124,7 @@ static JsonVariant **json_variant_array_unref(JsonVariant **variant) {
|
|||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant **, json_variant_array_unref);
|
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant **, json_variant_array_unref);
|
||||||
|
|
||||||
JsonVariant *json_variant_unref(JsonVariant *variant) {
|
JsonVariant *json_variant_unref(JsonVariant *variant) {
|
||||||
@ -127,7 +133,6 @@ JsonVariant *json_variant_unref(JsonVariant *variant) {
|
|||||||
|
|
||||||
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
|
if (variant->type == JSON_VARIANT_ARRAY || variant->type == JSON_VARIANT_OBJECT)
|
||||||
json_object_unref(variant);
|
json_object_unref(variant);
|
||||||
|
|
||||||
else if (variant->type == JSON_VARIANT_STRING)
|
else if (variant->type == JSON_VARIANT_STRING)
|
||||||
free(variant->string);
|
free(variant->string);
|
||||||
|
|
||||||
@ -174,11 +179,13 @@ JsonVariant *json_variant_element(JsonVariant *variant, unsigned index) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
JsonVariant *json_variant_value(JsonVariant *variant, const char *key) {
|
JsonVariant *json_variant_value(JsonVariant *variant, const char *key) {
|
||||||
|
size_t i;
|
||||||
|
|
||||||
assert(variant);
|
assert(variant);
|
||||||
assert(variant->type == JSON_VARIANT_OBJECT);
|
assert(variant->type == JSON_VARIANT_OBJECT);
|
||||||
assert(variant->objects);
|
assert(variant->objects);
|
||||||
|
|
||||||
for (unsigned i = 0; i < variant->size; i += 2) {
|
for (i = 0; i < variant->size; i += 2) {
|
||||||
JsonVariant *p = &variant->objects[i];
|
JsonVariant *p = &variant->objects[i];
|
||||||
if (p->type == JSON_VARIANT_STRING && streq(key, p->string))
|
if (p->type == JSON_VARIANT_STRING && streq(key, p->string))
|
||||||
return &variant->objects[i + 1];
|
return &variant->objects[i + 1];
|
||||||
@ -624,9 +631,11 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
|
|||||||
assert(scope);
|
assert(scope);
|
||||||
|
|
||||||
while((var = *i < n ? tokens[(*i)++] : NULL) != NULL) {
|
while((var = *i < n ? tokens[(*i)++] : NULL) != NULL) {
|
||||||
bool stopper = !json_is_value(var) && var->value.integer == terminator;
|
bool stopper;
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
|
stopper = !json_is_value(var) && var->value.integer == terminator;
|
||||||
|
|
||||||
if (stopper) {
|
if (stopper) {
|
||||||
if (state != STATE_COMMA && size > 0)
|
if (state != STATE_COMMA && size > 0)
|
||||||
goto error;
|
goto error;
|
||||||
@ -655,7 +664,7 @@ static int json_scoped_parse(JsonVariant **tokens, size_t *i, size_t n, JsonVari
|
|||||||
state = STATE_VALUE;
|
state = STATE_VALUE;
|
||||||
}
|
}
|
||||||
else if (state == STATE_VALUE) {
|
else if (state == STATE_VALUE) {
|
||||||
_cleanup_jsonunref_ JsonVariant *v = NULL;
|
_cleanup_json_variant_unref_ JsonVariant *v = NULL;
|
||||||
size_t toadd = arr ? 1 : 2;
|
size_t toadd = arr ? 1 : 2;
|
||||||
|
|
||||||
if (!json_is_value(var)) {
|
if (!json_is_value(var)) {
|
||||||
@ -723,7 +732,7 @@ static int json_parse_tokens(JsonVariant **tokens, size_t ntokens, JsonVariant *
|
|||||||
size_t it = 0;
|
size_t it = 0;
|
||||||
int r;
|
int r;
|
||||||
JsonVariant *e;
|
JsonVariant *e;
|
||||||
_cleanup_jsonunref_ JsonVariant *p = NULL;
|
_cleanup_json_variant_unref_ JsonVariant *p = NULL;
|
||||||
|
|
||||||
assert(tokens);
|
assert(tokens);
|
||||||
assert(ntokens);
|
assert(ntokens);
|
||||||
@ -767,8 +776,8 @@ static int json_tokens(const char *string, size_t size, JsonVariant ***tokens, s
|
|||||||
|
|
||||||
p = buf;
|
p = buf;
|
||||||
for (;;) {
|
for (;;) {
|
||||||
|
_cleanup_json_variant_unref_ JsonVariant *var = NULL;
|
||||||
_cleanup_free_ char *rstr = NULL;
|
_cleanup_free_ char *rstr = NULL;
|
||||||
_cleanup_jsonunref_ JsonVariant *var = NULL;
|
|
||||||
|
|
||||||
t = json_tokenize(&p, &rstr, &v, &json_state, NULL);
|
t = json_tokenize(&p, &rstr, &v, &json_state, NULL);
|
||||||
|
|
||||||
|
@ -57,30 +57,32 @@ union json_value {
|
|||||||
};
|
};
|
||||||
|
|
||||||
typedef struct JsonVariant {
|
typedef struct JsonVariant {
|
||||||
|
JsonVariantType type;
|
||||||
|
size_t size;
|
||||||
union {
|
union {
|
||||||
char *string;
|
char *string;
|
||||||
struct JsonVariant *objects;
|
struct JsonVariant *objects;
|
||||||
union json_value value;
|
union json_value value;
|
||||||
};
|
};
|
||||||
JsonVariantType type;
|
|
||||||
unsigned size;
|
|
||||||
} JsonVariant;
|
} JsonVariant;
|
||||||
|
|
||||||
int json_variant_new(JsonVariant **ret, JsonVariantType type);
|
int json_variant_new(JsonVariant **ret, JsonVariantType type);
|
||||||
JsonVariant *json_variant_unref(JsonVariant *);
|
JsonVariant *json_variant_unref(JsonVariant *v);
|
||||||
|
|
||||||
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
|
DEFINE_TRIVIAL_CLEANUP_FUNC(JsonVariant *, json_variant_unref);
|
||||||
#define _cleanup_jsonunref_ _cleanup_(json_variant_unrefp)
|
#define _cleanup_json_variant_unref_ _cleanup_(json_variant_unrefp)
|
||||||
|
|
||||||
char *json_variant_string(JsonVariant *);
|
char *json_variant_string(JsonVariant *v);
|
||||||
bool json_variant_bool(JsonVariant *);
|
bool json_variant_bool(JsonVariant *v);
|
||||||
intmax_t json_variant_integer(JsonVariant *);
|
intmax_t json_variant_integer(JsonVariant *v);
|
||||||
double json_variant_real(JsonVariant *);
|
double json_variant_real(JsonVariant *v);
|
||||||
|
|
||||||
JsonVariant *json_variant_element(JsonVariant *, unsigned index);
|
JsonVariant *json_variant_element(JsonVariant *v, unsigned index);
|
||||||
JsonVariant *json_variant_value(JsonVariant *, const char *key);
|
JsonVariant *json_variant_value(JsonVariant *v, const char *key);
|
||||||
|
|
||||||
#define JSON_VALUE_NULL ((union json_value) {})
|
#define JSON_VALUE_NULL ((union json_value) {})
|
||||||
|
|
||||||
int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
|
int json_tokenize(const char **p, char **ret_string, union json_value *ret_value, void **state, unsigned *line);
|
||||||
|
|
||||||
int json_parse(const char *string, JsonVariant **rv);
|
int json_parse(const char *string, JsonVariant **rv);
|
||||||
int json_parse_measure(const char *string, size_t *size);
|
int json_parse_measure(const char *string, size_t *size);
|
||||||
|
@ -75,9 +75,10 @@ static void test_one(const char *data, ...) {
|
|||||||
typedef void (*Test)(JsonVariant *);
|
typedef void (*Test)(JsonVariant *);
|
||||||
|
|
||||||
static void test_file(const char *data, Test test) {
|
static void test_file(const char *data, Test test) {
|
||||||
_cleanup_jsonunref_ JsonVariant *v = NULL;
|
_cleanup_json_variant_unref_ JsonVariant *v = NULL;
|
||||||
int r = json_parse(data, &v);
|
int r;
|
||||||
|
|
||||||
|
r = json_parse(data, &v);
|
||||||
assert_se(r == 0);
|
assert_se(r == 0);
|
||||||
assert_se(v != NULL);
|
assert_se(v != NULL);
|
||||||
assert_se(v->type == JSON_VARIANT_OBJECT);
|
assert_se(v->type == JSON_VARIANT_OBJECT);
|
||||||
|
Loading…
Reference in New Issue
Block a user