You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@karaf.apache.org by fp...@apache.org on 2022/03/08 16:39:24 UTC

[karaf-decanter] branch KARAF-7403 created (now b2c413c)

This is an automated email from the ASF dual-hosted git repository.

fpapon pushed a change to branch KARAF-7403
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git.


      at b2c413c  [KARAF-7403] Add a config param to use default key id in the split parser

This branch includes the following new commits:

     new b2c413c  [KARAF-7403] Add a config param to use default key id in the split parser

The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.


[karaf-decanter] 01/01: [KARAF-7403] Add a config param to use default key id in the split parser

Posted by fp...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

fpapon pushed a commit to branch KARAF-7403
in repository https://gitbox.apache.org/repos/asf/karaf-decanter.git

commit b2c413c53d16a16b835e69d7b8547e2cb3b4e408
Author: francois papon <fp...@apache.org>
AuthorDate: Tue Mar 8 17:38:40 2022 +0100

    [KARAF-7403] Add a config param to use default key id in the split parser
---
 .../src/main/asciidoc/user-guide/collectors.adoc   |  4 +-
 .../cfg/org.apache.karaf.decanter.parser.split.cfg |  4 +-
 .../karaf/decanter/parser/split/SplitParser.java   | 47 +++++++++++++---------
 .../decanter/parser/split/SplitParserTest.java     | 20 ++++++++-
 4 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/manual/src/main/asciidoc/user-guide/collectors.adoc b/manual/src/main/asciidoc/user-guide/collectors.adoc
