cstella-stripe commented on a change in pull request #1385: METRON-2071 Add
MAP_PUT and MAP_MERGE to Stellar
URL: https://github.com/apache/metron/pull/1385#discussion_r275782059
##########
File path:
metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/MapFunctions.java
##########
@@ -82,4 +83,67 @@ public Object apply(List<Object> objects) {
return ret;
}
}
+
+ @Stellar(name = "PUT",
+ namespace = "MAP",
+ description = "Adds a key/value pair to a map",
+ params = {
+ "key - The key",
+ "value - The value",
+ "map - The map to perform the put on"
+ },
+ returns = "The original map modified with the key/value. If the map
argument is null, a new map will be created and returned that contains the
provided key and value - note: if the 'map' argument is null, only the returned
map will be non-null and contain the key/value."
+ )
+ public static class MapPut extends BaseStellarFunction {
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Object apply(List<Object> objects) {
+ if (objects.size() < 3) {
+ throw new IllegalArgumentException("Must pass a key, value, and map");
+ } else {
+ Object keyObj = objects.get(0);
+ Object valueObj = objects.get(1);
+ Object mapObj = objects.get(2);
+ if (mapObj == null) {
+ mapObj = new HashMap<>();
+ }
+ Map<Object, Object> map = (Map) mapObj;
+ map.put(keyObj, valueObj);
+ return map;
+ }
+ }
+ }
+
+ @Stellar(name = "MERGE",
+ namespace = "MAP",
+ description = "Merges a list of maps",
+ params = {"maps - A collection of maps to merge"},
+ returns = "A Map. null if the list of maps is empty."
+ )
+ public static class MapMerge extends BaseStellarFunction {
+
+ @Override
+ @SuppressWarnings("unchecked")
+ public Object apply(List<Object> list) {
+ if (list.size() < 1) {
+ return null;
+ }
+ LinkedHashMap<Object, Object> ret = new LinkedHashMap<>();
+ Object o = list.get(0);
+ if (o != null) {
+ if (!(o instanceof Iterable)) {
+ throw new IllegalArgumentException("Expected an Iterable, but " + o
+ " is of type " + o.getClass());
+ }
+ Iterable<? extends Map> maps = (Iterable<? extends Map>) o;
Review comment:
Perhaps `if(Iterables.isEmpty(maps)) { return Iterables.getFirst(maps);)`?
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
With regards,
Apache Git Services