Skip to content

Commit

Permalink
Add dev.cel.runtime.CelFunctionBinding
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 725808692
  • Loading branch information
l46kok authored and copybara-github committed Feb 14, 2025
1 parent a0c2012 commit 17278d9
Show file tree
Hide file tree
Showing 6 changed files with 201 additions and 22 deletions.
16 changes: 16 additions & 0 deletions runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,22 @@ java_library(
],
)

java_library(
name = "function_binding",
visibility = ["//visibility:public"],
exports = [
"//runtime/src/main/java/dev/cel/runtime:function_binding",
],
)

java_library(
name = "function_overload",
visibility = ["//visibility:public"],
exports = [
"//runtime/src/main/java/dev/cel/runtime:function_overload",
],
)

java_library(
name = "evaluation_exception_builder",
exports = ["//runtime/src/main/java/dev/cel/runtime:evaluation_exception_builder"],
Expand Down
46 changes: 39 additions & 7 deletions runtime/src/main/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@ package(

BASE_SOURCES = [
"DefaultMetadata.java",
"FunctionResolver.java",
"FunctionOverload.java",
"MessageProvider.java",
"Registrar.java",
"ResolvedOverload.java",
]

# keep sorted
Expand Down Expand Up @@ -70,14 +67,12 @@ java_library(
tags = [
],
deps = [
":evaluation_exception",
":metadata",
"//common:cel_ast",
"//common/annotations",
"@maven//:com_google_code_findbugs_annotations",
"//runtime:function_overload",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

Expand Down Expand Up @@ -111,6 +106,7 @@ java_library(
"//common/types",
"//common/types:cel_types",
"//common/types:type_providers",
"//runtime:function_overload",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down Expand Up @@ -196,7 +192,6 @@ java_library(

# keep sorted
RUNTIME_SOURCES = [
"CelFunctionOverload.java",
"CelFunctionResolver.java",
"CelLateFunctionBindings.java",
"CelResolvedOverload.java",
Expand Down Expand Up @@ -252,6 +247,41 @@ java_library(
],
)

java_library(
name = "function_binding",
srcs = [
"CelFunctionBinding.java",
"FunctionBindingImpl.java",
],
tags = [
],
deps = [
":function_overload",
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "function_overload",
srcs = [
"CelFunctionOverload.java",
"FunctionOverload.java",
"FunctionResolver.java",
"ResolvedOverload.java",
],
tags = [
],
deps = [
":evaluation_exception",
"//common/annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
"@maven//:com_google_protobuf_protobuf_java",
],
)

java_library(
name = "runtime",
srcs = RUNTIME_SOURCES,
Expand Down Expand Up @@ -282,6 +312,8 @@ java_library(
"//common/types:cel_types",
"//common/values:cel_value_provider",
"//common/values:proto_message_value_provider",
"//runtime:function_binding",
"//runtime:function_overload",
"@maven//:com_google_code_findbugs_annotations",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
Expand Down
71 changes: 71 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/CelFunctionBinding.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2025 Google LLC
//
// 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 at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;
import dev.cel.common.annotations.Internal;

/**
* Binding consisting of an overload id, a Java-native argument signature, and an overload
* definition.
*
* <p>While the CEL function has a human-readable {@code camelCase} name, overload ids should use
* the following convention where all {@code <type>} names should be ASCII lower-cased. For types
* prefer the unparameterized simple name of time, or unqualified name of any proto-based type:
*
* <ul>
* <li>unary member function: <type>_<function>
* <li>binary member function: <type>_<function>_<arg_type>
* <li>unary global function: <function>_<type>
* <li>binary global function: <function>_<arg_type1>_<arg_type2>
* <li>global function: <function>_<arg_type1>_<arg_type2>_<arg_typeN>
* </ul>
*
* <p>Examples: string_startsWith_string, mathMax_list, lessThan_money_money
*/
@Internal
@Immutable
public interface CelFunctionBinding {
String getOverloadId();

ImmutableList<Class<?>> getArgTypes();

CelFunctionOverload getDefinition();

/** Create a unary function binding from the {@code overloadId}, {@code arg}, and {@code impl}. */
@SuppressWarnings("unchecked")
static <T> CelFunctionBinding from(
String overloadId, Class<T> arg, CelFunctionOverload.Unary<T> impl) {
return from(overloadId, ImmutableList.of(arg), (args) -> impl.apply((T) args[0]));
}

/**
* Create a binary function binding from the {@code overloadId}, {@code arg1}, {@code arg2}, and
* {@code impl}.
*/
@SuppressWarnings("unchecked")
static <T1, T2> CelFunctionBinding from(
String overloadId, Class<T1> arg1, Class<T2> arg2, CelFunctionOverload.Binary<T1, T2> impl) {
return from(
overloadId, ImmutableList.of(arg1, arg2), (args) -> impl.apply((T1) args[0], (T2) args[1]));
}

/** Create a function binding from the {@code overloadId}, {@code argTypes}, and {@code impl}. */
static CelFunctionBinding from(
String overloadId, Iterable<Class<?>> argTypes, CelFunctionOverload impl) {
return new FunctionBindingImpl(overloadId, ImmutableList.copyOf(argTypes), impl);
}
}
39 changes: 24 additions & 15 deletions runtime/src/main/java/dev/cel/runtime/CelRuntime.java
Original file line number Diff line number Diff line change
Expand Up @@ -244,49 +244,58 @@ static Program from(Interpretable interpretable, CelOptions options) {
*
* <p>Examples: string_startsWith_string, mathMax_list, lessThan_money_money
*/
@AutoValue
@Immutable
abstract class CelFunctionBinding {
final class CelFunctionBinding implements dev.cel.runtime.CelFunctionBinding {
private final dev.cel.runtime.CelFunctionBinding functionBinding;

private CelFunctionBinding(dev.cel.runtime.CelFunctionBinding functionBinding) {
this.functionBinding = functionBinding;
}

public abstract String getOverloadId();
@Override
public String getOverloadId() {
return functionBinding.getOverloadId();
}

public abstract ImmutableList<Class<?>> getArgTypes();
@Override
public ImmutableList<Class<?>> getArgTypes() {
return functionBinding.getArgTypes();
}

public abstract CelFunctionOverload getDefinition();
@Override
public CelFunctionOverload getDefinition() {
return functionBinding.getDefinition();
}

/**
* Create a unary function binding from the {@code overloadId}, {@code arg}, and {@code impl}.
*/
@SuppressWarnings("unchecked")
public static <T> CelFunctionBinding from(
String overloadId, Class<T> arg, CelFunctionOverload.Unary<T> impl) {
return new AutoValue_CelRuntime_CelFunctionBinding(
overloadId, ImmutableList.of(arg), (args) -> impl.apply((T) args[0]));
return new CelRuntime.CelFunctionBinding(
dev.cel.runtime.CelFunctionBinding.from(overloadId, arg, impl));
}

/**
* Create a binary function binding from the {@code overloadId}, {@code arg1}, {@code arg2}, and
* {@code impl}.
*/
@SuppressWarnings("unchecked")
public static <T1, T2> CelFunctionBinding from(
String overloadId,
Class<T1> arg1,
Class<T2> arg2,
CelFunctionOverload.Binary<T1, T2> impl) {
return new AutoValue_CelRuntime_CelFunctionBinding(
overloadId,
ImmutableList.of(arg1, arg2),
(args) -> impl.apply((T1) args[0], (T2) args[1]));
return new CelRuntime.CelFunctionBinding(
dev.cel.runtime.CelFunctionBinding.from(overloadId, arg1, arg2, impl));
}

/**
* Create a function binding from the {@code overloadId}, {@code argTypes}, and {@code impl}.
*/
public static CelFunctionBinding from(
String overloadId, Iterable<Class<?>> argTypes, CelFunctionOverload impl) {
return new AutoValue_CelRuntime_CelFunctionBinding(
overloadId, ImmutableList.copyOf(argTypes), impl);
return new CelRuntime.CelFunctionBinding(
dev.cel.runtime.CelFunctionBinding.from(overloadId, argTypes, impl));
}
}
}
50 changes: 50 additions & 0 deletions runtime/src/main/java/dev/cel/runtime/FunctionBindingImpl.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright 2025 Google LLC
//
// 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 at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package dev.cel.runtime;

import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.Immutable;

@Immutable
final class FunctionBindingImpl implements CelFunctionBinding {

private final String overloadId;

private final ImmutableList<Class<?>> argTypes;

private final CelFunctionOverload definition;

@Override
public String getOverloadId() {
return overloadId;
}

@Override
public ImmutableList<Class<?>> getArgTypes() {
return argTypes;
}

@Override
public CelFunctionOverload getDefinition() {
return definition;
}

FunctionBindingImpl(
String overloadId, ImmutableList<Class<?>> argTypes, CelFunctionOverload definition) {
this.overloadId = overloadId;
this.argTypes = argTypes;
this.definition = definition;
}
}
1 change: 1 addition & 0 deletions runtime/src/test/java/dev/cel/runtime/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ java_library(
"//runtime",
"//runtime:evaluation_exception_builder",
"//runtime:evaluation_listener",
"//runtime:function_overload",
"//runtime:interpreter",
"//runtime:proto_message_runtime_equality",
"//runtime:proto_message_runtime_helpers",
Expand Down

0 comments on commit 17278d9

Please sign in to comment.