CLEANUP: sample: Improve local variables in sample_conv_json_query

This improves the use of local variables in sample_conv_json_query:

- Use the enum type for the return value of `mjson_find`.
- Do not use single letter variables.
- Reduce the scope of variables that are only needed in a single branch.
- Add missing newlines after variable declaration.
This commit is contained in:
Tim Duesterhus 2021-04-15 18:08:48 +02:00 committed by Willy Tarreau
parent 7d9acced27
commit 4809c8c955

View File

@ -3723,16 +3723,17 @@ static int sample_check_json_query(struct arg *arg, struct sample_conv *conv,
static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private) static int sample_conv_json_query(const struct arg *args, struct sample *smp, void *private)
{ {
struct buffer *trash = get_trash_chunk(); struct buffer *trash = get_trash_chunk();
const char *p; /* holds the temporary string from mjson_find */ const char *token; /* holds the temporary string from mjson_find */
int tok, n; /* holds the token enum and the length of the value */ int token_size; /* holds the length of <token> */
int rc; /* holds the return code from mjson_get_string */
tok = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &p, &n); enum mjson_tok token_type;
switch(tok) { token_type = mjson_find(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &token, &token_size);
switch (token_type) {
case MJSON_TOK_NUMBER: case MJSON_TOK_NUMBER:
if (args[1].type == ARGT_SINT) { if (args[1].type == ARGT_SINT) {
smp->data.u.sint = atoll(p); smp->data.u.sint = atoll(token);
if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX) if (smp->data.u.sint < JSON_INT_MIN || smp->data.u.sint > JSON_INT_MAX)
return 0; return 0;
@ -3740,6 +3741,7 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo
smp->data.type = SMP_T_SINT; smp->data.type = SMP_T_SINT;
} else { } else {
double double_val; double double_val;
if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) { if (mjson_get_number(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, &double_val) == 0) {
return 0; return 0;
} else { } else {
@ -3757,17 +3759,21 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo
smp->data.type = SMP_T_BOOL; smp->data.type = SMP_T_BOOL;
smp->data.u.sint = 0; smp->data.u.sint = 0;
break; break;
case MJSON_TOK_STRING: case MJSON_TOK_STRING: {
rc = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size); int len;
if (rc == -1) {
len = mjson_get_string(smp->data.u.str.area, smp->data.u.str.data, args[0].data.str.area, trash->area, trash->size);
if (len == -1) {
/* invalid string */ /* invalid string */
return 0; return 0;
} else { } else {
trash->data = rc; trash->data = len;
smp->data.u.str = *trash; smp->data.u.str = *trash;
smp->data.type = SMP_T_STR; smp->data.type = SMP_T_STR;
} }
break; break;
}
default: default:
/* no valid token found */ /* no valid token found */
return 0; return 0;