index 1c69523..30c5ab5 100644
--- a/manual/src/main/asciidoc/user-guide/collectors.adoc
+++ b/manual/src/main/asciidoc/user-guide/collectors.adoc
@@ -185,6 +185,7 @@ For instance, you can have the following `etc/org.apache.karaf.decanter.parser.s
 ----
 separator=,
 keys=first,second,third,fourth
+useDefaultKey=false
 ----
 
 If the parser gets a line (collected by the file collector) like `this,is,a,test`, the line will be parsed as follows (the file collector will send the following data to the dispatcher):
@@ -196,7 +197,8 @@ third->a
 fourth->test
 ----
 
-If the `keys` configuration is not set, then `key-0`, `key-1`, etc will be used.
+If the `keys` configuration is not set and `useDefaultKey` is set to `true`, then `key-0`, `key-1`, etc will be used.
+When the `useDefaultKey` is set to `false` and the line doesn't match the `keys` configuration the data will not be sent.
 
 To use this parser in the file collector, you have to define it in the `parser.target` configuration (in `etc/org.apache.karaf.decanter.collector.file-XXXX.cfg`):
 
diff --git a/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg b/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
index 4796b77..8dd59cb 100644
--- a/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
+++ b/parser/split/src/main/cfg/org.apache.karaf.decanter.parser.split.cfg
@@ -22,4 +22,6 @@
 #
 separator=,
 
-# keys=key1,key2,key3
\ No newline at end of file
+# keys=key1,key2,key3
+
+useDefaultKey=false
\ No newline at end of file
diff --git a/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java b/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
index 6f2c9b5..9195c57 100644
--- a/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
+++ b/parser/split/src/main/java/org/apache/karaf/decanter/parser/split/SplitParser.java
@@ -38,6 +38,7 @@ public class SplitParser implements Parser {
 
     private String separator;
     private String keys = null;
+    private Boolean useDefaultKey;
 
     @Activate
     public void activate(ComponentContext componentContext) {
@@ -47,11 +48,13 @@ public class SplitParser implements Parser {
     public void activate(Dictionary<String, Object> config) {
         this.separator = (config.get("separator") != null) ? (String) config.get("separator") : ",";
         this.keys = (config.get("keys") != null) ? (String) config.get("keys") : null;
+        this.useDefaultKey = (config.get("useDefaultKey") != null) ? (Boolean) config.get("useDefaultKey") : false;
     }
 
     @Override
     public Map<String, Object> parse(String key, String line) {
         Map<String, Object> map = new HashMap<>();
+        boolean isByPassed = false;
         if (line != null) {
             String[] valuesArray = line.split(separator);
             String[] keysArray;
@@ -59,10 +62,15 @@ public class SplitParser implements Parser {
             if (this.keys != null) {
                 keysArray = this.keys.split(",");
                 if (keysArray.length != valuesArray.length) {
-                    LOGGER.warn("keys count and values count don't match, using default keys ID");
-                    keysArray = new String[valuesArray.length];
-                    for (int i = 0; i < valuesArray.length; i++) {
-                        keysArray[i] = "key-" + i;
+                    if (useDefaultKey) {
+                        LOGGER.warn("keys count and values count don't match, using default key ID");
+                        keysArray = new String[valuesArray.length];
+                        for (int i = 0; i < valuesArray.length; i++) {
+                            keysArray[i] = "key-" + i;
+                        }
+                    } else {
+                        LOGGER.warn("keys count and values count don't match, bypassing default key ID");
+                        isByPassed = true;
                     }
                 }
             } else {
@@ -71,22 +79,25 @@ public class SplitParser implements Parser {
                     keysArray[i] = "key-" + i;
                 }
             }
-            for (int i = 0; i < valuesArray.length; i++) {
-                try {
-                    map.put(keysArray[i], Integer.parseInt(valuesArray[i]));
-                    continue;
-                } catch (Exception e) {
-                    // nothing to do
-                }
 
-                try {
-                    map.put(keysArray[i], Long.parseLong(valuesArray[i]));
-                    continue;
-                } catch (Exception e) {
-                    // nothing to do
+            if (!isByPassed) {
+                for (int i = 0; i < valuesArray.length; i++) {
+                    try {
+                        map.put(keysArray[i], Integer.parseInt(valuesArray[i]));
+                        continue;
+                    } catch (Exception e) {
+                        // nothing to do
+                    }
+
+                    try {
+                        map.put(keysArray[i], Long.parseLong(valuesArray[i]));
+                        continue;
+                    } catch (Exception e) {
+                        // nothing to do
+                    }
+                    // if not integer and long value
+                    map.put(keysArray[i], valuesArray[i]);
                 }
-                // if not integer and long value
-                map.put(keysArray[i], valuesArray[i]);
             }
         }
         return map;
diff --git a/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java b/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
index c05149a..a2ad070 100644
--- a/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
+++ b/parser/split/src/test/java/org/apache/karaf/decanter/parser/split/SplitParserTest.java
@@ -59,11 +59,29 @@ public class SplitParserTest {
     }
 
     @Test
-    public void invalidKeys() throws Exception {
+    public void invalidKeysWithoutDefaultKey() throws Exception {
         SplitParser splitParser = new SplitParser();
         Dictionary<String, Object> config = new Hashtable<>();
         config.put("separator", "\\s+");
         config.put("keys", "this,is");
+        config.put("useDefaultKey", true);
+        splitParser.activate(config);
+
+        Map<String, Object> result = splitParser.parse("line", TEST_LINE);
+        Assert.assertEquals(4, result.size());
+        Assert.assertEquals("this", result.get("key-0"));
+        Assert.assertEquals("is", result.get("key-1"));
+        Assert.assertEquals("a", result.get("key-2"));
+        Assert.assertEquals("test", result.get("key-3"));
+    }
+
+    @Test
+    public void invalidKeysWithDefaultKey() throws Exception {
+        SplitParser splitParser = new SplitParser();
+        Dictionary<String, Object> config = new Hashtable<>();
+        config.put("separator", "\\s+");
+        config.put("keys", "this,is");
+        config.put("useDefaultKey", true);
         splitParser.activate(config);
 
         Map<String, Object> result = splitParser.parse("line", TEST_LINE);