Search in sources :

Example 1 with TypeAdapter

use of org.projectnessie.cel.common.types.ref.TypeAdapter in project cel-java by projectnessie.

the class ConformanceServiceImpl method valueToRefValue.

/**
 * ValueToRefValue converts between exprpb.Value and ref.Val.
 */
static Val valueToRefValue(TypeAdapter adapter, Value v) {
    switch(v.getKindCase()) {
        case NULL_VALUE:
            return NullT.NullValue;
        case BOOL_VALUE:
            return boolOf(v.getBoolValue());
        case INT64_VALUE:
            return intOf(v.getInt64Value());
        case UINT64_VALUE:
            return uintOf(v.getUint64Value());
        case DOUBLE_VALUE:
            return doubleOf(v.getDoubleValue());
        case STRING_VALUE:
            return stringOf(v.getStringValue());
        case BYTES_VALUE:
            return bytesOf(v.getBytesValue().toByteArray());
        case OBJECT_VALUE:
            Any any = v.getObjectValue();
            return adapter.nativeToValue(any);
        case MAP_VALUE:
            MapValue m = v.getMapValue();
            Map<Val, Val> entries = new HashMap<>();
            for (Entry entry : m.getEntriesList()) {
                Val key = valueToRefValue(adapter, entry.getKey());
                Val pb = valueToRefValue(adapter, entry.getValue());
                entries.put(key, pb);
            }
            return adapter.nativeToValue(entries);
        case LIST_VALUE:
            ListValue l = v.getListValue();
            List<Val> elts = l.getValuesList().stream().map(el -> valueToRefValue(adapter, el)).collect(Collectors.toList());
            return adapter.nativeToValue(elts);
        case TYPE_VALUE:
            String typeName = v.getTypeValue();
            Type tv = Types.getTypeByName(typeName);
            if (tv != null) {
                return tv;
            }
            return newObjectTypeValue(typeName);
        default:
            throw new IllegalArgumentException("unknown value " + v.getKindCase());
    }
}
Also used : Val(org.projectnessie.cel.common.types.ref.Val) MapValue(com.google.api.expr.v1alpha1.MapValue) Lister(org.projectnessie.cel.common.types.traits.Lister) EnvOption.container(org.projectnessie.cel.EnvOption.container) CheckRequest(com.google.api.expr.v1alpha1.CheckRequest) Err(org.projectnessie.cel.common.types.Err) True(org.projectnessie.cel.common.types.BoolT.True) Err.isError(org.projectnessie.cel.common.types.Err.isError) Ast(org.projectnessie.cel.Ast) CheckResponse(com.google.api.expr.v1alpha1.CheckResponse) Map(java.util.Map) Value(com.google.api.expr.v1alpha1.Value) UnknownSet(com.google.api.expr.v1alpha1.UnknownSet) Val(org.projectnessie.cel.common.types.ref.Val) PrintWriter(java.io.PrintWriter) IteratorT(org.projectnessie.cel.common.types.IteratorT) DoubleT.doubleOf(org.projectnessie.cel.common.types.DoubleT.doubleOf) Status(com.google.rpc.Status) EnvOption.clearMacros(org.projectnessie.cel.EnvOption.clearMacros) StringT.stringOf(org.projectnessie.cel.common.types.StringT.stringOf) EnvOption(org.projectnessie.cel.EnvOption) ExprValue(com.google.api.expr.v1alpha1.ExprValue) Collectors(java.util.stream.Collectors) IntT.intOf(org.projectnessie.cel.common.types.IntT.intOf) UintT.uintOf(org.projectnessie.cel.common.types.UintT.uintOf) ByteString(com.google.protobuf.ByteString) List(java.util.List) ErrorSet(com.google.api.expr.v1alpha1.ErrorSet) UnknownT.unknownOf(org.projectnessie.cel.common.types.UnknownT.unknownOf) Type(org.projectnessie.cel.common.types.ref.Type) Any(com.google.protobuf.Any) EvalResult(org.projectnessie.cel.Program.EvalResult) ParseRequest(com.google.api.expr.v1alpha1.ParseRequest) Err.newErr(org.projectnessie.cel.common.types.Err.newErr) Env.newEnv(org.projectnessie.cel.Env.newEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) ParseResponse(com.google.api.expr.v1alpha1.ParseResponse) StdLib(org.projectnessie.cel.Library.StdLib) EnvOption.declarations(org.projectnessie.cel.EnvOption.declarations) ConformanceServiceImplBase(com.google.api.expr.v1alpha1.ConformanceServiceGrpc.ConformanceServiceImplBase) HashMap(java.util.HashMap) Timestamp(com.google.protobuf.Timestamp) Supplier(java.util.function.Supplier) ArrayList(java.util.ArrayList) AstIssuesTuple(org.projectnessie.cel.Env.AstIssuesTuple) Mapper(org.projectnessie.cel.common.types.traits.Mapper) Types(org.projectnessie.cel.common.types.Types) IssueDetails(com.google.api.expr.v1alpha1.IssueDetails) CEL.astToCheckedExpr(org.projectnessie.cel.CEL.astToCheckedExpr) EvalRequest(com.google.api.expr.v1alpha1.EvalRequest) Code(com.google.rpc.Code) StringWriter(java.io.StringWriter) EnvOption.types(org.projectnessie.cel.EnvOption.types) NullT(org.projectnessie.cel.common.types.NullT) Types.boolOf(org.projectnessie.cel.common.types.Types.boolOf) TypeAdapter(org.projectnessie.cel.common.types.ref.TypeAdapter) UnknownT.isUnknown(org.projectnessie.cel.common.types.UnknownT.isUnknown) ListValue(com.google.api.expr.v1alpha1.ListValue) CEL.parsedExprToAst(org.projectnessie.cel.CEL.parsedExprToAst) BytesT.bytesOf(org.projectnessie.cel.common.types.BytesT.bytesOf) CELError(org.projectnessie.cel.common.CELError) SourcePosition(com.google.api.expr.v1alpha1.SourcePosition) TypeT(org.projectnessie.cel.common.types.TypeT) Duration(com.google.protobuf.Duration) CEL.astToParsedExpr(org.projectnessie.cel.CEL.astToParsedExpr) CEL.checkedExprToAst(org.projectnessie.cel.CEL.checkedExprToAst) Program(org.projectnessie.cel.Program) EvalResponse(com.google.api.expr.v1alpha1.EvalResponse) TypeT.newObjectTypeValue(org.projectnessie.cel.common.types.TypeT.newObjectTypeValue) Message(com.google.protobuf.Message) Env(org.projectnessie.cel.Env) Env.newCustomEnv(org.projectnessie.cel.Env.newCustomEnv) Entry(com.google.api.expr.v1alpha1.MapValue.Entry) Type(org.projectnessie.cel.common.types.ref.Type) HashMap(java.util.HashMap) ListValue(com.google.api.expr.v1alpha1.ListValue) MapValue(com.google.api.expr.v1alpha1.MapValue) ByteString(com.google.protobuf.ByteString) Any(com.google.protobuf.Any)

