metron-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ceste...@apache.org
Subject [05/17] incubator-metron git commit: METRON-687 Create String Formatting Function for Stellar (nickwallen) closes apache/incubator-metron#434
Date Mon, 06 Feb 2017 20:17:33 GMT
METRON-687 Create String Formatting Function for Stellar (nickwallen) closes apache/incubator-metron#434


Project: http://git-wip-us.apache.org/repos/asf/incubator-metron/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-metron/commit/fd77ec32
Tree: http://git-wip-us.apache.org/repos/asf/incubator-metron/tree/fd77ec32
Diff: http://git-wip-us.apache.org/repos/asf/incubator-metron/diff/fd77ec32

Branch: refs/heads/Metron_0.3.1
Commit: fd77ec329e96490e8401e300ac4104bf3f4a9901
Parents: 75d122d
Author: nickwallen <nick@nickallen.org>
Authored: Fri Feb 3 10:27:45 2017 -0500
Committer: Nick Allen <nick@nickallen.org>
Committed: Fri Feb 3 10:27:45 2017 -0500

----------------------------------------------------------------------
 metron-platform/metron-common/README.md         |  8 +++++
 .../common/dsl/functions/StringFunctions.java   | 21 ++++++++++++
 .../dsl/functions/StringFunctionsTest.java      | 35 ++++++++++++++++++++
 3 files changed, 64 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/fd77ec32/metron-platform/metron-common/README.md
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/README.md b/metron-platform/metron-common/README.md
index ace1a0c..c24ae73 100644
--- a/metron-platform/metron-common/README.md
+++ b/metron-platform/metron-common/README.md
@@ -96,6 +96,7 @@ The `!=` operator is the negation of the above.
 | [ `ENRICHMENT_GET`](#enrichment_get)                                                  
            |
 | [ `FILL_LEFT`](#fill_left)                                                            
            |
 | [ `FILL_RIGHT`](#fill_right)                                                          
            |
+| [ `FORMAT`](#format)                                                                  
    |
 | [ `HLLP_CARDINALITY`](../../metron-analytics/metron-statistics#hllp_cardinality)      
            |
 | [ `HLLP_INIT`](../../metron-analytics/metron-statistics#hllp_init)                    
            |
 | [ `HLLP_MERGE`](../../metron-analytics/metron-statistics#hllp_merge)                  
            |
@@ -270,6 +271,13 @@ The `!=` operator is the negation of the above.
     * len - the required length
   * Returns: Last element of the list
 
+### `FORMAT`
+  * Description: Returns a formatted string using the specified format string and arguments.
Uses Java's string formatting conventions.
+  * Input:
+    * format - string
+    * arguments... - object(s)
+  * Returns: A formatted string.
+
 ### `GEO_GET`
   * Description: Look up an IPV4 address and returns geographic information about it
   * Input:

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/fd77ec32/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
index fa2b55b..1086da3 100644
--- a/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
+++ b/metron-platform/metron-common/src/main/java/org/apache/metron/common/dsl/functions/StringFunctions.java
@@ -322,4 +322,25 @@ public class StringFunctions {
       return ret;
     }
   }
+
+  @Stellar( name="FORMAT"
+          , description = "Returns a formatted string using the specified format string and
arguments. Uses Java's string formatting conventions."
+          , params = { "format - string", "arguments... - object(s)" }
+          , returns = "A formatted string."
+  )
+  public static class Format extends BaseStellarFunction {
+
+    @Override
+    public Object apply(List<Object> args) {
+
+      if(args.size() == 0) {
+        throw new IllegalArgumentException("[FORMAT] missing argument: format string");
+      }
+
+      String format = ConversionUtils.convert(args.get(0), String.class);
+      Object[] formatArgs = args.subList(1, args.size()).toArray();
+
+      return String.format(format, formatArgs);
+    }
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-metron/blob/fd77ec32/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
----------------------------------------------------------------------
diff --git a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
index 18d2eb2..d8854f8 100644
--- a/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
+++ b/metron-platform/metron-common/src/test/java/org/apache/metron/common/dsl/functions/StringFunctionsTest.java
@@ -21,10 +21,13 @@ package org.apache.metron.common.dsl.functions;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.ImmutableMap;
 import org.apache.commons.collections.map.HashedMap;
+import org.apache.commons.collections.map.SingletonMap;
 import org.apache.metron.common.dsl.ParseException;
 import org.junit.Assert;
 import org.junit.Test;
 
+import java.util.Calendar;
+import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -172,4 +175,36 @@ public class StringFunctionsTest {
      */
     Assert.assertEquals(1.5, (Double)run("STRING_ENTROPY(foo)", ImmutableMap.of("foo", "aaaaaaaaaabbbbbccccc")),
0.0);
   }
+
+  @Test
+  public void testFormat() throws Exception {
+
+    Map<String, Object> vars = ImmutableMap.of(
+            "cal", new Calendar.Builder().setDate(2017, 02, 02).build(),
+            "x", 234,
+            "y", 3);
+
+    Assert.assertEquals("no args",        run("FORMAT('no args')", vars));
+    Assert.assertEquals("234.0",          run("FORMAT('%.1f', TO_DOUBLE(234))", vars));
+    Assert.assertEquals("000234",         run("FORMAT('%06d', 234)", vars));
+    Assert.assertEquals("03 2,2017",      run("FORMAT('%1$tm %1$te,%1$tY', cal)", vars));
+    Assert.assertEquals("234 > 3",        run("FORMAT('%d > %d', x, y)", vars));
+    Assert.assertEquals("missing: null",  run("FORMAT('missing: %d', missing)", vars));
+  }
+
+  /**
+   * FORMAT - Not passing a format string will throw an exception
+   */
+  @Test(expected = ParseException.class)
+  public void testFormatWithNoArguments() throws Exception {
+    run("FORMAT()", Collections.emptyMap());
+  }
+
+  /**
+   * FORMAT - Forgetting to pass an argument required by the format string will throw an
exception.
+   */
+  @Test(expected = ParseException.class)
+  public void testFormatWithMissingArguments() throws Exception {
+    run("FORMAT('missing arg: %d')", Collections.emptyMap());
+  }
 }


Mime
View raw message