Skip to content
Merged
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 @@ -40,9 +40,9 @@
import dev.cel.common.CelVarDecl;
import dev.cel.common.internal.EnvVisitable;
import dev.cel.common.internal.EnvVisitor;
import dev.cel.common.types.CelKind;
import dev.cel.common.types.CelProtoTypes;
import dev.cel.common.types.CelType;
import dev.cel.common.types.CelTypes;
import dev.cel.compiler.CelCompiler;
import dev.cel.extensions.CelExtensionLibrary;
import dev.cel.extensions.CelExtensions;
Expand Down Expand Up @@ -484,7 +484,12 @@ private CelEnvironment.OverloadDecl toCelEnvOverloadDecl(CelOverloadDecl overloa
}

private CelEnvironment.TypeDecl toCelEnvTypeDecl(CelType type) {
return CelEnvironment.TypeDecl.create(CelTypes.format(type));
return CelEnvironment.TypeDecl.newBuilder()
.setName(type.name())
.setIsTypeParam(type.kind() == CelKind.TYPE_PARAM)
.addParams(
type.parameters().stream().map(this::toCelEnvTypeDecl).collect(toImmutableList()))
.build();
}

/** Wrapper for CelOverloadDecl, associating it with the corresponding function name. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,10 @@
import dev.cel.common.CelOptions;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.CelVarDecl;
import dev.cel.common.types.ListType;
import dev.cel.common.types.OpaqueType;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.TypeParamType;
import dev.cel.extensions.CelExtensions;
import java.net.URL;
import java.util.HashSet;
Expand Down Expand Up @@ -176,6 +178,20 @@ public void customFunctions() {
"math.isFinite",
CelOverloadDecl.newGlobalOverload(
"math_isFinite_int64", SimpleType.BOOL, SimpleType.INT)),
CelFunctionDecl.newFunctionDeclaration(
"zipGeneric",
CelOverloadDecl.newGlobalOverload(
"zip_list_list",
ListType.create(ListType.create(TypeParamType.create("T"))),
ListType.create(TypeParamType.create("T")),
ListType.create(TypeParamType.create("T")))),
CelFunctionDecl.newFunctionDeclaration(
"zip",
CelOverloadDecl.newGlobalOverload(
"zip_list_int_list_int",
ListType.create(ListType.create(SimpleType.INT)),
ListType.create(SimpleType.INT),
ListType.create(SimpleType.INT))),
CelFunctionDecl.newFunctionDeclaration(
"addWeeks",
CelOverloadDecl.newMemberOverload(
Expand Down Expand Up @@ -207,6 +223,68 @@ public void customFunctions() {
.setTarget(TypeDecl.create("google.protobuf.Timestamp"))
.setArguments(ImmutableList.of(TypeDecl.create("int")))
.setReturnType(TypeDecl.create("bool"))
.build())),
FunctionDecl.create(
"zipGeneric",
ImmutableSet.of(
OverloadDecl.newBuilder()
.setId("zip_list_list")
.setArguments(
ImmutableList.of(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build(),
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build()))
.setReturnType(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build())
.build())
.build())),
FunctionDecl.create(
"zip",
ImmutableSet.of(
OverloadDecl.newBuilder()
.setId("zip_list_int_list_int")
.setArguments(
ImmutableList.of(
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build(),
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build()))
.setReturnType(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build())
.build())
.build())));

// Random-check some standard functions: we don't want to see them explicitly defined.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,68 @@ public void toYaml_success() throws Exception {
.setReturnType(
TypeDecl.newBuilder().setName("V").setIsTypeParam(true).build())
.build())),
FunctionDecl.create(
"zip",
ImmutableSet.of(
OverloadDecl.newBuilder()
.setId("zip_list_int_list_int")
.setArguments(
ImmutableList.of(
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build(),
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build()))
.setReturnType(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("list")
.addParams(TypeDecl.create("int"))
.build())
.build())
.build())),
FunctionDecl.create(
"zipGeneric",
ImmutableSet.of(
OverloadDecl.newBuilder()
.setId("zip_list_list")
.setArguments(
ImmutableList.of(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build(),
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build()))
.setReturnType(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("list")
.addParams(
TypeDecl.newBuilder()
.setName("T")
.setIsTypeParam(true)
.build())
.build())
.build())
.build())),
FunctionDecl.create(
"coalesce",
ImmutableSet.of(
Expand Down
35 changes: 35 additions & 0 deletions testing/src/test/resources/environment/dump_env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,41 @@ functions:
return:
type_name: V
is_type_param: true
- name: zip
overloads:
- id: zip_list_int_list_int
args:
- type_name: list
params:
- type_name: int
- type_name: list
params:
- type_name: int
return:
type_name: list
params:
- type_name: list
params:
- type_name: int
- name: zipGeneric
overloads:
- id: zip_list_list
args:
- type_name: list
params:
- type_name: T
is_type_param: true
- type_name: list
params:
- type_name: T
is_type_param: true
return:
type_name: list
params:
- type_name: list
params:
- type_name: T
is_type_param: true
- name: coalesce
overloads:
- id: coalesce_null_int
Expand Down
Loading