1616
1717import static com .google .common .truth .Truth .assertThat ;
1818import static com .google .common .truth .extensions .proto .ProtoTruth .assertThat ;
19+ import static dev .cel .common .CelFunctionDecl .newFunctionDeclaration ;
20+ import static dev .cel .common .CelOverloadDecl .newGlobalOverload ;
21+ import static dev .cel .common .CelOverloadDecl .newMemberOverload ;
1922import static org .junit .Assert .assertThrows ;
2023
2124import dev .cel .expr .CheckedExpr ;
5558import dev .cel .checker .ProtoTypeMask ;
5659import dev .cel .checker .TypeProvider ;
5760import dev .cel .common .CelAbstractSyntaxTree ;
58- import dev .cel .common .CelFunctionDecl ;
5961import dev .cel .common .CelIssue ;
6062import dev .cel .common .CelOptions ;
61- import dev .cel .common .CelOverloadDecl ;
6263import dev .cel .common .CelProtoAbstractSyntaxTree ;
6364import dev .cel .common .CelValidationException ;
6465import dev .cel .common .CelValidationResult ;
9798import dev .cel .testing .testdata .proto3 .TestAllTypesProto .TestAllTypes ;
9899import java .util .ArrayList ;
99100import java .util .List ;
101+ import java .util .Map ;
100102import java .util .Optional ;
101103import java .util .concurrent .ExecutorService ;
102104import java .util .concurrent .Executors ;
@@ -1800,9 +1802,9 @@ public boolean isAssignableFrom(CelType other) {
18001802 CelFactory .standardCelBuilder ()
18011803 .addVar ("x" , SimpleType .INT )
18021804 .addFunctionDeclarations (
1803- CelFunctionDecl . newFunctionDeclaration (
1805+ newFunctionDeclaration (
18041806 "print" ,
1805- CelOverloadDecl . newGlobalOverload (
1807+ newGlobalOverload (
18061808 "print_overload" ,
18071809 SimpleType .STRING ,
18081810 customType ))) // The overload would accept either Int or CustomType
@@ -1816,6 +1818,36 @@ public boolean isAssignableFrom(CelType other) {
18161818 assertThat (result ).isEqualTo ("5" );
18171819 }
18181820
1821+ @ Test
1822+ @ SuppressWarnings ("unchecked" ) // test only
1823+ public void program_functionParamWithWellKnownType () throws Exception {
1824+ Cel cel =
1825+ CelFactory .standardCelBuilder ()
1826+ .addFunctionDeclarations (
1827+ newFunctionDeclaration (
1828+ "hasStringValue" ,
1829+ newMemberOverload (
1830+ "struct_hasStringValue_string_string" ,
1831+ SimpleType .BOOL ,
1832+ StructTypeReference .create ("google.protobuf.Struct" ),
1833+ SimpleType .STRING ,
1834+ SimpleType .STRING )))
1835+ .addFunctionBindings (
1836+ CelFunctionBinding .from (
1837+ "struct_hasStringValue_string_string" ,
1838+ ImmutableList .of (Map .class , String .class , String .class ),
1839+ args -> {
1840+ Map <String , String > map = (Map <String , String >) args [0 ];
1841+ return map .containsKey (args [1 ]) && map .containsValue (args [2 ]);
1842+ }))
1843+ .build ();
1844+ CelAbstractSyntaxTree ast = cel .compile ("{'a': 'b'}.hasStringValue('a', 'b')" ).getAst ();
1845+
1846+ boolean result = (boolean ) cel .createProgram (ast ).eval ();
1847+
1848+ assertThat (result ).isTrue ();
1849+ }
1850+
18191851 @ Test
18201852 public void toBuilder_isImmutable () {
18211853 CelBuilder celBuilder = CelFactory .standardCelBuilder ();
0 commit comments