|
| 1 | +// Copyright 2025 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package dev.cel.compiler.tools; |
| 16 | + |
| 17 | +import dev.cel.expr.CheckedExpr; |
| 18 | +import com.google.common.collect.ImmutableSet; |
| 19 | +import com.google.common.io.Files; |
| 20 | +import com.google.protobuf.DescriptorProtos.FileDescriptorSet; |
| 21 | +import com.google.protobuf.Descriptors.FileDescriptor; |
| 22 | +import com.google.protobuf.ExtensionRegistry; |
| 23 | +import dev.cel.common.CelAbstractSyntaxTree; |
| 24 | +import dev.cel.common.CelDescriptorUtil; |
| 25 | +import dev.cel.common.CelOptions; |
| 26 | +import dev.cel.common.CelProtoAbstractSyntaxTree; |
| 27 | +import dev.cel.common.CelValidationException; |
| 28 | +import dev.cel.compiler.CelCompiler; |
| 29 | +import dev.cel.compiler.CelCompilerBuilder; |
| 30 | +import dev.cel.compiler.CelCompilerFactory; |
| 31 | +import dev.cel.extensions.CelExtensions; |
| 32 | +import dev.cel.extensions.CelOptionalLibrary; |
| 33 | +import dev.cel.parser.CelStandardMacro; |
| 34 | +import java.io.File; |
| 35 | +import java.io.FileOutputStream; |
| 36 | +import java.io.IOException; |
| 37 | +import java.nio.file.Path; |
| 38 | +import java.nio.file.Paths; |
| 39 | +import java.util.concurrent.Callable; |
| 40 | +import picocli.CommandLine; |
| 41 | +import picocli.CommandLine.Option; |
| 42 | + |
| 43 | +/** |
| 44 | + * CelCompilerTool is a binary that takes a CEL expression in string, compiles it into a |
| 45 | + * dev.cel.expr.CheckedExpr protobuf message, then writes the content to a .binary pb file. |
| 46 | + */ |
| 47 | +final class CelCompilerTool implements Callable<Integer> { |
| 48 | + |
| 49 | + @Option( |
| 50 | + names = {"--cel_expression"}, |
| 51 | + description = "CEL expression") |
| 52 | + private String celExpression = ""; |
| 53 | + |
| 54 | + @Option( |
| 55 | + names = {"--transitive_descriptor_set"}, |
| 56 | + description = "Path to the transitive set of descriptors") |
| 57 | + private String transitiveDescriptorSetPath = ""; |
| 58 | + |
| 59 | + @Option( |
| 60 | + names = {"--output"}, |
| 61 | + description = "Output path for the compiled binarypb") |
| 62 | + private String output = ""; |
| 63 | + |
| 64 | + private static final CelOptions CEL_OPTIONS = CelOptions.DEFAULT; |
| 65 | + |
| 66 | + private static FileDescriptorSet load(String descriptorSetPath) { |
| 67 | + try { |
| 68 | + byte[] descriptorBytes = Files.toByteArray(new File(descriptorSetPath)); |
| 69 | + return FileDescriptorSet.parseFrom(descriptorBytes, ExtensionRegistry.getEmptyRegistry()); |
| 70 | + } catch (IOException e) { |
| 71 | + throw new IllegalArgumentException( |
| 72 | + "Failed to load FileDescriptorSet from path: " + descriptorSetPath, e); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static CelAbstractSyntaxTree compile( |
| 77 | + String expression, String transitiveDescriptorSetPath) throws CelValidationException { |
| 78 | + CelCompilerBuilder compilerBuilder = |
| 79 | + CelCompilerFactory.standardCelCompilerBuilder() |
| 80 | + .setOptions(CEL_OPTIONS) |
| 81 | + .setStandardMacros(CelStandardMacro.STANDARD_MACROS) |
| 82 | + .addLibraries( |
| 83 | + CelExtensions.bindings(), |
| 84 | + CelExtensions.encoders(), |
| 85 | + CelExtensions.math(CEL_OPTIONS), |
| 86 | + CelExtensions.lists(), |
| 87 | + CelExtensions.protos(), |
| 88 | + CelExtensions.strings(), |
| 89 | + CelOptionalLibrary.INSTANCE); |
| 90 | + if (!transitiveDescriptorSetPath.isEmpty()) { |
| 91 | + ImmutableSet<FileDescriptor> transitiveFileDescriptors = |
| 92 | + CelDescriptorUtil.getFileDescriptorsFromFileDescriptorSet( |
| 93 | + load(transitiveDescriptorSetPath)); |
| 94 | + compilerBuilder.addFileTypes(transitiveFileDescriptors); |
| 95 | + } |
| 96 | + |
| 97 | + CelCompiler celCompiler = compilerBuilder.build(); |
| 98 | + |
| 99 | + return celCompiler.compile(expression).getAst(); |
| 100 | + } |
| 101 | + |
| 102 | + private static void writeCheckedExpr(CelAbstractSyntaxTree ast, String filePath) |
| 103 | + throws IOException { |
| 104 | + CheckedExpr checkedExpr = CelProtoAbstractSyntaxTree.fromCelAst(ast).toCheckedExpr(); |
| 105 | + Path path = Paths.get(filePath); |
| 106 | + try (FileOutputStream output = new FileOutputStream(path.toFile())) { |
| 107 | + checkedExpr.writeTo(output); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public Integer call() { |
| 113 | + try { |
| 114 | + CelAbstractSyntaxTree ast = compile(celExpression, transitiveDescriptorSetPath); |
| 115 | + writeCheckedExpr(ast, output); |
| 116 | + } catch (Exception e) { |
| 117 | + String errorMessage = |
| 118 | + String.format( |
| 119 | + "Expression [%s] failed to compile. Reason: %s", celExpression, e.getMessage()); |
| 120 | + System.err.print(errorMessage); |
| 121 | + return -1; |
| 122 | + } |
| 123 | + |
| 124 | + return 0; |
| 125 | + } |
| 126 | + |
| 127 | + public static void main(String[] args) { |
| 128 | + CelCompilerTool compilerTool = new CelCompilerTool(); |
| 129 | + CommandLine cmd = new CommandLine(compilerTool); |
| 130 | + cmd.setTrimQuotes(false); |
| 131 | + cmd.parseArgs(args); |
| 132 | + |
| 133 | + int exitCode = cmd.execute(args); |
| 134 | + System.exit(exitCode); |
| 135 | + } |
| 136 | + |
| 137 | + CelCompilerTool() {} |
| 138 | +} |
0 commit comments