Search in sources :

Example 1 with TestAllTypes

use of com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes in project cel-java by projectnessie.

the class AttributesTest method benchmarkResolverFieldQualifier.

@Test
void benchmarkResolverFieldQualifier() {
    TestAllTypes msg = TestAllTypes.newBuilder().setSingleNestedMessage(NestedMessage.newBuilder().setBb(123)).build();
    TypeRegistry reg = newRegistry(msg);
    AttributeFactory attrs = newAttributeFactory(Container.defaultContainer, reg, reg);
    Activation vars = newActivation(mapOf("msg", msg));
    NamespacedAttribute attr = attrs.absoluteAttribute(1, "msg");
    Type opType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes");
    assertThat(opType).isNotNull();
    Type fieldType = reg.findType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage");
    assertThat(fieldType).isNotNull();
    attr.addQualifier(makeQualifier(attrs, opType.getType(), 2, "single_nested_message"));
    attr.addQualifier(makeQualifier(attrs, fieldType.getType(), 3, "bb"));
// Note: migrated to JMH
}
Also used : IntType(org.projectnessie.cel.common.types.IntT.IntType) Type(com.google.api.expr.v1alpha1.Type) NamespacedAttribute(org.projectnessie.cel.interpreter.AttributeFactory.NamespacedAttribute) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 2 with TestAllTypes

use of com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes in project cel-java by projectnessie.

the class InterpreterTest method setProto2PrimitiveFields.

@Test
void setProto2PrimitiveFields() {
    // Test the use of proto2 primitives within object construction.
    Source src = newTextSource("input == TestAllTypes{\n" + "  single_int32: 1,\n" + "  single_int64: 2,\n" + "  single_uint32: 3u,\n" + "  single_uint64: 4u,\n" + "  single_float: -3.3,\n" + "  single_double: -2.2,\n" + "  single_string: \"hello world\",\n" + "  single_bool: true\n" + "}");
    ParseResult parsed = Parser.parseAllMacros(src);
    assertThat(parsed.hasErrors()).withFailMessage(parsed.getErrors()::toDisplayString).isFalse();
    Container cont = testContainer("google.api.expr.test.v1.proto2");
    TypeRegistry reg = newRegistry(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.getDefaultInstance());
    CheckerEnv env = newStandardCheckerEnv(cont, reg);
    env.add(singletonList(Decls.newVar("input", Decls.newObjectType("google.api.expr.test.v1.proto2.TestAllTypes"))));
    CheckResult checkResult = Checker.Check(parsed, src, env);
    if (parsed.hasErrors()) {
        throw new IllegalArgumentException(parsed.getErrors().toDisplayString());
    }
    AttributeFactory attrs = newAttributeFactory(cont, reg, reg);
    Interpreter i = newStandardInterpreter(cont, reg, reg, attrs);
    Interpretable eval = i.newInterpretable(checkResult.getCheckedExpr());
    int one = 1;
    long two = 2L;
    int three = 3;
    long four = 4L;
    float five = -3.3f;
    double six = -2.2d;
    String str = "hello world";
    boolean truth = true;
    com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes input = com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes.newBuilder().setSingleInt32(one).setSingleInt64(two).setSingleUint32(three).setSingleUint64(four).setSingleFloat(five).setSingleDouble(six).setSingleString(str).setSingleBool(truth).build();
    Activation vars = newActivation(mapOf("input", reg.nativeToValue(input)));
    Val result = eval.eval(vars);
    assertThat(result.value()).isInstanceOf(Boolean.class);
    boolean got = (Boolean) result.value();
    assertThat(got).isTrue();
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) TestAllTypes(com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes) Interpreter.newStandardInterpreter(org.projectnessie.cel.interpreter.Interpreter.newStandardInterpreter) Interpreter.newInterpreter(org.projectnessie.cel.interpreter.Interpreter.newInterpreter) ParseResult(org.projectnessie.cel.parser.Parser.ParseResult) AttributePattern.newPartialAttributeFactory(org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory) AttributeFactory.newAttributeFactory(org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory) Activation.newPartialActivation(org.projectnessie.cel.interpreter.Activation.newPartialActivation) Activation.emptyActivation(org.projectnessie.cel.interpreter.Activation.emptyActivation) Activation.newActivation(org.projectnessie.cel.interpreter.Activation.newActivation) ByteString(com.google.protobuf.ByteString) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) Source(org.projectnessie.cel.common.Source) MethodSource(org.junit.jupiter.params.provider.MethodSource) Source.newTextSource(org.projectnessie.cel.common.Source.newTextSource) Container.newContainer(org.projectnessie.cel.common.containers.Container.newContainer) Container(org.projectnessie.cel.common.containers.Container) CheckResult(org.projectnessie.cel.checker.Checker.CheckResult) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) CheckerEnv.newStandardCheckerEnv(org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Test(org.junit.jupiter.api.Test)

Example 3 with TestAllTypes

use of com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes in project cel-java by projectnessie.

the class FieldDescriptionTest method isSet.

@ParameterizedTest
@MethodSource("isSetTestCases")
void isSet(TestCase tc) {
    Db pbdb = newDb();
    TestAllTypes msg = TestAllTypes.getDefaultInstance();
    String msgName = msg.getDescriptorForType().getFullName();
    pbdb.registerMessage(msg);
    PbTypeDescription td = pbdb.describeType(msgName);
    assertThat(td).isNotNull();
    FieldDescription f = td.fieldByName(tc.field);
    assertThat(f).isNotNull();
    assertThat(f.isSet(tc.msg)).isEqualTo(tc.isSet);
}
Also used : TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) NestedTestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes) Db.newDb(org.projectnessie.cel.common.types.pb.Db.newDb) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 4 with TestAllTypes

