1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-28 11:44:57 +03:00

Make some use of anonymous unions [libpq-oauth]

Make some use of anonymous unions, which are allowed as of C11, as
examples and encouragement for future code, and to test compilers.

This commit changes the json_field struct.

Reviewed-by: Nathan Bossart <nathandbossart@gmail.com>
Discussion: https://postgr.es/m/CAOYmi%2BnV25oC5uXFgWodydGrHkfWMDCLUcjbAreM3mNX%3DF2JWw%40mail.gmail.com
This commit is contained in:
Jacob Champion
2025-11-24 09:55:16 -08:00
parent bc32a12e0d
commit f1881c7dd3

View File

@@ -439,7 +439,7 @@ struct json_field
{ {
char **scalar; /* for all scalar types */ char **scalar; /* for all scalar types */
struct curl_slist **array; /* for type == JSON_TOKEN_ARRAY_START */ struct curl_slist **array; /* for type == JSON_TOKEN_ARRAY_START */
} target; };
bool required; /* REQUIRED field, or just OPTIONAL? */ bool required; /* REQUIRED field, or just OPTIONAL? */
}; };
@@ -561,8 +561,8 @@ oauth_json_object_field_start(void *state, char *name, bool isnull)
{ {
field = ctx->active; field = ctx->active;
if ((field->type == JSON_TOKEN_ARRAY_START && *field->target.array) if ((field->type == JSON_TOKEN_ARRAY_START && *field->array)
|| (field->type != JSON_TOKEN_ARRAY_START && *field->target.scalar)) || (field->type != JSON_TOKEN_ARRAY_START && *field->scalar))
{ {
oauth_parse_set_error(ctx, "field \"%s\" is duplicated", oauth_parse_set_error(ctx, "field \"%s\" is duplicated",
field->name); field->name);
@@ -706,7 +706,7 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
} }
/* ...and that a result has not already been set. */ /* ...and that a result has not already been set. */
if (*field->target.scalar) if (*field->scalar)
{ {
Assert(false); Assert(false);
oauth_parse_set_error(ctx, oauth_parse_set_error(ctx,
@@ -715,8 +715,8 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
return JSON_SEM_ACTION_FAILED; return JSON_SEM_ACTION_FAILED;
} }
*field->target.scalar = strdup(token); *field->scalar = strdup(token);
if (!*field->target.scalar) if (!*field->scalar)
return JSON_OUT_OF_MEMORY; return JSON_OUT_OF_MEMORY;
ctx->active = NULL; ctx->active = NULL;
@@ -738,11 +738,11 @@ oauth_json_scalar(void *state, char *token, JsonTokenType type)
} }
/* Note that curl_slist_append() makes a copy of the token. */ /* Note that curl_slist_append() makes a copy of the token. */
temp = curl_slist_append(*field->target.array, token); temp = curl_slist_append(*field->array, token);
if (!temp) if (!temp)
return JSON_OUT_OF_MEMORY; return JSON_OUT_OF_MEMORY;
*field->target.array = temp; *field->array = temp;
} }
} }
else else
@@ -878,8 +878,8 @@ parse_oauth_json(struct async_ctx *actx, const struct json_field *fields)
while (fields->name) while (fields->name)
{ {
if (fields->required if (fields->required
&& !*fields->target.scalar && !*fields->scalar
&& !*fields->target.array) && !*fields->array)
{ {
actx_error(actx, "field \"%s\" is missing", fields->name); actx_error(actx, "field \"%s\" is missing", fields->name);
goto cleanup; goto cleanup;