Skip to content

Commit c205cbe

Browse files
retooanthonycorbacho
authored andcommitted
Grok Compiler: wrap grok patterns in (?:...) in namedOnly mode (#70)
This ensures that the namedOnly behaves similar like the normal mode. i.E. WORD foo|bar TEXT %{WORD}+ => should render to (?:foo|bar)+ and NOT foo|bar+
1 parent b9d2f68 commit c205cbe

File tree

2 files changed

+65
-7
lines changed

2 files changed

+65
-7
lines changed

src/main/java/io/thekraken/grok/api/Grok.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ public void compile(String pattern, boolean namedOnly) throws GrokException {
390390
for (int i = 0; i < count; i++) {
391391
String replacement = String.format("(?<name%d>%s)", index, grokPatternDefinition.get(group.get("pattern")));
392392
if (namedOnly && group.get("subname") == null) {
393-
replacement = grokPatternDefinition.get(group.get("pattern"));
393+
replacement = String.format("(?:%s)", grokPatternDefinition.get(group.get("pattern")));
394394
}
395395
namedRegexCollection.put("name" + index,
396396
(group.get("subname") != null ? group.get("subname") : group.get("name")));

src/test/java/io/thekraken/grok/api/GrokTest.java

Lines changed: 64 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
package io.thekraken.grok.api;
22

3-
import io.thekraken.grok.api.exception.GrokException;
4-
import org.junit.FixMethodOrder;
5-
import org.junit.Test;
6-
import org.junit.runners.MethodSorters;
3+
import static java.lang.String.format;
4+
import static org.junit.Assert.assertEquals;
5+
import static org.junit.Assert.assertNotEquals;
6+
import static org.junit.Assert.assertNotNull;
7+
import static org.junit.Assert.assertTrue;
78

89
import java.io.BufferedReader;
910
import java.io.FileReader;
10-
import java.util.*;
11+
import java.util.ArrayList;
12+
import java.util.Arrays;
13+
import java.util.Collections;
14+
import java.util.HashMap;
15+
import java.util.List;
1116

12-
import static org.junit.Assert.*;
17+
import org.junit.FixMethodOrder;
18+
import org.junit.Test;
19+
import org.junit.runners.MethodSorters;
20+
21+
import io.thekraken.grok.api.exception.GrokException;
1322

1423

1524
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
@@ -522,6 +531,55 @@ public void test017_nonMachingList() throws GrokException {
522531
assertEquals(i, 4);
523532
}
524533

534+
@Test
535+
public void test018_namedOnlySimpleCase() throws GrokException {
536+
Grok grok = Grok.create(ResourceManager.PATTERNS);
537+
538+
grok.addPattern("WORD", "foo|bar");
539+
grok.addPattern("TEXT", "<< %{WORD}+ >>");
540+
541+
grok.compile("%{TEXT:text}", true);
542+
543+
String text = "<< barfoobarfoo >>";
544+
Match match = grok.match(text);
545+
match.captures();
546+
assertEquals("unable to parse: " + text,
547+
text,
548+
match.toMap().get("text"));
549+
}
550+
551+
@Test
552+
public void test019_namedOnlyAllCases() throws GrokException {
553+
/* like previous test, but systematic all four possible options */
554+
testPatternRepetitions(true, "(?:foo|bar)");
555+
testPatternRepetitions(true, "foo|bar");
556+
testPatternRepetitions(false, "(?:foo|bar)");
557+
testPatternRepetitions(false, "foo|bar");
558+
}
559+
560+
private void testPatternRepetitions(boolean namedOnly, String pattern) throws GrokException {
561+
String description = format("[readonly:%s pattern:%s] ", namedOnly, pattern);;
562+
563+
Grok grok = Grok.create(ResourceManager.PATTERNS);
564+
565+
grok.addPattern("WORD", pattern);
566+
grok.addPattern("TEXT", "<< %{WORD}+ >>");
567+
568+
grok.compile("%{TEXT:text}", namedOnly);
569+
assertMatches(description, grok, "<< foo >>");
570+
assertMatches(description, grok, "<< foobar >>");
571+
assertMatches(description, grok, "<< foofoobarbar >>");
572+
assertMatches(description, grok, "<< barfoobarfoo >>");
573+
}
574+
575+
private void assertMatches(String description, Grok grok, String text) {
576+
Match match = grok.match(text);
577+
match.captures();
578+
assertEquals(format("%s: unable to parse '%s'", description, text),
579+
text,
580+
match.toMap().get("text"));
581+
}
582+
525583
/* see: https://github.com/thekrakken/java-grok/issues/64 */
526584
@Test
527585
public void testDisablingAutomaticConversion() throws GrokException {

0 commit comments

Comments
 (0)