Changeset: e2a49777af60 for MonetDB
URL: http://dev.monetdb.org/hg/MonetDB?cmd=changeset;node=e2a49777af60
Modified Files:
monetdb5/modules/atoms/Tests/json01.mal
monetdb5/modules/atoms/Tests/json01.stable.out
monetdb5/modules/atoms/json.c
Branch: default
Log Message:
Allow for multiple path expressions
A list of path expression is interpreted in the filter command.
This avoid duplicate parsing in compound projects.
diffs (101 lines):
diff --git a/monetdb5/modules/atoms/Tests/json01.mal
b/monetdb5/modules/atoms/Tests/json01.mal
--- a/monetdb5/modules/atoms/Tests/json01.mal
+++ b/monetdb5/modules/atoms/Tests/json01.mal
@@ -8,6 +8,8 @@ io.print(f);
b:= json.new("{\"f1\":1,\"f2\":2}");
f:= json.filter(b,"f2");
io.print(f);
+f:= json.filter(b,"f1,f2");
+io.print(f);
b:= json.new("{\"f1\":1,\"f2\":2,\"f1\":3}");
f:= json.filter(b,"f1[0]");
io.print(f);
diff --git a/monetdb5/modules/atoms/Tests/json01.stable.out
b/monetdb5/modules/atoms/Tests/json01.stable.out
--- a/monetdb5/modules/atoms/Tests/json01.stable.out
+++ b/monetdb5/modules/atoms/Tests/json01.stable.out
@@ -29,6 +29,8 @@ function user.main():void;
b := json.new("{\"f1\":1,\"f2\":2}");
f := json.filter(b,"f2");
io.print(f);
+ f := json.filter(b,"f1,f2");
+ io.print(f);
b := json.new("{\"f1\":1,\"f2\":2,\"f1\":3}");
f := json.filter(b,"f1[0]");
io.print(f);
@@ -77,6 +79,7 @@ end main;
[ "[]" ]
[ "[1]" ]
[ "[2]" ]
+[ "[1,2]" ]
[ "[1]" ]
[ "[3]" ]
[ "[]" ]
diff --git a/monetdb5/modules/atoms/json.c b/monetdb5/modules/atoms/json.c
--- a/monetdb5/modules/atoms/json.c
+++ b/monetdb5/modules/atoms/json.c
@@ -371,7 +371,7 @@ JSONcompile(char *expr, pattern terms[])
// child step
if (*s != '[') {
for (beg = s; *s; s++)
- if (*s == '.' || *s == '[')
+ if (*s == '.' || *s == '[' || *s == ',')
break;
terms[t].name = GDKzalloc(s - beg + 1);
terms[t].namelen = s - beg;
@@ -404,6 +404,11 @@ JSONcompile(char *expr, pattern terms[])
if (*s != ']')
throw(MAL, "json.path", "] expected");
}
+ if ( *s == ','){
+ if (++t == MAXTERMS)
+ throw(MAL, "json.path", "too many terms");
+ terms[t].token = END_STEP;
+ }
if (++t == MAXTERMS)
throw(MAL, "json.path", "too many terms");
}
@@ -524,6 +529,7 @@ static str
JSONfilterInternal(json *ret, json *js, str *expr, str other)
{
pattern terms[MAXTERMS];
+ int tidx = 0;
JSON *jt;
str j = *js, msg = MAL_SUCCEED, s;
json result = 0;
@@ -545,20 +551,26 @@ JSONfilterInternal(json *ret, json *js,
return msg;
}
- // unwrap the outer brackets before matching
- s = JSONmatch(jt, 0, terms, 0);
- if ( s ) {
- result = (char *) GDKzalloc(l = strlen(s) + 4);
- if (result )
- snprintf(result,l,"[%s]",s);
- else
- msg = createException(MAL, "json.path",
MAL_MALLOC_FAIL);
- } else result = (char*) GDKstrdup("[]");
+ result = s= JSONmatch(jt, 0, terms, tidx);
+ // process all other PATH expression
+ for(tidx++; tidx <MAXTERMS && terms[tidx].token ; tidx++)
+ if ( terms[tidx].token == END_STEP && tidx+1 < MAXTERMS &&
terms[tidx+1].token){
+ s = JSONmatch(jt, 0, terms, ++tidx);
+ result = JSONglue(result,s,',');
+ }
+ if ( s) {
+ l = strlen(s);
+ if( s[l-1] == ',')
+ s[l-1]=0;
+ } else l= 3;
+ s = GDKzalloc(l+3);
+ snprintf(s,l+3,"[%s]", (result?result:""));
+
for (l = 0; terms[l].token; l++)
if (terms[l].name)
GDKfree(terms[l].name);
JSONfree(jt);
- *ret = result;
+ *ret = s;
return msg;
}
_______________________________________________
checkin-list mailing list
[email protected]
https://www.monetdb.org/mailman/listinfo/checkin-list