org.projectnessie.cel.tools.ScriptHost Maven / Gradle / Ivy
The newest version!
/*
* Copyright (C) 2021 The Authors of CEL-Java
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.projectnessie.cel.tools;
import static java.util.Arrays.asList;
import static org.projectnessie.cel.Env.newCustomEnv;
import static org.projectnessie.cel.EnvOption.container;
import static org.projectnessie.cel.EnvOption.declarations;
import static org.projectnessie.cel.EnvOption.types;
import static org.projectnessie.cel.EvalOption.OptOptimize;
import static org.projectnessie.cel.Library.StdLib;
import static org.projectnessie.cel.ProgramOption.evalOptions;
import com.google.api.expr.v1alpha1.Decl;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import org.projectnessie.cel.Ast;
import org.projectnessie.cel.Env;
import org.projectnessie.cel.Env.AstIssuesTuple;
import org.projectnessie.cel.EnvOption;
import org.projectnessie.cel.EvalOption;
import org.projectnessie.cel.Library;
import org.projectnessie.cel.Program;
import org.projectnessie.cel.ProgramOption;
import org.projectnessie.cel.common.types.pb.ProtoTypeRegistry;
import org.projectnessie.cel.common.types.ref.TypeRegistry;
/**
* Manages {@link Script} instances, works like a factory to generate reusable scripts.
*
* The current implementation is rather dumb, but it might be extended in the future to cache
* {@link Script} instances returned by {@link #getOrCreateScript(String, List, List)}.
*/
public final class ScriptHost {
private final boolean disableOptimize;
private final TypeRegistry registry;
private ScriptHost(boolean disableOptimize, TypeRegistry registry) {
this.disableOptimize = disableOptimize;
this.registry = registry;
}
/** Use {@link #buildScript(String)}. */
@Deprecated
public Script getOrCreateScript(String sourceText, List declarations, List