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 ;
@@ -90,7 +91,8 @@ public final class CelNativeTypesExtensionsTest {
9091 TestGetterFieldTypeMismatchPojo .class ,
9192 TestAbstractPojo .class ,
9293 TestURLPojo .class ,
93- PojoWithEnum .class );
94+ PojoWithEnum .class ,
95+ TestArrayPojo .class );
9496
9597 private static final Cel CEL =
9698 CelFactory .plannerCelBuilder ()
@@ -323,10 +325,10 @@ public void nativeTypes_anonymousClass_throwsException() {
323325
324326 @ Test
325327 public void nativeTypes_createStruct_privateConstructor () throws Exception {
326- Object result = eval ("TestPrivateConstructorPojo{value:" + " 'hello'}" );
328+ TestPrivateConstructorPojo result =
329+ (TestPrivateConstructorPojo ) eval ("TestPrivateConstructorPojo{value:" + " 'hello'}" );
327330
328- assertThat (result ).isInstanceOf (TestPrivateConstructorPojo .class );
329- assertThat (((TestPrivateConstructorPojo ) result ).value ).isEqualTo ("hello" );
331+ assertThat (result .value ).isEqualTo ("hello" );
330332 }
331333
332334 @ Test
@@ -375,10 +377,9 @@ public void nativeTypes_missingNoArgConstructor_throws() throws Exception {
375377
376378 @ Test
377379 public void nativeTypes_createWithDeepConversion () throws Exception {
378- Object result = eval ("TestDeepConversionPojo{ints: [1, 2], floats: {'a': 1.0, 'b': 2.0}}" );
379-
380- assertThat (result ).isInstanceOf (TestDeepConversionPojo .class );
381- TestDeepConversionPojo pojo = (TestDeepConversionPojo ) result ;
380+ TestDeepConversionPojo pojo =
381+ (TestDeepConversionPojo )
382+ eval ("TestDeepConversionPojo{ints: [1, 2], floats: {'a': 1.0, 'b': 2.0}}" );
382383 assertThat (pojo .ints .get (0 )).isEqualTo (1 );
383384 assertThat (pojo .floats ).containsEntry ("a" , 1.0f );
384385 }
@@ -398,11 +399,92 @@ public void nativeTypes_unsupportedTypeSet_throwsOnRegistration() throws Excepti
398399 }
399400
400401 @ Test
401- public void nativeTypes_arrayType_throwsOnRegistration () throws Exception {
402- IllegalArgumentException e =
402+ public void nativeTypes_arrayType_construction () throws Exception {
403+ String expr =
404+ "TestArrayPojo{"
405+ + " strings: ['a', 'b'],"
406+ + " ints: [1, 2],"
407+ + " nesteds: [TestNestedType{value: 'nested'}],"
408+ + " matrix: [[1, 2], [3, 4]],"
409+ + " nestedMatrix: [[TestNestedType{value: 'm1'}], [TestNestedType{value: 'm2'}]],"
410+ + " byteArrays: [b'foo', b'bar']"
411+ + "}" ;
412+
413+ TestArrayPojo pojo = (TestArrayPojo ) eval (expr );
414+
415+ assertThat (pojo .strings ).isEqualTo (new String [] {"a" , "b" });
416+ assertThat (pojo .ints ).isEqualTo (new int [] {1 , 2 });
417+ assertThat (pojo .nesteds ).hasLength (1 );
418+ assertThat (pojo .nesteds [0 ].value ).isEqualTo ("nested" );
419+ assertThat (pojo .matrix ).hasLength (2 );
420+ assertThat (pojo .matrix [0 ]).isEqualTo (new int [] {1 , 2 });
421+ assertThat (pojo .matrix [1 ]).isEqualTo (new int [] {3 , 4 });
422+ assertThat (pojo .nestedMatrix ).hasLength (2 );
423+ assertThat (pojo .nestedMatrix [0 ][0 ].value ).isEqualTo ("m1" );
424+ assertThat (pojo .nestedMatrix [1 ][0 ].value ).isEqualTo ("m2" );
425+ assertThat (pojo .byteArrays ).hasLength (2 );
426+ assertThat (pojo .byteArrays [0 ]).isEqualTo ("foo" .getBytes (UTF_8 ));
427+ assertThat (pojo .byteArrays [1 ]).isEqualTo ("bar" .getBytes (UTF_8 ));
428+ }
429+
430+ @ Test
431+ public void nativeTypes_arrayType_selection () throws Exception {
432+ CelNativeTypesExtensions extensions = CelExtensions .nativeTypes (TestArrayPojo .class );
433+ Cel cel =
434+ CelFactory .plannerCelBuilder ()
435+ .setContainer (CelContainer .ofName ("dev.cel.extensions.CelNativeTypesExtensionsTest" ))
436+ .addCompilerLibraries (extensions )
437+ .addRuntimeLibraries (extensions )
438+ .addVar ("pojo" , StructTypeReference .create (TestArrayPojo .class .getCanonicalName ()))
439+ .build ();
440+ String expr =
441+ "pojo.strings[1] == 'b'"
442+ + " && pojo.ints[0] == 1"
443+ + " && pojo.nesteds[0].value == 'nested'"
444+ + " && pojo.matrix[1][0] == 3"
445+ + " && pojo.nestedMatrix[1][0].value == 'm2'"
446+ + " && pojo.byteArrays[1] == b'bar'" ;
447+ CelAbstractSyntaxTree ast = cel .compile (expr ).getAst ();
448+ CelRuntime .Program program = cel .createProgram (ast );
449+
450+ TestArrayPojo input = new TestArrayPojo ();
451+ input .strings = new String [] {"a" , "b" };
452+ input .ints = new int [] {1 , 2 };
453+ TestNestedType nested = new TestNestedType ();
454+ nested .value = "nested" ;
455+ input .nesteds = new TestNestedType [] {nested };
456+ input .matrix = new int [][] {{1 , 2 }, {3 , 4 }};
457+ TestNestedType m1 = new TestNestedType ();
458+ m1 .value = "m1" ;
459+ TestNestedType m2 = new TestNestedType ();
460+ m2 .value = "m2" ;
461+ input .nestedMatrix = new TestNestedType [][] {{m1 }, {m2 }};
462+ input .byteArrays = new byte [][] {"foo" .getBytes (UTF_8 ), "bar" .getBytes (UTF_8 )};
463+
464+ assertThat (program .eval (ImmutableMap .of ("pojo" , input ))).isEqualTo (true );
465+ }
466+
467+ @ Test
468+ public void nativeTypes_arrayWithNullElement_throws () throws Exception {
469+ CelNativeTypesExtensions extensions = CelExtensions .nativeTypes (TestArrayPojo .class );
470+ Cel cel =
471+ CelFactory .plannerCelBuilder ()
472+ .setContainer (CelContainer .ofName ("dev.cel.extensions.CelNativeTypesExtensionsTest" ))
473+ .addCompilerLibraries (extensions )
474+ .addRuntimeLibraries (extensions )
475+ .addVar ("pojo" , StructTypeReference .create (TestArrayPojo .class .getCanonicalName ()))
476+ .build ();
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 =
403484 assertThrows (
404- IllegalArgumentException .class , () -> CelExtensions .nativeTypes (TestArrayPojo .class ));
405- 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." );
406488 }
407489
408490 @ Test
@@ -662,10 +744,7 @@ public void nativeTypes_createWithUint_fromUnsignedLong() throws Exception {
662744 .getAst ();
663745 CelRuntime .Program program = celRuntime .createProgram (ast );
664746
665- Object result = program .eval ();
666-
667- assertThat (result ).isInstanceOf (TestAllTypesPublicFieldsPojo .class );
668- TestAllTypesPublicFieldsPojo pojo = (TestAllTypesPublicFieldsPojo ) result ;
747+ TestAllTypesPublicFieldsPojo pojo = (TestAllTypesPublicFieldsPojo ) program .eval ();
669748 assertThat (pojo .uintVal ).isEqualTo (UnsignedLong .fromLongBits (42L ));
670749 }
671750
@@ -782,6 +861,8 @@ public void nativeTypes_nullSafeTraversal() throws Exception {
782861 assertThat (cel .createProgram (cel .compile ("pojo.int64Val" ).getAst ()).eval (vars )).isEqualTo (0L );
783862 assertThat (cel .createProgram (cel .compile ("pojo.nestedVal.value" ).getAst ()).eval (vars ))
784863 .isEqualTo ("" );
864+ assertThat (cel .createProgram (cel .compile ("size(pojo.arrayVal) == 0" ).getAst ()).eval (vars ))
865+ .isEqualTo (true );
785866 CelAbstractSyntaxTree abstractPojoAst = cel .compile ("pojo.abstractPojo.value" ).getAst ();
786867 CelRuntime .Program abstractPojoProgram = cel .createProgram (abstractPojoAst );
787868 CelEvaluationException e =
@@ -942,6 +1023,7 @@ public String get() {
9421023 public double doubleVal ;
9431024 public float floatVal ;
9441025 public byte [] bytesVal ;
1026+ public String [] arrayVal ;
9451027 public Duration durationVal ;
9461028 public Instant timestampVal ;
9471029 public TestNestedType nestedVal ;
@@ -1259,7 +1341,12 @@ public static class TestWildcardPojo {
12591341 }
12601342
12611343 public static class TestArrayPojo {
1262- public String [] values ;
1344+ public String [] strings ;
1345+ public int [] ints ;
1346+ public TestNestedType [] nesteds ;
1347+ public int [][] matrix ;
1348+ public TestNestedType [][] nestedMatrix ;
1349+ public byte [][] byteArrays ;
12631350 }
12641351
12651352 public static class TestOptionalUrlPojo {
0 commit comments