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
17 changes: 17 additions & 0 deletions extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,20 @@ java_library(
visibility = ["//:internal"],
exports = ["//extensions/src/main/java/dev/cel/extensions:bindings"],
)

java_library(
name = "encoders_compiler_library",
visibility = ["//:internal"],
exports = ["//extensions/src/main/java/dev/cel/extensions:encoders_compiler_library"],
)

java_library(
name = "encoders_runtime_library",
visibility = ["//:internal"],
exports = ["//extensions/src/main/java/dev/cel/extensions:encoders_runtime_library"],
)

cel_android_library(
name = "encoders_runtime_library_android",
exports = ["//extensions/src/main/java/dev/cel/extensions:encoders_runtime_library_android"],
)
54 changes: 50 additions & 4 deletions extensions/src/main/java/dev/cel/extensions/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -236,17 +236,18 @@ java_library(
],
)

# build_cleaner: disable=deps
java_library(
name = "encoders",
srcs = ["CelEncoderExtensions.java"],
deps = [
":encoders_compiler_library",
":encoders_runtime_library",
":extension_library",
"//checker:checker_builder",
"//common:compiler_common",
"//common:cel_function_decl",
"//common:options",
"//common/types",
"//common/values:cel_byte_string",
"//compiler:compiler_builder",
"//extensions:extension_library",
"//runtime",
"//runtime:function_binding",
"@maven//:com_google_errorprone_error_prone_annotations",
Expand All @@ -255,6 +256,51 @@ java_library(
],
)

java_library(
name = "encoders_compiler_library",
srcs = ["CelEncoderCompilerLibrary.java"],
tags = [
],
deps = [
":extension_library",
"//checker:checker_builder",
"//common:cel_function_decl",
"//common:cel_overload_decl",
"//common/types",
"//compiler:compiler_builder",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

java_library(
name = "encoders_runtime_library",
srcs = ["CelEncoderRuntimeLibrary.java"],
tags = [
],
deps = [
"//common/values:cel_byte_string",
"//runtime:function_binding",
"//runtime:lite_runtime",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven//:com_google_guava_guava",
],
)

cel_android_library(
name = "encoders_runtime_library_android",
srcs = ["CelEncoderRuntimeLibrary.java"],
tags = [
],
deps = [
"//common/values:cel_byte_string",
"//runtime:function_binding_android",
"//runtime:lite_runtime_android",
"@maven//:com_google_errorprone_error_prone_annotations",
"@maven_android//:com_google_guava_guava",
],
)

java_library(
name = "optional_library",
srcs = ["CelOptionalLibrary.java"],
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2023 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.extensions;

import static com.google.common.collect.ImmutableSet.toImmutableSet;

import com.google.common.collect.ImmutableSet;
import com.google.errorprone.annotations.Immutable;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.types.SimpleType;
import dev.cel.compiler.CelCompilerLibrary;
import java.util.Set;

/** Internal implementation of CEL Encoder compile-time extensions. */
@Immutable
public final class CelEncoderCompilerLibrary
implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet {

/** Enumeration of functions for Encoder compile-time extension. */
public enum Function {
DECODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.decode",
CelOverloadDecl.newGlobalOverload(
"base64_decode_string", SimpleType.BYTES, SimpleType.STRING))),
ENCODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.encode",
CelOverloadDecl.newGlobalOverload(
"base64_encode_bytes", SimpleType.STRING, SimpleType.BYTES)));

private final CelFunctionDecl functionDecl;

public String getFunction() {
return functionDecl.name();
}

public CelFunctionDecl getFunctionDecl() {
return functionDecl;
}

Function(CelFunctionDecl functionDecl) {
this.functionDecl = functionDecl;
}
}

private static final class Library implements CelExtensionLibrary<CelEncoderCompilerLibrary> {
private final CelEncoderCompilerLibrary version0;

Library() {
version0 = new CelEncoderCompilerLibrary(0, ImmutableSet.copyOf(Function.values()));
}

@Override
public String name() {
return "encoders";
}

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

private static final Library LIBRARY = new Library();

public static CelExtensionLibrary<CelEncoderCompilerLibrary> library() {
return LIBRARY;
}

/** Returns the latest version of the 'encoders' compiler extension. */
public static CelEncoderCompilerLibrary encoders() {
return library().latest();
}

/** Returns the specified version of the 'encoders' compiler extension. */
public static CelEncoderCompilerLibrary encoders(int version) {
return library().version(version);
}

/** Returns the 'encoders' compiler extension with only the specified functions. */
public static CelEncoderCompilerLibrary encoders(Function... functions) {
return encoders(ImmutableSet.copyOf(functions));
}

/** Returns the 'encoders' compiler extension with only the specified functions. */
public static CelEncoderCompilerLibrary encoders(Set<Function> functions) {
return new CelEncoderCompilerLibrary(functions);
}

private final ImmutableSet<Function> functions;
private final int version;

CelEncoderCompilerLibrary(Set<Function> functions) {
this(-1, functions);
}

private CelEncoderCompilerLibrary(int version, Set<Function> functions) {
this.version = version;
this.functions = ImmutableSet.copyOf(functions);
}

@Override
public int version() {
return version;
}

@Override
public ImmutableSet<CelFunctionDecl> functions() {
return functions.stream().map(Function::getFunctionDecl).collect(toImmutableSet());
}

@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
}
}
Loading
Loading