package com.onthegomap.planetiler.custommap.expression; import com.google.api.expr.v1alpha1.Decl; import java.util.List; import java.util.stream.Stream; /** * Type definitions for the environment that a script expression runs in. * * @param types Additional types available. * @param declarations Global variable types * @param clazz Class of the input context type * @param The runtime expression context type */ public record ScriptEnvironment (List types, List declarations, Class clazz) { private static List concat(List a, T[] b) { return Stream.concat(a.stream(), Stream.of(b)).toList(); } /** Returns a copy of this environment with a new input type {@code U}. */ public ScriptEnvironment forInput(Class newClazz) { return new ScriptEnvironment<>(types, declarations, newClazz); } /** Returns a copy of this environment with a list of variable declarations appended to the global environment. */ public ScriptEnvironment withDeclarations(Decl... others) { return new ScriptEnvironment<>(types, concat(declarations, others), clazz); } /** Returns an empty environment with no variables defined. */ public static ScriptEnvironment root() { return new ScriptEnvironment<>(List.of(), List.of(), ScriptContext.class); } /** Returns true if this contains a variable declaration for {@code variable}. */ public boolean containsVariable(String variable) { return declarations().stream().anyMatch(decl -> decl.getName().equals(variable)); } @Override public String toString() { return "ScriptContextDescription{" + "declarations=" + declarations.stream().map(Decl::getName).toList() + ", clazz=" + clazz + '}'; } }