From 59fd390d5e8a7e169ff8f095882e32e189f3c5bd Mon Sep 17 00:00:00 2001 From: Peter Eisentraut Date: Wed, 3 Jan 2024 21:56:41 +0100 Subject: Second attempt at organizing jsonpath operators and methods Second attempt at 283a95da923. Since we can't reorder the enum values of JsonPathItemType, instead reorder the switch cases where they are used to generally follow the order of the enum values, for better maintainability. --- src/backend/utils/adt/jsonpath.c | 250 +++++++++++++++++++-------------------- 1 file changed, 125 insertions(+), 125 deletions(-) (limited to 'src/backend/utils/adt/jsonpath.c') diff --git a/src/backend/utils/adt/jsonpath.c b/src/backend/utils/adt/jsonpath.c index c5ba3b7f1d0..0a16f93f39b 100644 --- a/src/backend/utils/adt/jsonpath.c +++ b/src/backend/utils/adt/jsonpath.c @@ -519,18 +519,9 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, case jpiNull: appendStringInfoString(buf, "null"); break; - case jpiKey: - if (inKey) - appendStringInfoChar(buf, '.'); - escape_json(buf, jspGetString(v, NULL)); - break; case jpiString: escape_json(buf, jspGetString(v, NULL)); break; - case jpiVariable: - appendStringInfoChar(buf, '$'); - escape_json(buf, jspGetString(v, NULL)); - break; case jpiNumeric: if (jspHasNext(v)) appendStringInfoChar(buf, '('); @@ -576,58 +567,6 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, if (printBracketes) appendStringInfoChar(buf, ')'); break; - case jpiLikeRegex: - if (printBracketes) - appendStringInfoChar(buf, '('); - - jspInitByBuffer(&elem, v->base, v->content.like_regex.expr); - printJsonPathItem(buf, &elem, false, - operationPriority(elem.type) <= - operationPriority(v->type)); - - appendStringInfoString(buf, " like_regex "); - - escape_json(buf, v->content.like_regex.pattern); - - if (v->content.like_regex.flags) - { - appendStringInfoString(buf, " flag \""); - - if (v->content.like_regex.flags & JSP_REGEX_ICASE) - appendStringInfoChar(buf, 'i'); - if (v->content.like_regex.flags & JSP_REGEX_DOTALL) - appendStringInfoChar(buf, 's'); - if (v->content.like_regex.flags & JSP_REGEX_MLINE) - appendStringInfoChar(buf, 'm'); - if (v->content.like_regex.flags & JSP_REGEX_WSPACE) - appendStringInfoChar(buf, 'x'); - if (v->content.like_regex.flags & JSP_REGEX_QUOTE) - appendStringInfoChar(buf, 'q'); - - appendStringInfoChar(buf, '"'); - } - - if (printBracketes) - appendStringInfoChar(buf, ')'); - break; - case jpiPlus: - case jpiMinus: - if (printBracketes) - appendStringInfoChar(buf, '('); - appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-'); - jspGetArg(v, &elem); - printJsonPathItem(buf, &elem, false, - operationPriority(elem.type) <= - operationPriority(v->type)); - if (printBracketes) - appendStringInfoChar(buf, ')'); - break; - case jpiFilter: - appendStringInfoString(buf, "?("); - jspGetArg(v, &elem); - printJsonPathItem(buf, &elem, false, false); - appendStringInfoChar(buf, ')'); - break; case jpiNot: appendStringInfoString(buf, "!("); jspGetArg(v, &elem); @@ -640,22 +579,17 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, printJsonPathItem(buf, &elem, false, false); appendStringInfoString(buf, ") is unknown"); break; - case jpiExists: - appendStringInfoString(buf, "exists ("); + case jpiPlus: + case jpiMinus: + if (printBracketes) + appendStringInfoChar(buf, '('); + appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-'); jspGetArg(v, &elem); - printJsonPathItem(buf, &elem, false, false); - appendStringInfoChar(buf, ')'); - break; - case jpiCurrent: - Assert(!inKey); - appendStringInfoChar(buf, '@'); - break; - case jpiRoot: - Assert(!inKey); - appendStringInfoChar(buf, '$'); - break; - case jpiLast: - appendStringInfoString(buf, "last"); + printJsonPathItem(buf, &elem, false, + operationPriority(elem.type) <= + operationPriority(v->type)); + if (printBracketes) + appendStringInfoChar(buf, ')'); break; case jpiAnyArray: appendStringInfoString(buf, "[*]"); @@ -712,6 +646,35 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, v->content.anybounds.first, v->content.anybounds.last); break; + case jpiKey: + if (inKey) + appendStringInfoChar(buf, '.'); + escape_json(buf, jspGetString(v, NULL)); + break; + case jpiCurrent: + Assert(!inKey); + appendStringInfoChar(buf, '@'); + break; + case jpiRoot: + Assert(!inKey); + appendStringInfoChar(buf, '$'); + break; + case jpiVariable: + appendStringInfoChar(buf, '$'); + escape_json(buf, jspGetString(v, NULL)); + break; + case jpiFilter: + appendStringInfoString(buf, "?("); + jspGetArg(v, &elem); + printJsonPathItem(buf, &elem, false, false); + appendStringInfoChar(buf, ')'); + break; + case jpiExists: + appendStringInfoString(buf, "exists ("); + jspGetArg(v, &elem); + printJsonPathItem(buf, &elem, false, false); + appendStringInfoChar(buf, ')'); + break; case jpiType: appendStringInfoString(buf, ".type()"); break; @@ -742,6 +705,43 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey, case jpiKeyValue: appendStringInfoString(buf, ".keyvalue()"); break; + case jpiLast: + appendStringInfoString(buf, "last"); + break; + case jpiLikeRegex: + if (printBracketes) + appendStringInfoChar(buf, '('); + + jspInitByBuffer(&elem, v->base, v->content.like_regex.expr); + printJsonPathItem(buf, &elem, false, + operationPriority(elem.type) <= + operationPriority(v->type)); + + appendStringInfoString(buf, " like_regex "); + + escape_json(buf, v->content.like_regex.pattern); + + if (v->content.like_regex.flags) + { + appendStringInfoString(buf, " flag \""); + + if (v->content.like_regex.flags & JSP_REGEX_ICASE) + appendStringInfoChar(buf, 'i'); + if (v->content.like_regex.flags & JSP_REGEX_DOTALL) + appendStringInfoChar(buf, 's'); + if (v->content.like_regex.flags & JSP_REGEX_MLINE) + appendStringInfoChar(buf, 'm'); + if (v->content.like_regex.flags & JSP_REGEX_WSPACE) + appendStringInfoChar(buf, 'x'); + if (v->content.like_regex.flags & JSP_REGEX_QUOTE) + appendStringInfoChar(buf, 'q'); + + appendStringInfoChar(buf, '"'); + } + + if (printBracketes) + appendStringInfoChar(buf, ')'); + break; default: elog(ERROR, "unrecognized jsonpath item type: %d", v->type); } @@ -771,11 +771,11 @@ jspOperationName(JsonPathItemType type) return "<="; case jpiGreaterOrEqual: return ">="; - case jpiPlus: case jpiAdd: + case jpiPlus: return "+"; - case jpiMinus: case jpiSub: + case jpiMinus: return "-"; case jpiMul: return "*"; @@ -783,26 +783,26 @@ jspOperationName(JsonPathItemType type) return "/"; case jpiMod: return "%"; - case jpiStartsWith: - return "starts with"; - case jpiLikeRegex: - return "like_regex"; case jpiType: return "type"; case jpiSize: return "size"; - case jpiKeyValue: - return "keyvalue"; - case jpiDouble: - return "double"; case jpiAbs: return "abs"; case jpiFloor: return "floor"; case jpiCeiling: return "ceiling"; + case jpiDouble: + return "double"; case jpiDatetime: return "datetime"; + case jpiKeyValue: + return "keyvalue"; + case jpiStartsWith: + return "starts with"; + case jpiLikeRegex: + return "like_regex"; default: elog(ERROR, "unrecognized jsonpath item type: %d", type); return NULL; @@ -900,8 +900,8 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos) case jpiKeyValue: case jpiLast: break; - case jpiKey: case jpiString: + case jpiKey: case jpiVariable: read_int32(v->content.value.datalen, base, pos); /* FALLTHROUGH */ @@ -911,30 +911,24 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos) break; case jpiAnd: case jpiOr: - case jpiAdd: - case jpiSub: - case jpiMul: - case jpiDiv: - case jpiMod: case jpiEqual: case jpiNotEqual: case jpiLess: case jpiGreater: case jpiLessOrEqual: case jpiGreaterOrEqual: + case jpiAdd: + case jpiSub: + case jpiMul: + case jpiDiv: + case jpiMod: case jpiStartsWith: read_int32(v->content.args.left, base, pos); read_int32(v->content.args.right, base, pos); break; - case jpiLikeRegex: - read_int32(v->content.like_regex.flags, base, pos); - read_int32(v->content.like_regex.expr, base, pos); - read_int32(v->content.like_regex.patternlen, base, pos); - v->content.like_regex.pattern = base + pos; - break; case jpiNot: - case jpiExists: case jpiIsUnknown: + case jpiExists: case jpiPlus: case jpiMinus: case jpiFilter: @@ -950,6 +944,12 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos) read_int32(v->content.anybounds.first, base, pos); read_int32(v->content.anybounds.last, base, pos); break; + case jpiLikeRegex: + read_int32(v->content.like_regex.flags, base, pos); + read_int32(v->content.like_regex.expr, base, pos); + read_int32(v->content.like_regex.patternlen, base, pos); + v->content.like_regex.pattern = base + pos; + break; default: elog(ERROR, "unrecognized jsonpath item type: %d", v->type); } @@ -958,12 +958,12 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos) void jspGetArg(JsonPathItem *v, JsonPathItem *a) { - Assert(v->type == jpiFilter || - v->type == jpiNot || + Assert(v->type == jpiNot || v->type == jpiIsUnknown || - v->type == jpiExists || v->type == jpiPlus || v->type == jpiMinus || + v->type == jpiFilter || + v->type == jpiExists || v->type == jpiDatetime); jspInitByBuffer(a, v->base, v->content.arg); @@ -974,21 +974,20 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a) { if (jspHasNext(v)) { - Assert(v->type == jpiString || + Assert(v->type == jpiNull || + v->type == jpiString || v->type == jpiNumeric || v->type == jpiBool || - v->type == jpiNull || - v->type == jpiKey || - v->type == jpiAny || - v->type == jpiAnyArray || - v->type == jpiAnyKey || - v->type == jpiIndexArray || - v->type == jpiFilter || - v->type == jpiCurrent || - v->type == jpiExists || - v->type == jpiRoot || - v->type == jpiVariable || - v->type == jpiLast || + v->type == jpiAnd || + v->type == jpiOr || + v->type == jpiNot || + v->type == jpiIsUnknown || + v->type == jpiEqual || + v->type == jpiNotEqual || + v->type == jpiLess || + v->type == jpiGreater || + v->type == jpiLessOrEqual || + v->type == jpiGreaterOrEqual || v->type == jpiAdd || v->type == jpiSub || v->type == jpiMul || @@ -996,16 +995,16 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a) v->type == jpiMod || v->type == jpiPlus || v->type == jpiMinus || - v->type == jpiEqual || - v->type == jpiNotEqual || - v->type == jpiGreater || - v->type == jpiGreaterOrEqual || - v->type == jpiLess || - v->type == jpiLessOrEqual || - v->type == jpiAnd || - v->type == jpiOr || - v->type == jpiNot || - v->type == jpiIsUnknown || + v->type == jpiAnyArray || + v->type == jpiAnyKey || + v->type == jpiIndexArray || + v->type == jpiAny || + v->type == jpiKey || + v->type == jpiCurrent || + v->type == jpiRoot || + v->type == jpiVariable || + v->type == jpiFilter || + v->type == jpiExists || v->type == jpiType || v->type == jpiSize || v->type == jpiAbs || @@ -1014,6 +1013,7 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a) v->type == jpiDouble || v->type == jpiDatetime || v->type == jpiKeyValue || + v->type == jpiLast || v->type == jpiStartsWith || v->type == jpiLikeRegex); -- cgit v1.2.3