1414
1515package dev .cel .runtime .standard ;
1616
17+ import com .google .common .collect .ImmutableList ;
1718import com .google .common .collect .ImmutableSet ;
19+ import com .google .errorprone .annotations .Immutable ;
20+ import com .google .re2j .Pattern ;
1821import dev .cel .common .CelOptions ;
19- import dev .cel .common .exceptions . CelInvalidArgumentException ;
22+ import dev .cel .common .annotations . Internal ;
2023import dev .cel .runtime .CelFunctionBinding ;
24+ import dev .cel .runtime .CelFunctionOverload ;
25+ import dev .cel .runtime .CelResolvedOverload ;
2126import dev .cel .runtime .RuntimeEquality ;
2227import dev .cel .runtime .RuntimeHelpers ;
2328import java .util .Arrays ;
2429
2530/** Standard function for {@code matches}. */
2631public final class MatchesFunction extends CelStandardFunction {
32+ private static final String MATCHES_FUNCTION = "matches" ;
2733 private static final MatchesFunction ALL_OVERLOADS = create (MatchesOverload .values ());
2834
2935 public static MatchesFunction create () {
@@ -38,51 +44,68 @@ public static MatchesFunction create(Iterable<MatchesFunction.MatchesOverload> o
3844 return new MatchesFunction (ImmutableSet .copyOf (overloads ));
3945 }
4046
47+ @ Internal
48+ public static boolean isMatchesOverload (CelResolvedOverload resolvedOverload ) {
49+ return resolvedOverload .getDefinition () instanceof StandardMatchesOverload ;
50+ }
51+
52+ @ Internal
53+ public static CelResolvedOverload newPrecompiledOverload (
54+ CelResolvedOverload resolvedOverload , String regexPattern ) {
55+ CelOptions celOptions = ((StandardMatchesOverload ) resolvedOverload .getDefinition ()).celOptions ;
56+ Pattern compiledPattern = RuntimeHelpers .compileRegexPattern (regexPattern , celOptions );
57+ String overloadId = resolvedOverload .getOverloadId ();
58+ CelFunctionBinding binding =
59+ CelFunctionBinding .from (
60+ overloadId ,
61+ String .class ,
62+ target -> RuntimeHelpers .matches (target , compiledPattern , celOptions ));
63+ return CelResolvedOverload .of (
64+ MATCHES_FUNCTION ,
65+ overloadId ,
66+ binding .getDefinition (),
67+ binding .isStrict (),
68+ binding .getArgTypes ());
69+ }
70+
4171 /** Overloads for the standard function. */
4272 public enum MatchesOverload implements CelStandardOverload {
43- MATCHES (
44- (celOptions , runtimeEquality ) ->
45- CelFunctionBinding .from (
46- "matches" ,
47- String .class ,
48- String .class ,
49- (String string , String regexp ) -> {
50- try {
51- return RuntimeHelpers .matches (string , regexp , celOptions );
52- } catch (RuntimeException e ) {
53- throw new CelInvalidArgumentException (e );
54- }
55- })),
73+ MATCHES (MATCHES_FUNCTION ),
5674 // Duplicate receiver-style matches overload.
57- MATCHES_STRING (
58- (celOptions , runtimeEquality ) ->
59- CelFunctionBinding .from (
60- "matches_string" ,
61- String .class ,
62- String .class ,
63- (String string , String regexp ) -> {
64- try {
65- return RuntimeHelpers .matches (string , regexp , celOptions );
66- } catch (RuntimeException e ) {
67- throw new CelInvalidArgumentException (e );
68- }
69- })),
75+ MATCHES_STRING ("matches_string" ),
7076 ;
7177
72- private final CelStandardOverload standardOverload ;
78+ private final String overloadId ;
7379
7480 @ Override
7581 public CelFunctionBinding newFunctionBinding (
7682 CelOptions celOptions , RuntimeEquality runtimeEquality ) {
77- return standardOverload .newFunctionBinding (celOptions , runtimeEquality );
83+ return CelFunctionBinding .from (
84+ overloadId ,
85+ ImmutableList .of (String .class , String .class ),
86+ new StandardMatchesOverload (celOptions ));
87+ }
88+
89+ MatchesOverload (String overloadId ) {
90+ this .overloadId = overloadId ;
91+ }
92+ }
93+
94+ @ Immutable
95+ private static final class StandardMatchesOverload implements CelFunctionOverload {
96+ private final CelOptions celOptions ;
97+
98+ @ Override
99+ public Object apply (Object [] args ) {
100+ return RuntimeHelpers .matches ((String ) args [0 ], (String ) args [1 ], celOptions );
78101 }
79102
80- MatchesOverload ( CelStandardOverload standardOverload ) {
81- this .standardOverload = standardOverload ;
103+ private StandardMatchesOverload ( CelOptions celOptions ) {
104+ this .celOptions = celOptions ;
82105 }
83106 }
84107
85108 private MatchesFunction (ImmutableSet <CelStandardOverload > overloads ) {
86- super ("matches" , overloads );
109+ super (MATCHES_FUNCTION , overloads );
87110 }
88111}
0 commit comments