mmiklavc 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_r275822862
##########
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:
Hey @cestella, thanks for the review! I'm not sure I completely follow -
`getFirst` also requires a default. I initialize the return value on line 132,
but it's outside of the `if` block. Maybe you meant `if (Iterables.size(maps)
== 1){ return Iterables.getFirst(maps);)`?
Side note, I had initially wanted to use `Iterables.addAll()` for
consistency , however this only accepts `Collections`, which the `Map`
interface does not extend.
https://github.com/apache/metron/blob/master/metron-stellar/stellar-common/src/main/java/org/apache/metron/stellar/dsl/functions/SetFunctions.java#L137
----------------------------------------------------------------
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