Skip to content

Commit b9d2f68

Browse files
retooanthonycorbacho
authored andcommitted
Grok/Match: provide option to completely disable automatic conversion (#71)
1 parent 1caca05 commit b9d2f68

File tree

3 files changed

+46
-12
lines changed

3 files changed

+46
-12
lines changed

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,12 @@ public class Grok implements Serializable {
8686
/** only use in grok discovery. */
8787
private String savedPattern;
8888

89+
90+
/**
91+
* automatic conversion of values
92+
*/
93+
private boolean automaticConversionEnabled = true;
94+
8995
/**
9096
* Create Empty {@code Grok}.
9197
*/
@@ -260,7 +266,17 @@ public void addPatternFromReader(Reader r) throws GrokException {
260266
} catch (GrokException e) {
261267
throw new GrokException(e.getMessage());
262268
}
269+
}
270+
271+
/**
272+
* Disable automatic conversion of values
273+
*/
274+
public void disableAutomaticConversion() {
275+
this.automaticConversionEnabled = false;
276+
}
263277

278+
public boolean isAutomaticConversionEnabled() {
279+
return automaticConversionEnabled;
264280
}
265281

266282
/**

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

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ private void captures(boolean flattened ) {
175175
return;
176176
}
177177
capture.clear();
178+
boolean automaticConversionEnabled = grok.isAutomaticConversionEnabled();
179+
178180

179181
// _capture.put("LINE", this.line);
180182
// _capture.put("LENGTH", this.line.length() +"");
@@ -195,21 +197,24 @@ private void captures(boolean flattened ) {
195197
if (pairs.getValue() != null) {
196198
value = pairs.getValue().toString();
197199

198-
KeyValue keyValue = Converter.convert(key, value);
199200

200-
// get validated key
201-
key = keyValue.getKey();
201+
if (automaticConversionEnabled) {
202+
KeyValue keyValue = Converter.convert(key, value);
202203

203-
// resolve value
204-
if (keyValue.getValue() instanceof String) {
205-
value = cleanString((String) keyValue.getValue());
206-
} else {
207-
value = keyValue.getValue();
208-
}
204+
// get validated key
205+
key = keyValue.getKey();
209206

210-
// set if grok failure
211-
if (keyValue.hasGrokFailure()) {
212-
capture.put(key + "_grokfailure", keyValue.getGrokFailure());
207+
// resolve value
208+
if (keyValue.getValue() instanceof String) {
209+
value = cleanString((String) keyValue.getValue());
210+
} else {
211+
value = keyValue.getValue();
212+
}
213+
214+
// set if grok failure
215+
if (keyValue.hasGrokFailure()) {
216+
capture.put(key + "_grokfailure", keyValue.getGrokFailure());
217+
}
213218
}
214219
}
215220

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,4 +522,17 @@ public void test017_nonMachingList() throws GrokException {
522522
assertEquals(i, 4);
523523
}
524524

525+
/* see: https://github.com/thekrakken/java-grok/issues/64 */
526+
@Test
527+
public void testDisablingAutomaticConversion() throws GrokException {
528+
String input = "client id: \"foo\" \"bar\"";
529+
String pattern = "(?<message>client id): (?<clientid>.*)";
530+
531+
Grok grok = new Grok();
532+
grok.disableAutomaticConversion();
533+
grok.compile(pattern, false);
534+
Match gm = grok.match(input);
535+
gm.captures();
536+
assertEquals("\"foo\" \"bar\"", gm.toMap().get("clientid"));
537+
}
525538
}

0 commit comments

Comments
 (0)