3030import dev .cel .common .CelContainer ;
3131import dev .cel .common .CelValidationException ;
3232import dev .cel .common .exceptions .CelAttributeNotFoundException ;
33+ import dev .cel .common .exceptions .CelInvalidArgumentException ;
3334import dev .cel .common .types .CelType ;
3435import dev .cel .common .types .ListType ;
3536import dev .cel .common .types .MapType ;
@@ -89,7 +90,8 @@ public final class CelNativeTypesExtensionsTest {
8990 TestNestedSimplePojo .class ,
9091 TestGetterFieldTypeMismatchPojo .class ,
9192 TestAbstractPojo .class ,
92- TestURLPojo .class );
93+ TestURLPojo .class ,
94+ TestArrayPojo .class );
9395
9496 private static final Cel CEL =
9597 CelFactory .plannerCelBuilder ()
@@ -322,10 +324,10 @@ public void nativeTypes_anonymousClass_throwsException() {
322324
323325 @ Test
324326 public void nativeTypes_createStruct_privateConstructor () throws Exception {
325- Object result = eval ("TestPrivateConstructorPojo{value:" + " 'hello'}" );
327+ TestPrivateConstructorPojo result =
328+ (TestPrivateConstructorPojo ) eval ("TestPrivateConstructorPojo{value:" + " 'hello'}" );
326329
327- assertThat (result ).isInstanceOf (TestPrivateConstructorPojo .class );
328- assertThat (((TestPrivateConstructorPojo ) result ).value ).isEqualTo ("hello" );
330+ assertThat (result .value ).isEqualTo ("hello" );
329331 }
330332
331333 @ Test
@@ -374,10 +376,9 @@ public void nativeTypes_missingNoArgConstructor_throws() throws Exception {
374376
375377 @ Test
376378 public void nativeTypes_createWithDeepConversion () throws Exception {
377- Object result = eval ("TestDeepConversionPojo{ints: [1, 2], floats: {'a': 1.0, 'b': 2.0}}" );
378-
379- assertThat (result ).isInstanceOf (TestDeepConversionPojo .class );
380- TestDeepConversionPojo pojo = (TestDeepConversionPojo ) result ;
379+ TestDeepConversionPojo pojo =
380+ (TestDeepConversionPojo )
381+ eval ("TestDeepConversionPojo{ints: [1, 2], floats: {'a': 1.0, 'b': 2.0}}" );
381382 assertThat (pojo .ints .get (0 )).isEqualTo (1 );
382383 assertThat (pojo .floats ).containsEntry ("a" , 1.0f );
383384 }
@@ -397,11 +398,93 @@ public void nativeTypes_unsupportedTypeSet_throwsOnRegistration() throws Excepti
397398 }
398399
399400 @ Test
400- public void nativeTypes_arrayType_throwsOnRegistration () throws Exception {
401- IllegalArgumentException e =
401+ public void nativeTypes_arrayType_construction () throws Exception {
402+ String expr =
403+ "TestArrayPojo{"
404+ + " strings: ['a', 'b'],"
405+ + " ints: [1, 2],"
406+ + " nesteds: [TestNestedType{value: 'nested'}],"
407+ + " matrix: [[1, 2], [3, 4]],"
408+ + " nestedMatrix: [[TestNestedType{value: 'm1'}], [TestNestedType{value: 'm2'}]],"
409+ + " byteArrays: [b'foo', b'bar']"
410+ + "}" ;
411+
412+ TestArrayPojo pojo = (TestArrayPojo ) eval (expr );
413+
414+ assertThat (pojo .strings ).isEqualTo (new String [] {"a" , "b" });
415+ assertThat (pojo .ints ).isEqualTo (new int [] {1 , 2 });
416+ assertThat (pojo .nesteds ).hasLength (1 );
417+ assertThat (pojo .nesteds [0 ].value ).isEqualTo ("nested" );
418+ assertThat (pojo .matrix ).hasLength (2 );
419+ assertThat (pojo .matrix [0 ]).isEqualTo (new int [] {1 , 2 });
420+ assertThat (pojo .matrix [1 ]).isEqualTo (new int [] {3 , 4 });
421+ assertThat (pojo .nestedMatrix ).hasLength (2 );
422+ assertThat (pojo .nestedMatrix [0 ][0 ].value ).isEqualTo ("m1" );
423+ assertThat (pojo .nestedMatrix [1 ][0 ].value ).isEqualTo ("m2" );
424+ assertThat (pojo .byteArrays ).hasLength (2 );
425+ assertThat (pojo .byteArrays [0 ]).isEqualTo ("foo" .getBytes (UTF_8 ));
426+ assertThat (pojo .byteArrays [1 ]).isEqualTo ("bar" .getBytes (UTF_8 ));
427+ }
428+
429+ @ Test
430+ public void nativeTypes_arrayType_selection () throws Exception {
431+ CelNativeTypesExtensions extensions = CelExtensions .nativeTypes (TestArrayPojo .class );
432+ Cel cel =
433+ CelFactory .plannerCelBuilder ()
434+ .setContainer (CelContainer .ofName ("dev.cel.extensions.CelNativeTypesExtensionsTest" ))
435+ .addCompilerLibraries (extensions )
436+ .addRuntimeLibraries (extensions )
437+ .addVar ("pojo" , StructTypeReference .create (TestArrayPojo .class .getCanonicalName ()))
438+ .build ();
439+ String expr =
440+ "pojo.strings[1] == 'b'"
441+ + " && pojo.ints[0] == 1"
442+ + " && pojo.nesteds[0].value == 'nested'"
443+ + " && pojo.matrix[1][0] == 3"
444+ + " && pojo.nestedMatrix[1][0].value == 'm2'"
445+ + " && pojo.byteArrays[1] == b'bar'" ;
446+ CelAbstractSyntaxTree ast = cel .compile (expr ).getAst ();
447+ CelRuntime .Program program = cel .createProgram (ast );
448+
449+ TestArrayPojo input = new TestArrayPojo ();
450+ input .strings = new String [] {"a" , "b" };
451+ input .ints = new int [] {1 , 2 };
452+ TestNestedType nested = new TestNestedType ();
453+ nested .value = "nested" ;
454+ input .nesteds = new TestNestedType [] {nested };
455+ input .matrix = new int [][] {{1 , 2 }, {3 , 4 }};
456+ TestNestedType m1 = new TestNestedType ();
457+ m1 .value = "m1" ;
458+ TestNestedType m2 = new TestNestedType ();
459+ m2 .value = "m2" ;
460+ input .nestedMatrix = new TestNestedType [][] {{m1 }, {m2 }};
461+ input .byteArrays = new byte [][] {"foo" .getBytes (UTF_8 ), "bar" .getBytes (UTF_8 )};
462+
463+ assertThat (program .eval (ImmutableMap .of ("pojo" , input ))).isEqualTo (true );
464+ }
465+
466+ @ Test
467+ public void nativeTypes_arrayWithNullElement_throws () throws Exception {
468+ CelNativeTypesExtensions extensions = CelExtensions .nativeTypes (TestArrayPojo .class );
469+ Cel cel =
470+ CelFactory .plannerCelBuilder ()
471+ .setContainer (CelContainer .ofName ("dev.cel.extensions.CelNativeTypesExtensionsTest" ))
472+ .addCompilerLibraries (extensions )
473+ .addRuntimeLibraries (extensions )
474+ .addVar ("pojo" , StructTypeReference .create (TestArrayPojo .class .getCanonicalName ()))
475+ .build ();
476+
477+ CelAbstractSyntaxTree ast = cel .compile ("pojo.strings" ).getAst ();
478+ CelRuntime .Program program = cel .createProgram (ast );
479+
480+ TestArrayPojo input = new TestArrayPojo ();
481+ input .strings = new String [] {"a" , null , "c" };
482+
483+ CelEvaluationException e =
402484 assertThrows (
403- IllegalArgumentException .class , () -> CelExtensions .nativeTypes (TestArrayPojo .class ));
404- assertThat (e ).hasMessageThat ().contains ("Unsupported type for property 'values'" );
485+ CelEvaluationException .class , () -> program .eval (ImmutableMap .of ("pojo" , input )));
486+ assertThat (e ).hasCauseThat ().isInstanceOf (CelInvalidArgumentException .class );
487+ assertThat (e ).hasCauseThat ().hasMessageThat ().contains ("Element at index 1 is null." );
405488 }
406489
407490 @ Test
@@ -653,10 +736,7 @@ public void nativeTypes_createWithUint_fromUnsignedLong() throws Exception {
653736 .getAst ();
654737 CelRuntime .Program program = celRuntime .createProgram (ast );
655738
656- Object result = program .eval ();
657-
658- assertThat (result ).isInstanceOf (TestAllTypesPublicFieldsPojo .class );
659- TestAllTypesPublicFieldsPojo pojo = (TestAllTypesPublicFieldsPojo ) result ;
739+ TestAllTypesPublicFieldsPojo pojo = (TestAllTypesPublicFieldsPojo ) program .eval ();
660740 assertThat (pojo .uintVal ).isEqualTo (UnsignedLong .fromLongBits (42L ));
661741 }
662742
@@ -1245,7 +1325,12 @@ public static class TestWildcardPojo {
12451325 }
12461326
12471327 public static class TestArrayPojo {
1248- public String [] values ;
1328+ public String [] strings ;
1329+ public int [] ints ;
1330+ public TestNestedType [] nesteds ;
1331+ public int [][] matrix ;
1332+ public TestNestedType [][] nestedMatrix ;
1333+ public byte [][] byteArrays ;
12491334 }
12501335
12511336 public static class TestOptionalUrlPojo {
0 commit comments