Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ enum StandardFunction {
Overload.Arithmetic.ADD_STRING,
Overload.Arithmetic.ADD_BYTES,
Overload.Arithmetic.ADD_LIST,
Overload.Arithmetic.MAP_INSERT_KEY_VALUE,
Overload.Arithmetic.MAP_INSERT_MAP,
Overload.Arithmetic.ADD_TIMESTAMP_DURATION,
Overload.Arithmetic.ADD_DURATION_TIMESTAMP,
Overload.Arithmetic.ADD_DURATION_DURATION),
Expand Down Expand Up @@ -410,6 +412,12 @@ public enum Arithmetic implements StandardOverload {
ADD_LIST(
CelOverloadDecl.newGlobalOverload(
"add_list", "list concatenation", LIST_OF_A, LIST_OF_A, LIST_OF_A)),
MAP_INSERT_KEY_VALUE(
CelOverloadDecl.newGlobalOverload(
"mapInsert_map_key_value", "map insertion", MAP_OF_AB, MAP_OF_AB, TYPE_PARAM_A, TYPE_PARAM_B)),
MAP_INSERT_MAP(
CelOverloadDecl.newGlobalOverload(
"mapInsert_map_map", "map insertion", MAP_OF_AB, MAP_OF_AB, MAP_OF_AB)),
ADD_TIMESTAMP_DURATION(
CelOverloadDecl.newGlobalOverload(
"add_timestamp_duration",
Expand Down
13 changes: 13 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/CelStandardFunctions.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import static com.google.common.base.Preconditions.checkNotNull;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.primitives.Ints;
import com.google.common.primitives.UnsignedLong;
Expand Down Expand Up @@ -83,6 +84,8 @@ public enum StandardFunction {
Arithmetic.ADD_STRING,
Arithmetic.ADD_BYTES,
Arithmetic.ADD_LIST,
Arithmetic.MAP_INSERT_MAP_MAP,
Arithmetic.MAP_INSERT_MAP_KEY_VALUE,
Arithmetic.ADD_TIMESTAMP_DURATION,
Arithmetic.ADD_DURATION_TIMESTAMP,
Arithmetic.ADD_DURATION_DURATION),
Expand Down Expand Up @@ -373,6 +376,16 @@ public enum Arithmetic implements StandardOverload {
(bindingHelper) ->
CelFunctionBinding.from(
"add_list", List.class, List.class, RuntimeHelpers::concat)),
MAP_INSERT_MAP_MAP(
(bindingHelper) ->
CelFunctionBinding.from(
"mapInsert_map_map", Map.class, Map.class, RuntimeHelpers::mapInsert)),
MAP_INSERT_MAP_KEY_VALUE(
(bindingHelper) ->
CelFunctionBinding.from(
"mapInsert_map_key_value",
ImmutableList.of(Map.class, Object.class, Object.class),
RuntimeHelpers::mapInsert)),
SUBTRACT_INT64(
(bindingHelper) ->
CelFunctionBinding.from(
Expand Down
27 changes: 27 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/RuntimeHelpers.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@
import dev.cel.common.internal.Converter;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.threeten.extra.AmountFormats;

Expand Down Expand Up @@ -96,6 +98,31 @@ static <E> List<E> concat(List<E> first, List<E> second) {
return result;
}

/** Concatenates two maps into a new map */
static Map<Object, Object> mapInsert(Map<Object, Object> first, Map<Object, Object> second) {
// TODO: return a mutable map instead of an actual copy.
Map<Object, Object> result = new HashMap<>(first.size() + second.size());
result.putAll(first);
result.putAll(second);
return result;
}

/** Add new key value pair to an existing map. */
static Map<Object, Object> mapInsert(Object[] args) {
Map<?, ?> map = (Map<?, ?>) args[0];
Object key = args[1];
Object value = args[2];
// TODO: return a mutable map instead of an actual copy.
if (map.containsKey(key)) {
throw new IllegalArgumentException(
String.format("insert failed: key %s already exists", key));
}
Map<Object, Object> result = new HashMap<>(map.size() + 1);
result.putAll(map);
result.put(key, value);
return result;
}

// Collections
// ===========

Expand Down
Loading