use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesConditionalAttr_TrueBranch.
@Test
void attributesConditionalAttr_TrueBranch() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", mapOf("c", mapOf(-1, new int[] { 2, 42 })));
Activation vars = newActivation(data);
// (true ? a : b.c)[-1][1]
NamespacedAttribute tv = attrs.absoluteAttribute(2, "a");
Attribute fv = attrs.maybeAttribute(3, "b");
Qualifier qualC = attrs.newQualifier(null, 4, "c");
fv.addQualifier(qualC);
Attribute cond = attrs.conditionalAttribute(1, newConstValue(0, True), tv, fv);
Qualifier qualNeg1 = attrs.newQualifier(null, 5, intOf(-1));
Qualifier qual1 = attrs.newQualifier(null, 6, intOf(1));
cond.addQualifier(qualNeg1);
cond.addQualifier(qual1);
Object out = cond.resolve(vars);
assertThat(out).isEqualTo(42);
assertThat(estimateCost(fv)).extracting("min", "max").containsExactly(1L, 1L);
// Note: migrated to JMH
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method benchmarkResolverCustomQualifier.
@Test
void benchmarkResolverCustomQualifier() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = new CustAttrFactory(newAttributeFactory(Container.defaultContainer, reg, reg));
NestedMessage msg = NestedMessage.newBuilder().setBb(123).build();
Activation vars = newActivation(mapOf("msg", msg));
NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
Qualifier qualBB = attrs.newQualifier(Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage").build(), 2, "bb");
attr.addQualifier(qualBB);
// Note: Migrated to JMH
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesMissingMsg.
@Test
void attributesMissingMsg() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Any any = Any.pack(TestAllTypes.getDefaultInstance());
Activation vars = newActivation(mapOf("missing_msg", any));
// missing_msg.field
NamespacedAttribute attr = attrs.absoluteAttribute(1, "missing_msg");
Qualifier field = attrs.newQualifier(null, 2, "field");
attr.addQualifier(field);
assertThatThrownBy(() -> attr.resolve(vars)).isInstanceOf(IllegalStateException.class).hasMessage("unknown type 'google.api.expr.test.v1.proto3.TestAllTypes'");
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method attributesRelativeAttr_Conditional.
@Test
void attributesRelativeAttr_Conditional() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
Map<Object, Object> data = mapOf("a", mapOf(-1, new int[] { 2, 42 }), "b", new int[] { 0, 1 }, "c", new Object[] { 1, 0 });
Activation vars = newActivation(data);
// The relative attribute under test is applied to a map literal:
// {
// a: {-1: [2, 42], b: 1}
// b: [0, 1],
// c: {1, 0},
// }
//
// The expression being evaluated is:
// <map-literal>.a[-1][(false ? b : c)[0]] -> 42
//
// Effectively the same as saying <map-literal>.a[-1][c[0]]
InterpretableConst cond = newConstValue(2, False);
Attribute condAttr = attrs.conditionalAttribute(4, cond, attrs.absoluteAttribute(5, "b"), attrs.absoluteAttribute(6, "c"));
Qualifier qual0 = attrs.newQualifier(null, 7, 0);
condAttr.addQualifier(qual0);
InterpretableConst obj = newConstValue(1, reg.nativeToValue(data));
Attribute attr = attrs.relativeAttribute(1, obj);
Qualifier qualA = attrs.newQualifier(null, 2, "a");
Qualifier qualNeg1 = attrs.newQualifier(null, 3, intOf(-1));
attr.addQualifier(qualA);
attr.addQualifier(qualNeg1);
attr.addQualifier(condAttr);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(intOf(42));
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
use of org.projectnessie.cel.common.types.ref.TypeRegistry in project cel-java by projectnessie.
the class AttributesTest method resolverCustomQualifier.
@Test
void resolverCustomQualifier() {
TypeRegistry reg = newRegistry();
AttributeFactory attrs = new CustAttrFactory(newAttributeFactory(Container.defaultContainer, reg, reg));
NestedMessage msg = NestedMessage.newBuilder().setBb(123).build();
Activation vars = newActivation(mapOf("msg", msg));
NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
Qualifier qualBB = attrs.newQualifier(Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage").build(), 2, "bb");
attr.addQualifier(qualBB);
Object out = attr.resolve(vars);
assertThat(out).isEqualTo(123);
assertThat(estimateCost(attr)).extracting("min", "max").containsExactly(1L, 1L);
}
Aggregations