Search in sources :

Example 1 with IdentDecl

use of com.google.api.expr.v1alpha1.Decl.IdentDecl in project cel-java by projectnessie.

the class Checker method checkCreateMessage.

void checkCreateMessage(Expr.Builder e) {
    CreateStruct.Builder msgVal = e.getStructExprBuilder();
    // Determine the type of the message.
    Type messageType = Decls.Error;
    Decl decl = env.lookupIdent(msgVal.getMessageName());
    if (decl == null) {
        errors.undeclaredReference(location(e), env.container.name(), msgVal.getMessageName());
        return;
    }
    // Ensure the type name is fully qualified in the AST.
    msgVal.setMessageName(decl.getName());
    setReference(e, newIdentReference(decl.getName(), null));
    IdentDecl ident = decl.getIdent();
    Types.Kind identKind = kindOf(ident.getType());
    if (identKind != Kind.kindError) {
        if (identKind != Kind.kindType) {
            errors.notAType(location(e), ident.getType());
        } else {
            messageType = ident.getType().getType();
            if (kindOf(messageType) != Kind.kindObject) {
                errors.notAMessageType(location(e), messageType);
                messageType = Decls.Error;
            }
        }
    }
    if (isObjectWellKnownType(messageType)) {
        setType(e, getObjectWellKnownType(messageType));
    } else {
        setType(e, messageType);
    }
    // Check the field initializers.
    for (Entry.Builder ent : msgVal.getEntriesBuilderList()) {
        String field = ent.getFieldKey();
        Expr.Builder value = ent.getValueBuilder();
        check(value);
        Type fieldType = Decls.Error;
        FieldType t = lookupFieldType(locationByID(ent.getId()), messageType.getMessageType(), field);
        if (t != null) {
            fieldType = t.type;
        }
        if (!isAssignable(fieldType, getType(value))) {
            errors.fieldTypeMismatch(locationByID(ent.getId()), field, fieldType, getType(value));
        }
    }
}
Also used : CreateStruct(com.google.api.expr.v1alpha1.Expr.CreateStruct) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl) Decl(com.google.api.expr.v1alpha1.Decl) FieldType(org.projectnessie.cel.common.types.ref.FieldType) CheckerEnv.getObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType) CheckerEnv.isObjectWellKnownType(org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType) CheckerEnv.dynElementType(org.projectnessie.cel.checker.CheckerEnv.dynElementType) Type(com.google.api.expr.v1alpha1.Type) MapType(com.google.api.expr.v1alpha1.Type.MapType) FieldType(org.projectnessie.cel.common.types.ref.FieldType) Entry(com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry) Expr(com.google.api.expr.v1alpha1.Expr) CheckedExpr(com.google.api.expr.v1alpha1.CheckedExpr) Kind(org.projectnessie.cel.checker.Types.Kind) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl)

Example 2 with IdentDecl

use of com.google.api.expr.v1alpha1.Decl.IdentDecl in project cel-java by projectnessie.

the class CheckerEnv method sanitizeIdent.

/**
 * sanitizeIdent replaces the identifier's well-known types referenced by message name with
 * references to CEL built-in type instances.
 */
Decl sanitizeIdent(Decl decl) {
    IdentDecl id = decl.getIdent();
    Type t = id.getType();
    if (!isObjectWellKnownType(t)) {
        return decl;
    }
    return Decls.newIdent(decl.getName(), getObjectWellKnownType(t), id.getValue());
}
Also used : Types.formatCheckedType(org.projectnessie.cel.checker.Types.formatCheckedType) Type(com.google.api.expr.v1alpha1.Type) ErrType(org.projectnessie.cel.common.types.Err.ErrType) IdentDecl(com.google.api.expr.v1alpha1.Decl.IdentDecl)

Aggregations

IdentDecl (com.google.api.expr.v1alpha1.Decl.IdentDecl)2 Type (com.google.api.expr.v1alpha1.Type)2 CheckedExpr (com.google.api.expr.v1alpha1.CheckedExpr)1 Decl (com.google.api.expr.v1alpha1.Decl)1 Expr (com.google.api.expr.v1alpha1.Expr)1 CreateStruct (com.google.api.expr.v1alpha1.Expr.CreateStruct)1 Entry (com.google.api.expr.v1alpha1.Expr.CreateStruct.Entry)1 MapType (com.google.api.expr.v1alpha1.Type.MapType)1 CheckerEnv.dynElementType (org.projectnessie.cel.checker.CheckerEnv.dynElementType)1 CheckerEnv.getObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.getObjectWellKnownType)1 CheckerEnv.isObjectWellKnownType (org.projectnessie.cel.checker.CheckerEnv.isObjectWellKnownType)1 Kind (org.projectnessie.cel.checker.Types.Kind)1 Types.formatCheckedType (org.projectnessie.cel.checker.Types.formatCheckedType)1 ErrType (org.projectnessie.cel.common.types.Err.ErrType)1 FieldType (org.projectnessie.cel.common.types.ref.FieldType)1