Skip to content
Open
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 @@ -33,7 +33,7 @@
@SuppressWarnings("unchecked") // Unchecked cast of generics due to type-erasure (ex: MapValue).
@Internal
@Immutable
abstract class CelValueConverter {
public abstract class CelValueConverter {

/** Adapts a {@link CelValue} to a plain old Java Object. */
public Object unwrap(CelValue celValue) {
Expand Down
8 changes: 8 additions & 0 deletions runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -255,3 +255,11 @@ java_library(
visibility = ["//:internal"],
exports = ["//runtime/src/main/java/dev/cel/runtime:metadata"],
)

java_library(
name = "concatenated_list_view",
visibility = ["//:internal"],
exports = [
"//runtime/src/main/java/dev/cel/runtime:concatenated_list_view",
],
)
4 changes: 3 additions & 1 deletion runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1142,7 +1142,9 @@ java_library(
name = "concatenated_list_view",
srcs = ["ConcatenatedListView.java"],
# used_by_android
visibility = ["//visibility:private"],
tags = [
],
deps = ["//common/annotations"],
)

java_library(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,16 @@ public static CelEvaluationExceptionBuilder newBuilder(String message, Object...
*/
@Internal
public static CelEvaluationExceptionBuilder newBuilder(CelRuntimeException celRuntimeException) {
// TODO: Temporary until migration is complete.
Throwable cause = celRuntimeException.getCause();
return new CelEvaluationExceptionBuilder(cause.getMessage())
.setCause(cause)
.setErrorCode(celRuntimeException.getErrorCode());
String message =
cause == null
? celRuntimeException.getMessage()
: celRuntimeException.getCause().getMessage();

return new CelEvaluationExceptionBuilder(message)
.setErrorCode(celRuntimeException.getErrorCode())
.setCause(cause);
}

private CelEvaluationExceptionBuilder(String message) {
Expand Down
11 changes: 8 additions & 3 deletions runtime/src/main/java/dev/cel/runtime/ConcatenatedListView.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License aj
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
Expand All @@ -14,6 +14,7 @@

package dev.cel.runtime;

import dev.cel.common.annotations.Internal;
import java.util.AbstractList;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -27,16 +28,20 @@
* comprehensions that dispatch `add_list` to concat N lists together).
*
* <p>This does not support any of the standard list operations from {@link java.util.List}.
*

* <p>CEL Library Internals. Do Not Use.
*/
final class ConcatenatedListView<E> extends AbstractList<E> {
@Internal
public final class ConcatenatedListView<E> extends AbstractList<E> {
private final List<List<? extends E>> sourceLists;
private int totalSize = 0;

ConcatenatedListView() {
this.sourceLists = new ArrayList<>();
}

ConcatenatedListView(Collection<? extends E> collection) {
public ConcatenatedListView(Collection<? extends E> collection) {
this();
addAll(collection);
}
Expand Down
55 changes: 2 additions & 53 deletions runtime/src/main/java/dev/cel/runtime/planner/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,64 +14,13 @@

package dev.cel.runtime.planner;


import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.common.types.TypeType;
import dev.cel.runtime.GlobalResolver;

/** Represents a resolvable symbol or path (such as a variable or a field selection). */
@Immutable
interface Attribute {
Object resolve(GlobalResolver ctx);

final class MaybeAttribute implements Attribute {
private final ImmutableList<Attribute> attributes;

@Override
public Object resolve(GlobalResolver ctx) {
for (Attribute attr : attributes) {
Object value = attr.resolve(ctx);
if (value != null) {
return value;
}
}

// TODO: Handle unknowns
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
}

MaybeAttribute(ImmutableList<Attribute> attributes) {
this.attributes = attributes;
}
}

final class NamespacedAttribute implements Attribute {
private final ImmutableList<String> namespacedNames;
private final CelTypeProvider typeProvider;

@Override
public Object resolve(GlobalResolver ctx) {
for (String name : namespacedNames) {
Object value = ctx.resolve(name);
if (value != null) {
// TODO: apply qualifiers
return value;
}

TypeType type = typeProvider.findType(name).map(TypeType::create).orElse(null);
if (type != null) {
return type;
}
}

// TODO: Handle unknowns
throw new UnsupportedOperationException("Unknown attributes is not supported yet");
}

NamespacedAttribute(CelTypeProvider typeProvider, ImmutableList<String> namespacedNames) {
this.typeProvider = typeProvider;
this.namespacedNames = namespacedNames;
}
}
Attribute addQualifier(Qualifier qualifier);
}
33 changes: 22 additions & 11 deletions runtime/src/main/java/dev/cel/runtime/planner/AttributeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,35 +15,46 @@
package dev.cel.runtime.planner;

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.CelContainer;
import dev.cel.common.types.CelTypeProvider;
import dev.cel.runtime.planner.Attribute.MaybeAttribute;
import dev.cel.runtime.planner.Attribute.NamespacedAttribute;
import dev.cel.common.values.CelValueConverter;

@Immutable
final class AttributeFactory {

private final CelContainer unusedContainer;
private final CelContainer container;
private final CelTypeProvider typeProvider;
private final CelValueConverter celValueConverter;

NamespacedAttribute newAbsoluteAttribute(String... names) {
return new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names));
return new NamespacedAttribute(typeProvider, celValueConverter, ImmutableSet.copyOf(names));
}

MaybeAttribute newMaybeAttribute(String... names) {
// TODO: Resolve container names
RelativeAttribute newRelativeAttribute(PlannedInterpretable operand) {
return new RelativeAttribute(operand, celValueConverter);
}

MaybeAttribute newMaybeAttribute(String name) {
return new MaybeAttribute(
ImmutableList.of(new NamespacedAttribute(typeProvider, ImmutableList.copyOf(names))));
this,
ImmutableList.of(
new NamespacedAttribute(
typeProvider, celValueConverter, container.resolveCandidateNames(name))));
}

static AttributeFactory newAttributeFactory(
CelContainer celContainer, CelTypeProvider typeProvider) {
return new AttributeFactory(celContainer, typeProvider);
CelContainer celContainer,
CelTypeProvider typeProvider,
CelValueConverter celValueConverter) {
return new AttributeFactory(celContainer, typeProvider, celValueConverter);
}

private AttributeFactory(CelContainer container, CelTypeProvider typeProvider) {
this.unusedContainer = container;
private AttributeFactory(
CelContainer container, CelTypeProvider typeProvider, CelValueConverter celValueConverter) {
this.container = container;
this.typeProvider = typeProvider;
this.celValueConverter = celValueConverter;
}
}
89 changes: 87 additions & 2 deletions runtime/src/main/java/dev/cel/runtime/planner/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,17 @@ java_library(
":eval_create_list",
":eval_create_map",
":eval_create_struct",
":eval_fold",
":eval_or",
":eval_test_only",
":eval_unary",
":eval_var_args_call",
":eval_zero_arity",
":interpretable_attribute",
":planned_interpretable",
":planned_program",
":qualifier",
":string_qualifier",
"//:auto_value",
"//common:cel_ast",
"//common:container",
Expand All @@ -36,11 +41,11 @@ java_library(
"//common/ast",
"//common/types",
"//common/types:type_providers",
"//common/values",
"//common/values:cel_value_provider",
"//runtime:dispatcher",
"//runtime:evaluation_exception",
"//runtime:evaluation_exception_builder",
"//runtime:interpretable",
"//runtime:program",
"//runtime:resolved_overload",
"@maven//:com_google_code_findbugs_annotations",
Expand Down Expand Up @@ -84,28 +89,77 @@ java_library(
],
)

java_library(
name = "interpretable_attribute",
srcs = ["InterpretableAttribute.java"],
deps = [
":planned_interpretable",
":qualifier",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "attribute",
srcs = [
"Attribute.java",
"AttributeFactory.java",
"MaybeAttribute.java",
"MissingAttribute.java",
"NamespacedAttribute.java",
"RelativeAttribute.java",
],
deps = [
":eval_helpers",
":planned_interpretable",
":qualifier",
"//common:container",
"//common/exceptions:attribute_not_found",
"//common/types",
"//common/types:type_providers",
"//common/values",
"//common/values:cel_value",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "qualifier",
srcs = ["Qualifier.java"],
deps = [
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "presence_test_qualifier",
srcs = ["PresenceTestQualifier.java"],
deps = [
":attribute",
":qualifier",
"//common/values",
],
)

java_library(
name = "string_qualifier",
srcs = ["StringQualifier.java"],
deps = [
":qualifier",
"//common/exceptions:attribute_not_found",
"//common/values",
],
)

java_library(
name = "eval_attribute",
srcs = ["EvalAttribute.java"],
deps = [
":attribute",
":planned_interpretable",
":interpretable_attribute",
":qualifier",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
Expand All @@ -114,6 +168,21 @@ java_library(
],
)

java_library(
name = "eval_test_only",
srcs = ["EvalTestOnly.java"],
deps = [
":interpretable_attribute",
":presence_test_qualifier",
":qualifier",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
],
)

java_library(
name = "eval_zero_arity",
srcs = ["EvalZeroArity.java"],
Expand Down Expand Up @@ -241,6 +310,22 @@ java_library(
],
)

java_library(
name = "eval_fold",
srcs = ["EvalFold.java"],
deps = [
":planned_interpretable",
"//runtime:concatenated_list_view",
"//runtime:evaluation_exception",
"//runtime:evaluation_listener",
"//runtime:function_resolver",
"//runtime:interpretable",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:org_jspecify_jspecify",
],
)

java_library(
name = "eval_helpers",
srcs = ["EvalHelpers.java"],
Expand Down
Loading