-
Notifications
You must be signed in to change notification settings - Fork 32
Expand file tree
/
Copy pathCelEncoderExtensions.java
More file actions
153 lines (132 loc) · 5.14 KB
/
CelEncoderExtensions.java
File metadata and controls
153 lines (132 loc) · 5.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// 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 com.google.protobuf.ByteString;
import dev.cel.checker.CelCheckerBuilder;
import dev.cel.common.CelFunctionDecl;
import dev.cel.common.CelOptions;
import dev.cel.common.CelOverloadDecl;
import dev.cel.common.types.SimpleType;
import dev.cel.common.values.CelByteString;
import dev.cel.compiler.CelCompilerLibrary;
import dev.cel.runtime.CelFunctionBinding;
import dev.cel.runtime.CelRuntimeBuilder;
import dev.cel.runtime.CelRuntimeLibrary;
import java.util.Base64;
import java.util.Base64.Decoder;
import java.util.Base64.Encoder;
/** Internal implementation of Encoder Extensions. */
@Immutable
public class CelEncoderExtensions
implements CelCompilerLibrary, CelRuntimeLibrary, CelExtensionLibrary.FeatureSet {
private static final Encoder BASE64_ENCODER = Base64.getEncoder();
private static final Decoder BASE64_DECODER = Base64.getDecoder();
private final ImmutableSet<Function> functions;
private final CelOptions celOptions;
enum Function {
DECODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.decode",
CelOverloadDecl.newGlobalOverload(
"base64_decode_string", SimpleType.BYTES, SimpleType.STRING)),
CelFunctionBinding.from(
"base64_decode_string",
String.class,
str -> CelByteString.of(BASE64_DECODER.decode(str))),
CelFunctionBinding.from(
"base64_decode_string",
String.class,
str -> ByteString.copyFrom(BASE64_DECODER.decode(str)))),
ENCODE(
CelFunctionDecl.newFunctionDeclaration(
"base64.encode",
CelOverloadDecl.newGlobalOverload(
"base64_encode_bytes", SimpleType.STRING, SimpleType.BYTES)),
CelFunctionBinding.from(
"base64_encode_bytes",
CelByteString.class,
bytes -> BASE64_ENCODER.encodeToString(bytes.toByteArray())),
CelFunctionBinding.from(
"base64_encode_bytes",
ByteString.class,
bytes -> BASE64_ENCODER.encodeToString(bytes.toByteArray()))),
;
private final CelFunctionDecl functionDecl;
private final CelFunctionBinding nativeBytesFunctionBinding;
private final CelFunctionBinding protoBytesFunctionBinding;
String getFunction() {
return functionDecl.name();
}
Function(
CelFunctionDecl functionDecl,
CelFunctionBinding nativeBytesFunctionBinding,
CelFunctionBinding protoBytesFunctionBinding) {
this.functionDecl = functionDecl;
this.nativeBytesFunctionBinding = nativeBytesFunctionBinding;
this.protoBytesFunctionBinding = protoBytesFunctionBinding;
}
}
private static final class Library implements CelExtensionLibrary<CelEncoderExtensions> {
private final CelEncoderExtensions version0;
@Override
public String name() {
return "encoders";
}
@Override
public ImmutableSet<CelEncoderExtensions> versions() {
return ImmutableSet.of(version0);
}
private Library(CelOptions celOptions) {
this.version0 = new CelEncoderExtensions(celOptions);
}
}
static CelExtensionLibrary<CelEncoderExtensions> library(CelOptions celOptions) {
return new Library(celOptions);
}
@Override
public int version() {
return 0;
}
@Override
public ImmutableSet<CelFunctionDecl> functions() {
return functions.stream().map(f -> f.functionDecl).collect(toImmutableSet());
}
@Override
public void setCheckerOptions(CelCheckerBuilder checkerBuilder) {
functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl));
}
@SuppressWarnings("Immutable") // Instances of java.util.Base64 are immutable
@Override
public void setRuntimeOptions(CelRuntimeBuilder runtimeBuilder) {
functions.forEach(
function -> {
if (celOptions.evaluateCanonicalTypesToNativeValues()) {
runtimeBuilder.addFunctionBindings(
CelFunctionBinding.fromOverloads(
function.getFunction(), function.nativeBytesFunctionBinding));
} else {
runtimeBuilder.addFunctionBindings(
CelFunctionBinding.fromOverloads(
function.getFunction(), function.protoBytesFunctionBinding));
}
});
}
CelEncoderExtensions(CelOptions celOptions) {
this.celOptions = celOptions;
this.functions = ImmutableSet.copyOf(Function.values());
}
}