Search in sources :

Example 1 with EnvFeature

use of org.projectnessie.cel.EnvOption.EnvFeature 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

Decl (com.google.api.expr.v1alpha1.Decl)1 ArrayList (java.util.ArrayList)1 EnvFeature (org.projectnessie.cel.EnvOption.EnvFeature)1 CheckerEnv (org.projectnessie.cel.checker.CheckerEnv)1 TypeAdapter (org.projectnessie.cel.common.types.ref.TypeAdapter)1 TypeProvider (org.projectnessie.cel.common.types.ref.TypeProvider)1 TypeRegistry (org.projectnessie.cel.common.types.ref.TypeRegistry)1 Macro (org.projectnessie.cel.parser.Macro)1