Skip to content

Commit 10c3f2c

Browse files
authored
Merge pull request #92 from sergeevik/ISSUES-91
ISSUES-91: fix regex matcher body and next func
2 parents 0e9c9a0 + 584a598 commit 10c3f2c

File tree

3 files changed

+62
-6
lines changed

3 files changed

+62
-6
lines changed

src/main/java/com/dashjoin/jsonata/Jsonata.java

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import java.util.function.BiFunction;
3838
import java.util.function.Function;
3939
import java.util.function.Supplier;
40+
import java.util.regex.Matcher;
4041
import java.util.regex.Pattern;
4142
import java.util.stream.Collectors;
4243

@@ -1753,14 +1754,18 @@ Jsonata getPerThreadInstance() {
17531754
}
17541755
} else if (proc instanceof Pattern) {
17551756
List _res = new ArrayList<>();
1756-
for (String s : (List<String>)validatedArgs) {
1757+
for (Object s : (List)validatedArgs) {
17571758
//System.err.println("PAT "+proc+" input "+s);
1758-
if (((Pattern)proc).matcher(s).find()) {
1759-
//System.err.println("MATCH");
1760-
_res.add(s);
1759+
if (s instanceof String) {
1760+
Matcher matcher = ((Pattern) proc).matcher((String) s);
1761+
_res.add(regexClosure(matcher));
17611762
}
17621763
}
1763-
result = _res;
1764+
if (_res.size() == 1) {
1765+
result = _res.get(0);
1766+
} else {
1767+
result = _res;
1768+
}
17641769
} else {
17651770
System.out.println("Proc not found "+proc);
17661771
throw new JException(
@@ -1779,6 +1784,21 @@ Jsonata getPerThreadInstance() {
17791784
}
17801785
return result;
17811786
}
1787+
1788+
private static Map regexClosure(Matcher matcher) {
1789+
if (matcher.find()) {
1790+
String group = matcher.group();
1791+
return Map.of(
1792+
"match", group,
1793+
"start", matcher.start(),
1794+
"end", matcher.end(),
1795+
"groups", List.of(group),
1796+
"next", (Fn0<Map>) () -> regexClosure(matcher)
1797+
);
1798+
} else {
1799+
return null;
1800+
}
1801+
}
17821802

17831803
/**
17841804
* Evaluate lambda against input data

src/main/java/com/dashjoin/jsonata/Parser.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,8 @@ Symbol processAST(Symbol expr) {
998998
rest.procedure.type.equals("path") &&
999999
rest.procedure.steps.size() == 1 &&
10001000
rest.procedure.steps.get(0).type.equals("name") &&
1001-
result.steps.get(result.steps.size() - 1).type.equals("function")) {
1001+
result.steps.get(result.steps.size() - 1).type.equals("function") &&
1002+
rest.procedure.steps.get(0).value instanceof Symbol) {
10021003
// next function in chain of functions - will override a thenable
10031004
result.steps.get(result.steps.size() - 1).nextFunction = (Symbol)rest.procedure.steps.get(0).value;
10041005
}

src/test/java/com/dashjoin/jsonata/RegexTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import org.junit.jupiter.api.Assertions;
44
import org.junit.jupiter.api.Test;
55

6+
import java.util.List;
7+
import java.util.Map;
8+
69
public class RegexTest {
710
@Test
811
public void testRegex() {
@@ -19,4 +22,36 @@ public void testEvalRegex() {
1922
String expected = "^test.*$";
2023
Assertions.assertEquals(expected, evaluate.toString());
2124
}
25+
26+
@Test
27+
public void testEvalRegexCheckAnswerData() {
28+
var expression = Jsonata.jsonata(
29+
"(\n" +
30+
" $matcher := $eval('/l/');\n" +
31+
" ('Hello World' ~> $matcher);\n" +
32+
")"
33+
);
34+
Map<String, Object> evaluate = (Map<String, Object>)(expression.evaluate(null));
35+
Assertions.assertEquals("l", evaluate.get("match"));
36+
Assertions.assertEquals(2, evaluate.get("start"));
37+
Assertions.assertEquals(3, evaluate.get("end"));
38+
Assertions.assertEquals(List.of("l"), evaluate.get("groups"));
39+
Assertions.assertInstanceOf(Jsonata.Fn0.class, evaluate.get("next"));
40+
}
41+
42+
@Test
43+
public void testEvalRegexCallNextAndCheckResult() {
44+
var expression = Jsonata.jsonata(
45+
"(\n" +
46+
" $matcher := $eval('/l/');\n" +
47+
" ('Hello World' ~> $matcher).next();\n" +
48+
")"
49+
);
50+
Map<String, Object> evaluate = (Map<String, Object>)(expression.evaluate(null));
51+
Assertions.assertEquals("l", evaluate.get("match"));
52+
Assertions.assertEquals(3, evaluate.get("start"));
53+
Assertions.assertEquals(4, evaluate.get("end"));
54+
Assertions.assertEquals(List.of("l"), evaluate.get("groups"));
55+
Assertions.assertInstanceOf(Jsonata.Fn0.class, evaluate.get("next"));
56+
}
2257
}

0 commit comments

Comments
 (0)