-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add dev.cel.runtime.CelFunctionBinding
PiperOrigin-RevId: 725808692
- Loading branch information
1 parent
a0c2012
commit 17278d9
Showing
6 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
runtime/src/main/java/dev/cel/runtime/CelFunctionBinding.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
runtime/src/main/java/dev/cel/runtime/FunctionBindingImpl.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters