Skip to content
Merged
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 @@ -52,7 +52,8 @@ public final class CelComprehensionsExtensions
private static final TypeParamType TYPE_PARAM_V = TypeParamType.create("V");
private static final MapType MAP_KV_TYPE = MapType.create(TYPE_PARAM_K, TYPE_PARAM_V);

enum Function {
/** Enumeration of functions for Comprehensions extension. */
public enum Function {
MAP_INSERT(
CelFunctionDecl.newFunctionDeclaration(
MAP_INSERT_FUNCTION,
Expand All @@ -72,6 +73,10 @@ enum Function {

private final CelFunctionDecl functionDecl;

public CelFunctionDecl functionDecl() {
return functionDecl;
}

String getFunction() {
return functionDecl.name();
}
Expand All @@ -81,20 +86,25 @@ String getFunction() {
}
}

private static final CelExtensionLibrary<CelComprehensionsExtensions> LIBRARY =
new CelExtensionLibrary<CelComprehensionsExtensions>() {
private final CelComprehensionsExtensions version0 = new CelComprehensionsExtensions();
private static final class Library implements CelExtensionLibrary<CelComprehensionsExtensions> {
private final CelComprehensionsExtensions version0;

@Override
public String name() {
return "comprehensions";
}
Library() {
version0 = new CelComprehensionsExtensions();
}

@Override
public ImmutableSet<CelComprehensionsExtensions> versions() {
return ImmutableSet.of(version0);
}
};
@Override
public String name() {
return "comprehensions";
}

@Override
public ImmutableSet<CelComprehensionsExtensions> versions() {
return ImmutableSet.of(version0);
}
}

private static final Library LIBRARY = new Library();

static CelExtensionLibrary<CelComprehensionsExtensions> library() {
return LIBRARY;
Expand All @@ -103,7 +113,7 @@ static CelExtensionLibrary<CelComprehensionsExtensions> library() {
private final ImmutableSet<Function> functions;

CelComprehensionsExtensions() {
this.functions = ImmutableSet.copyOf(Function.values());
this.functions = ImmutableSet.of(Function.MAP_INSERT);
}

@Override
Expand Down Expand Up @@ -175,10 +185,10 @@ public void setParserOptions(CelParserBuilder parserBuilder) {
private static Map<Object, Object> mapInsertMap(
Map<?, ?> targetMap, Map<?, ?> mapToMerge, RuntimeEquality equality) {
for (Object key : mapToMerge.keySet()) {
if (equality.findInMap(targetMap, key).isPresent()) {
throw new IllegalArgumentException(
String.format("insert failed: key '%s' already exists", key));
}
checkArgument(
!equality.findInMap(targetMap, key).isPresent(),
"insert failed: key '%s' already exists",
key);
}

if (targetMap instanceof MutableMapValue) {
Expand All @@ -198,10 +208,10 @@ private static Map<Object, Object> mapInsertKeyValue(Object[] args, RuntimeEqual
Object key = args[1];
Object value = args[2];

if (equality.findInMap(mapArg, key).isPresent()) {
throw new IllegalArgumentException(
String.format("insert failed: key '%s' already exists", key));
}
checkArgument(
!equality.findInMap(mapArg, key).isPresent(),
"insert failed: key '%s' already exists",
key);

if (mapArg instanceof MutableMapValue) {
MutableMapValue mutableMap = (MutableMapValue) mapArg;
Expand Down
Loading