use of com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes in project cel-java by projectnessie.

the class FieldDescriptionTest method fieldDescription.

@Test
void fieldDescription() {
    Db pbdb = newDb();
    NestedTestAllTypes msg = NestedTestAllTypes.getDefaultInstance();
    String msgName = msg.getDescriptorForType().getFullName();
    pbdb.registerMessage(msg);
    PbTypeDescription td = pbdb.describeType(msgName);
    assertThat(td).isNotNull();
    FieldDescription fd = td.fieldByName("payload");
    assertThat(fd).isNotNull();
    assertThat(fd).extracting(FieldDescription::name).isEqualTo("payload");
    assertThat(fd).extracting(FieldDescription::isOneof).isEqualTo(false);
    assertThat(fd).extracting(FieldDescription::isMap).isEqualTo(false);
    assertThat(fd).extracting(FieldDescription::isMessage).isEqualTo(true);
    assertThat(fd).extracting(FieldDescription::isEnum).isEqualTo(false);
    assertThat(fd).extracting(FieldDescription::isList).isEqualTo(false);
    // Access the field by its Go struct name and check to see that it's index
    // matches the one determined by the TypeDescription utils.
    Type got = fd.checkedType();
    Type wanted = Type.newBuilder().setMessageType("google.api.expr.test.v1.proto3.TestAllTypes").build();
    assertThat(got).isEqualTo(wanted);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) Db.newDb(org.projectnessie.cel.common.types.pb.Db.newDb) NestedTestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 5 with TestAllTypes

use of com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes in project cel-java by projectnessie.

the class PbTypeDescriptionTest method checkedType.

@Test
void checkedType() {
    Db pbdb = newDb();
    TestAllTypes msg = TestAllTypes.getDefaultInstance();
    String msgName = msg.getDescriptorForType().getFullName();
    pbdb.registerMessage(msg);
    PbTypeDescription td = pbdb.describeType(msgName);
    assertThat(td).isNotNull();
    FieldDescription field = td.fieldByName("map_string_string");
    assertThat(field).isNotNull();
    Type mapType = Decls.newMapType(Decls.String, Decls.String);
    assertThat(field.checkedType()).isEqualTo(mapType);
    field = td.fieldByName("repeated_nested_message");
    assertThat(field).isNotNull();
    Type listType = Decls.newListType(Decls.newObjectType("google.api.expr.test.v1.proto3.TestAllTypes.NestedMessage"));
    assertThat(field.checkedType()).isEqualTo(listType);
}
Also used : Type(com.google.api.expr.v1alpha1.Type) ByteString(com.google.protobuf.ByteString) TestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes) NestedTestAllTypes(com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes) Db.newDb(org.projectnessie.cel.common.types.pb.Db.newDb) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)6 TestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.TestAllTypes)5 Test (org.junit.jupiter.api.Test)5 NestedTestAllTypes (com.google.api.expr.test.v1.proto3.TestAllTypesProto.NestedTestAllTypes)4 Db.newDb (org.projectnessie.cel.common.types.pb.Db.newDb)4 Type (com.google.api.expr.v1alpha1.Type)3 MethodSource (org.junit.jupiter.params.provider.MethodSource)3 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)3 ByteString (com.google.protobuf.ByteString)2 Val (org.projectnessie.cel.common.types.ref.Val)2 Activation.emptyActivation (org.projectnessie.cel.interpreter.Activation.emptyActivation)2 Activation.newActivation (org.projectnessie.cel.interpreter.Activation.newActivation)2 Activation.newPartialActivation (org.projectnessie.cel.interpreter.Activation.newPartialActivation)2 AttributeFactory.newAttributeFactory (org.projectnessie.cel.interpreter.AttributeFactory.newAttributeFactory)2 AttributePattern.newPartialAttributeFactory (org.projectnessie.cel.interpreter.AttributePattern.newPartialAttributeFactory)2 TestAllTypes (com.google.api.expr.test.v1.proto2.TestAllTypesProto.TestAllTypes)1 CheckResult (org.projectnessie.cel.checker.Checker.CheckResult)1 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)1 CheckerEnv.newStandardCheckerEnv (org.projectnessie.cel.checker.CheckerEnv.newStandardCheckerEnv)1 Source (org.projectnessie.cel.common.Source)1