Example 2 with TypeAdapter

use of org.projectnessie.cel.common.types.ref.TypeAdapter in project cel-java by projectnessie.

the class Env method extend.

/**
 * Extend the current environment with additional options to produce a new Env.
 *
 * <p>Note, the extended Env value should not share memory with the original. It is possible,
 * however, that a CustomTypeAdapter or CustomTypeProvider options could provide values which are
 * mutable. To ensure separation of state between extended environments either make sure the
 * TypeAdapter and TypeProvider are immutable, or that their underlying implementations are based
 * on the ref.TypeRegistry which provides a Copy method which will be invoked by this method.
 */
public Env extend(List<EnvOption> opts) {
    if (chkErr != null) {
        throw chkErr;
    }
    // Copy slices.
    List<Decl> decsCopy = new ArrayList<>(declarations);
    List<Macro> macsCopy = new ArrayList<>(macros);
    List<ProgramOption> progOptsCopy = new ArrayList<>(progOpts);
    // Copy the adapter / provider if they appear to be mutable.
    TypeAdapter adapter = this.adapter;
    TypeProvider provider = this.provider;
    // TypeRegistry as the base implementation are captured below.
    if (this.adapter instanceof TypeRegistry && this.provider instanceof TypeRegistry) {
        TypeRegistry adapterReg = (TypeRegistry) this.adapter;
        TypeRegistry providerReg = (TypeRegistry) this.provider;
        TypeRegistry reg = providerReg.copy();
        provider = reg;
        // to the same ref.TypeRegistry as the provider.
        if (adapterReg.equals(providerReg)) {
            adapter = reg;
        } else {
            // Otherwise, make a copy of the adapter.
            adapter = adapterReg.copy();
        }
    } else if (this.provider instanceof TypeRegistry) {
        provider = ((TypeRegistry) this.provider).copy();
    } else if (this.adapter instanceof TypeRegistry) {
        adapter = ((TypeRegistry) this.adapter).copy();
    }
    Set<EnvFeature> featuresCopy = EnumSet.copyOf(this.features);
    Env ext = new Env(this.container, decsCopy, macsCopy, adapter, provider, featuresCopy, progOptsCopy);
    return ext.configure(opts);
}
Also used : Macro(org.projectnessie.cel.parser.Macro) ArrayList(java.util.ArrayList) TypeProvider(org.projectnessie.cel.common.types.ref.TypeProvider) Decl(com.google.api.expr.v1alpha1.Decl) CheckerEnv(org.projectnessie.cel.checker.CheckerEnv) TypeRegistry(org.projectnessie.cel.common.types.ref.TypeRegistry) EnvFeature(org.projectnessie.cel.EnvOption.EnvFeature) TypeAdapter(org.projectnessie.cel.common.types.ref.TypeAdapter)

Aggregations

ArrayList (java.util.ArrayList)2 TypeAdapter (org.projectnessie.cel.common.types.ref.TypeAdapter)2 CheckRequest (com.google.api.expr.v1alpha1.CheckRequest)1 CheckResponse (com.google.api.expr.v1alpha1.CheckResponse)1 ConformanceServiceImplBase (com.google.api.expr.v1alpha1.ConformanceServiceGrpc.ConformanceServiceImplBase)1 Decl (com.google.api.expr.v1alpha1.Decl)1 ErrorSet (com.google.api.expr.v1alpha1.ErrorSet)1 EvalRequest (com.google.api.expr.v1alpha1.EvalRequest)1 EvalResponse (com.google.api.expr.v1alpha1.EvalResponse)1 ExprValue (com.google.api.expr.v1alpha1.ExprValue)1 IssueDetails (com.google.api.expr.v1alpha1.IssueDetails)1 ListValue (com.google.api.expr.v1alpha1.ListValue)1 MapValue (com.google.api.expr.v1alpha1.MapValue)1 Entry (com.google.api.expr.v1alpha1.MapValue.Entry)1 ParseRequest (com.google.api.expr.v1alpha1.ParseRequest)1 ParseResponse (com.google.api.expr.v1alpha1.ParseResponse)1 SourcePosition (com.google.api.expr.v1alpha1.SourcePosition)1 UnknownSet (com.google.api.expr.v1alpha1.UnknownSet)1 Value (com.google.api.expr.v1alpha1.Value)1 Any (com.google.protobuf.Any)1