Skip to content

Commit 59fd390

Browse files
committed
Second attempt at organizing jsonpath operators and methods
Second attempt at 283a95d. 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.
1 parent 0958f8f commit 59fd390

File tree

3 files changed

+298
-291
lines changed

3 files changed

+298
-291
lines changed

src/backend/utils/adt/jsonpath.c

+125-125
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,9 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
519519
case jpiNull:
520520
appendStringInfoString(buf, "null");
521521
break;
522-
case jpiKey:
523-
if (inKey)
524-
appendStringInfoChar(buf, '.');
525-
escape_json(buf, jspGetString(v, NULL));
526-
break;
527522
case jpiString:
528523
escape_json(buf, jspGetString(v, NULL));
529524
break;
530-
case jpiVariable:
531-
appendStringInfoChar(buf, '$');
532-
escape_json(buf, jspGetString(v, NULL));
533-
break;
534525
case jpiNumeric:
535526
if (jspHasNext(v))
536527
appendStringInfoChar(buf, '(');
@@ -576,58 +567,6 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
576567
if (printBracketes)
577568
appendStringInfoChar(buf, ')');
578569
break;
579-
case jpiLikeRegex:
580-
if (printBracketes)
581-
appendStringInfoChar(buf, '(');
582-
583-
jspInitByBuffer(&elem, v->base, v->content.like_regex.expr);
584-
printJsonPathItem(buf, &elem, false,
585-
operationPriority(elem.type) <=
586-
operationPriority(v->type));
587-
588-
appendStringInfoString(buf, " like_regex ");
589-
590-
escape_json(buf, v->content.like_regex.pattern);
591-
592-
if (v->content.like_regex.flags)
593-
{
594-
appendStringInfoString(buf, " flag \"");
595-
596-
if (v->content.like_regex.flags & JSP_REGEX_ICASE)
597-
appendStringInfoChar(buf, 'i');
598-
if (v->content.like_regex.flags & JSP_REGEX_DOTALL)
599-
appendStringInfoChar(buf, 's');
600-
if (v->content.like_regex.flags & JSP_REGEX_MLINE)
601-
appendStringInfoChar(buf, 'm');
602-
if (v->content.like_regex.flags & JSP_REGEX_WSPACE)
603-
appendStringInfoChar(buf, 'x');
604-
if (v->content.like_regex.flags & JSP_REGEX_QUOTE)
605-
appendStringInfoChar(buf, 'q');
606-
607-
appendStringInfoChar(buf, '"');
608-
}
609-
610-
if (printBracketes)
611-
appendStringInfoChar(buf, ')');
612-
break;
613-
case jpiPlus:
614-
case jpiMinus:
615-
if (printBracketes)
616-
appendStringInfoChar(buf, '(');
617-
appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-');
618-
jspGetArg(v, &elem);
619-
printJsonPathItem(buf, &elem, false,
620-
operationPriority(elem.type) <=
621-
operationPriority(v->type));
622-
if (printBracketes)
623-
appendStringInfoChar(buf, ')');
624-
break;
625-
case jpiFilter:
626-
appendStringInfoString(buf, "?(");
627-
jspGetArg(v, &elem);
628-
printJsonPathItem(buf, &elem, false, false);
629-
appendStringInfoChar(buf, ')');
630-
break;
631570
case jpiNot:
632571
appendStringInfoString(buf, "!(");
633572
jspGetArg(v, &elem);
@@ -640,22 +579,17 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
640579
printJsonPathItem(buf, &elem, false, false);
641580
appendStringInfoString(buf, ") is unknown");
642581
break;
643-
case jpiExists:
644-
appendStringInfoString(buf, "exists (");
582+
case jpiPlus:
583+
case jpiMinus:
584+
if (printBracketes)
585+
appendStringInfoChar(buf, '(');
586+
appendStringInfoChar(buf, v->type == jpiPlus ? '+' : '-');
645587
jspGetArg(v, &elem);
646-
printJsonPathItem(buf, &elem, false, false);
647-
appendStringInfoChar(buf, ')');
648-
break;
649-
case jpiCurrent:
650-
Assert(!inKey);
651-
appendStringInfoChar(buf, '@');
652-
break;
653-
case jpiRoot:
654-
Assert(!inKey);
655-
appendStringInfoChar(buf, '$');
656-
break;
657-
case jpiLast:
658-
appendStringInfoString(buf, "last");
588+
printJsonPathItem(buf, &elem, false,
589+
operationPriority(elem.type) <=
590+
operationPriority(v->type));
591+
if (printBracketes)
592+
appendStringInfoChar(buf, ')');
659593
break;
660594
case jpiAnyArray:
661595
appendStringInfoString(buf, "[*]");
@@ -712,6 +646,35 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
712646
v->content.anybounds.first,
713647
v->content.anybounds.last);
714648
break;
649+
case jpiKey:
650+
if (inKey)
651+
appendStringInfoChar(buf, '.');
652+
escape_json(buf, jspGetString(v, NULL));
653+
break;
654+
case jpiCurrent:
655+
Assert(!inKey);
656+
appendStringInfoChar(buf, '@');
657+
break;
658+
case jpiRoot:
659+
Assert(!inKey);
660+
appendStringInfoChar(buf, '$');
661+
break;
662+
case jpiVariable:
663+
appendStringInfoChar(buf, '$');
664+
escape_json(buf, jspGetString(v, NULL));
665+
break;
666+
case jpiFilter:
667+
appendStringInfoString(buf, "?(");
668+
jspGetArg(v, &elem);
669+
printJsonPathItem(buf, &elem, false, false);
670+
appendStringInfoChar(buf, ')');
671+
break;
672+
case jpiExists:
673+
appendStringInfoString(buf, "exists (");
674+
jspGetArg(v, &elem);
675+
printJsonPathItem(buf, &elem, false, false);
676+
appendStringInfoChar(buf, ')');
677+
break;
715678
case jpiType:
716679
appendStringInfoString(buf, ".type()");
717680
break;
@@ -742,6 +705,43 @@ printJsonPathItem(StringInfo buf, JsonPathItem *v, bool inKey,
742705
case jpiKeyValue:
743706
appendStringInfoString(buf, ".keyvalue()");
744707
break;
708+
case jpiLast:
709+
appendStringInfoString(buf, "last");
710+
break;
711+
case jpiLikeRegex:
712+
if (printBracketes)
713+
appendStringInfoChar(buf, '(');
714+
715+
jspInitByBuffer(&elem, v->base, v->content.like_regex.expr);
716+
printJsonPathItem(buf, &elem, false,
717+
operationPriority(elem.type) <=
718+
operationPriority(v->type));
719+
720+
appendStringInfoString(buf, " like_regex ");
721+
722+
escape_json(buf, v->content.like_regex.pattern);
723+
724+
if (v->content.like_regex.flags)
725+
{
726+
appendStringInfoString(buf, " flag \"");
727+
728+
if (v->content.like_regex.flags & JSP_REGEX_ICASE)
729+
appendStringInfoChar(buf, 'i');
730+
if (v->content.like_regex.flags & JSP_REGEX_DOTALL)
731+
appendStringInfoChar(buf, 's');
732+
if (v->content.like_regex.flags & JSP_REGEX_MLINE)
733+
appendStringInfoChar(buf, 'm');
734+
if (v->content.like_regex.flags & JSP_REGEX_WSPACE)
735+
appendStringInfoChar(buf, 'x');
736+
if (v->content.like_regex.flags & JSP_REGEX_QUOTE)
737+
appendStringInfoChar(buf, 'q');
738+
739+
appendStringInfoChar(buf, '"');
740+
}
741+
742+
if (printBracketes)
743+
appendStringInfoChar(buf, ')');
744+
break;
745745
default:
746746
elog(ERROR, "unrecognized jsonpath item type: %d", v->type);
747747
}
@@ -771,38 +771,38 @@ jspOperationName(JsonPathItemType type)
771771
return "<=";
772772
case jpiGreaterOrEqual:
773773
return ">=";
774-
case jpiPlus:
775774
case jpiAdd:
775+
case jpiPlus:
776776
return "+";
777-
case jpiMinus:
778777
case jpiSub:
778+
case jpiMinus:
779779
return "-";
780780
case jpiMul:
781781
return "*";
782782
case jpiDiv:
783783
return "/";
784784
case jpiMod:
785785
return "%";
786-
case jpiStartsWith:
787-
return "starts with";
788-
case jpiLikeRegex:
789-
return "like_regex";
790786
case jpiType:
791787
return "type";
792788
case jpiSize:
793789
return "size";
794-
case jpiKeyValue:
795-
return "keyvalue";
796-
case jpiDouble:
797-
return "double";
798790
case jpiAbs:
799791
return "abs";
800792
case jpiFloor:
801793
return "floor";
802794
case jpiCeiling:
803795
return "ceiling";
796+
case jpiDouble:
797+
return "double";
804798
case jpiDatetime:
805799
return "datetime";
800+
case jpiKeyValue:
801+
return "keyvalue";
802+
case jpiStartsWith:
803+
return "starts with";
804+
case jpiLikeRegex:
805+
return "like_regex";
806806
default:
807807
elog(ERROR, "unrecognized jsonpath item type: %d", type);
808808
return NULL;
@@ -900,8 +900,8 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
900900
case jpiKeyValue:
901901
case jpiLast:
902902
break;
903-
case jpiKey:
904903
case jpiString:
904+
case jpiKey:
905905
case jpiVariable:
906906
read_int32(v->content.value.datalen, base, pos);
907907
/* FALLTHROUGH */
@@ -911,30 +911,24 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
911911
break;
912912
case jpiAnd:
913913
case jpiOr:
914-
case jpiAdd:
915-
case jpiSub:
916-
case jpiMul:
917-
case jpiDiv:
918-
case jpiMod:
919914
case jpiEqual:
920915
case jpiNotEqual:
921916
case jpiLess:
922917
case jpiGreater:
923918
case jpiLessOrEqual:
924919
case jpiGreaterOrEqual:
920+
case jpiAdd:
921+
case jpiSub:
922+
case jpiMul:
923+
case jpiDiv:
924+
case jpiMod:
925925
case jpiStartsWith:
926926
read_int32(v->content.args.left, base, pos);
927927
read_int32(v->content.args.right, base, pos);
928928
break;
929-
case jpiLikeRegex:
930-
read_int32(v->content.like_regex.flags, base, pos);
931-
read_int32(v->content.like_regex.expr, base, pos);
932-
read_int32(v->content.like_regex.patternlen, base, pos);
933-
v->content.like_regex.pattern = base + pos;
934-
break;
935929
case jpiNot:
936-
case jpiExists:
937930
case jpiIsUnknown:
931+
case jpiExists:
938932
case jpiPlus:
939933
case jpiMinus:
940934
case jpiFilter:
@@ -950,6 +944,12 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
950944
read_int32(v->content.anybounds.first, base, pos);
951945
read_int32(v->content.anybounds.last, base, pos);
952946
break;
947+
case jpiLikeRegex:
948+
read_int32(v->content.like_regex.flags, base, pos);
949+
read_int32(v->content.like_regex.expr, base, pos);
950+
read_int32(v->content.like_regex.patternlen, base, pos);
951+
v->content.like_regex.pattern = base + pos;
952+
break;
953953
default:
954954
elog(ERROR, "unrecognized jsonpath item type: %d", v->type);
955955
}
@@ -958,12 +958,12 @@ jspInitByBuffer(JsonPathItem *v, char *base, int32 pos)
958958
void
959959
jspGetArg(JsonPathItem *v, JsonPathItem *a)
960960
{
961-
Assert(v->type == jpiFilter ||
962-
v->type == jpiNot ||
961+
Assert(v->type == jpiNot ||
963962
v->type == jpiIsUnknown ||
964-
v->type == jpiExists ||
965963
v->type == jpiPlus ||
966964
v->type == jpiMinus ||
965+
v->type == jpiFilter ||
966+
v->type == jpiExists ||
967967
v->type == jpiDatetime);
968968

969969
jspInitByBuffer(a, v->base, v->content.arg);
@@ -974,38 +974,37 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
974974
{
975975
if (jspHasNext(v))
976976
{
977-
Assert(v->type == jpiString ||
977+
Assert(v->type == jpiNull ||
978+
v->type == jpiString ||
978979
v->type == jpiNumeric ||
979980
v->type == jpiBool ||
980-
v->type == jpiNull ||
981-
v->type == jpiKey ||
982-
v->type == jpiAny ||
983-
v->type == jpiAnyArray ||
984-
v->type == jpiAnyKey ||
985-
v->type == jpiIndexArray ||
986-
v->type == jpiFilter ||
987-
v->type == jpiCurrent ||
988-
v->type == jpiExists ||
989-
v->type == jpiRoot ||
990-
v->type == jpiVariable ||
991-
v->type == jpiLast ||
981+
v->type == jpiAnd ||
982+
v->type == jpiOr ||
983+
v->type == jpiNot ||
984+
v->type == jpiIsUnknown ||
985+
v->type == jpiEqual ||
986+
v->type == jpiNotEqual ||
987+
v->type == jpiLess ||
988+
v->type == jpiGreater ||
989+
v->type == jpiLessOrEqual ||
990+
v->type == jpiGreaterOrEqual ||
992991
v->type == jpiAdd ||
993992
v->type == jpiSub ||
994993
v->type == jpiMul ||
995994
v->type == jpiDiv ||
996995
v->type == jpiMod ||
997996
v->type == jpiPlus ||
998997
v->type == jpiMinus ||
999-
v->type == jpiEqual ||
1000-
v->type == jpiNotEqual ||
1001-
v->type == jpiGreater ||
1002-
v->type == jpiGreaterOrEqual ||
1003-
v->type == jpiLess ||
1004-
v->type == jpiLessOrEqual ||
1005-
v->type == jpiAnd ||
1006-
v->type == jpiOr ||
1007-
v->type == jpiNot ||
1008-
v->type == jpiIsUnknown ||
998+
v->type == jpiAnyArray ||
999+
v->type == jpiAnyKey ||
1000+
v->type == jpiIndexArray ||
1001+
v->type == jpiAny ||
1002+
v->type == jpiKey ||
1003+
v->type == jpiCurrent ||
1004+
v->type == jpiRoot ||
1005+
v->type == jpiVariable ||
1006+
v->type == jpiFilter ||
1007+
v->type == jpiExists ||
10091008
v->type == jpiType ||
10101009
v->type == jpiSize ||
10111010
v->type == jpiAbs ||
@@ -1014,6 +1013,7 @@ jspGetNext(JsonPathItem *v, JsonPathItem *a)
10141013
v->type == jpiDouble ||
10151014
v->type == jpiDatetime ||
10161015
v->type == jpiKeyValue ||
1016+
v->type == jpiLast ||
10171017
v->type == jpiStartsWith ||
10181018
v->type == jpiLikeRegex);
10191019

0 commit comments

Comments
 (0)