|
| 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.extensions; |
| 16 | + |
| 17 | +import static com.google.common.collect.ImmutableSet.toImmutableSet; |
| 18 | + |
| 19 | +import com.google.common.collect.ImmutableSet; |
| 20 | +import com.google.errorprone.annotations.Immutable; |
| 21 | +import dev.cel.checker.CelCheckerBuilder; |
| 22 | +import dev.cel.common.CelFunctionDecl; |
| 23 | +import dev.cel.common.CelOverloadDecl; |
| 24 | +import dev.cel.common.types.ListType; |
| 25 | +import dev.cel.common.types.OptionalType; |
| 26 | +import dev.cel.common.types.SimpleType; |
| 27 | +import dev.cel.compiler.CelCompilerLibrary; |
| 28 | +import java.util.Set; |
| 29 | + |
| 30 | +/** Internal implementation of CEL regex compile-time extensions. */ |
| 31 | +@Immutable |
| 32 | +public final class CelRegexCompilerLibrary |
| 33 | + implements CelCompilerLibrary, CelExtensionLibrary.FeatureSet { |
| 34 | + |
| 35 | + private static final String REGEX_REPLACE_FUNCTION = "regex.replace"; |
| 36 | + private static final String REGEX_EXTRACT_FUNCTION = "regex.extract"; |
| 37 | + private static final String REGEX_EXTRACT_ALL_FUNCTION = "regex.extractAll"; |
| 38 | + |
| 39 | + /** Supported functions for the Regex compiler extension. */ |
| 40 | + public enum Function { |
| 41 | + REPLACE( |
| 42 | + CelFunctionDecl.newFunctionDeclaration( |
| 43 | + REGEX_REPLACE_FUNCTION, |
| 44 | + CelOverloadDecl.newGlobalOverload( |
| 45 | + "regex_replaceAll_string_string_string", |
| 46 | + "Replaces all the matched values using the given replace string.", |
| 47 | + SimpleType.STRING, |
| 48 | + SimpleType.STRING, |
| 49 | + SimpleType.STRING, |
| 50 | + SimpleType.STRING), |
| 51 | + CelOverloadDecl.newGlobalOverload( |
| 52 | + "regex_replaceCount_string_string_string_int", |
| 53 | + "Replaces the given number of matched values using the given replace string.", |
| 54 | + SimpleType.STRING, |
| 55 | + SimpleType.STRING, |
| 56 | + SimpleType.STRING, |
| 57 | + SimpleType.STRING, |
| 58 | + SimpleType.INT))), |
| 59 | + EXTRACT( |
| 60 | + CelFunctionDecl.newFunctionDeclaration( |
| 61 | + REGEX_EXTRACT_FUNCTION, |
| 62 | + CelOverloadDecl.newGlobalOverload( |
| 63 | + "regex_extract_string_string", |
| 64 | + "Returns the first substring that matches the regex.", |
| 65 | + OptionalType.create(SimpleType.STRING), |
| 66 | + SimpleType.STRING, |
| 67 | + SimpleType.STRING))), |
| 68 | + EXTRACTALL( |
| 69 | + CelFunctionDecl.newFunctionDeclaration( |
| 70 | + REGEX_EXTRACT_ALL_FUNCTION, |
| 71 | + CelOverloadDecl.newGlobalOverload( |
| 72 | + "regex_extractAll_string_string", |
| 73 | + "Returns an array of all substrings that match the regex.", |
| 74 | + ListType.create(SimpleType.STRING), |
| 75 | + SimpleType.STRING, |
| 76 | + SimpleType.STRING))); |
| 77 | + |
| 78 | + private final CelFunctionDecl functionDecl; |
| 79 | + |
| 80 | + String getFunction() { |
| 81 | + return functionDecl.name(); |
| 82 | + } |
| 83 | + |
| 84 | + public CelFunctionDecl getFunctionDecl() { |
| 85 | + return functionDecl; |
| 86 | + } |
| 87 | + |
| 88 | + Function(CelFunctionDecl functionDecl) { |
| 89 | + this.functionDecl = functionDecl; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private static final class Library implements CelExtensionLibrary<CelRegexCompilerLibrary> { |
| 94 | + private final CelRegexCompilerLibrary version0 = |
| 95 | + new CelRegexCompilerLibrary(0, ImmutableSet.copyOf(Function.values())); |
| 96 | + |
| 97 | + @Override |
| 98 | + public String name() { |
| 99 | + return "regex"; |
| 100 | + } |
| 101 | + |
| 102 | + @Override |
| 103 | + public ImmutableSet<CelRegexCompilerLibrary> versions() { |
| 104 | + return ImmutableSet.of(version0); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private static final Library LIBRARY = new Library(); |
| 109 | + |
| 110 | + public static CelExtensionLibrary<CelRegexCompilerLibrary> library() { |
| 111 | + return LIBRARY; |
| 112 | + } |
| 113 | + |
| 114 | + /** Returns the latest version of the 'regex' compiler extension. */ |
| 115 | + public static CelRegexCompilerLibrary regex() { |
| 116 | + return library().latest(); |
| 117 | + } |
| 118 | + |
| 119 | + /** Returns the specified version of the 'regex' compiler extension. */ |
| 120 | + public static CelRegexCompilerLibrary regex(int version) { |
| 121 | + return library().version(version); |
| 122 | + } |
| 123 | + |
| 124 | + /** Returns the 'regex' compiler extension with only the specified functions. */ |
| 125 | + public static CelRegexCompilerLibrary regex(Function... functions) { |
| 126 | + return regex(ImmutableSet.copyOf(functions)); |
| 127 | + } |
| 128 | + |
| 129 | + /** Returns the 'regex' compiler extension with only the specified functions. */ |
| 130 | + public static CelRegexCompilerLibrary regex(Set<Function> functions) { |
| 131 | + return new CelRegexCompilerLibrary(functions); |
| 132 | + } |
| 133 | + |
| 134 | + private final ImmutableSet<Function> functions; |
| 135 | + private final int version; |
| 136 | + |
| 137 | + CelRegexCompilerLibrary(Set<Function> functions) { |
| 138 | + this(-1, functions); |
| 139 | + } |
| 140 | + |
| 141 | + private CelRegexCompilerLibrary(int version, Set<Function> functions) { |
| 142 | + this.version = version; |
| 143 | + this.functions = ImmutableSet.copyOf(functions); |
| 144 | + } |
| 145 | + |
| 146 | + @Override |
| 147 | + public int version() { |
| 148 | + return version; |
| 149 | + } |
| 150 | + |
| 151 | + @Override |
| 152 | + public ImmutableSet<CelFunctionDecl> functions() { |
| 153 | + return functions.stream().map(Function::getFunctionDecl).collect(toImmutableSet()); |
| 154 | + } |
| 155 | + |
| 156 | + @Override |
| 157 | + public void setCheckerOptions(CelCheckerBuilder checkerBuilder) { |
| 158 | + functions.forEach(function -> checkerBuilder.addFunctionDeclarations(function.functionDecl)); |
| 159 | + } |
| 160 | +} |
0 commit comments