Hello Scott,

Scott Gray <scott.g...@hotwaxsystems.com> writes:

> I think it relates to the "context" variable that is frequently used in
> groovy data prep scripts for the script output.
>
> I'm not in front of a computer, but looking at it in that light may help.

As shown by this snippet from ‘GroovyUtil.java’, the “context” binding
is added explicitly so it doesn't require the ‘MapStack’ special case
for the "context" key.  In fact since the context is copied to an
‘HashMap’ before being passed to Groovy, the ‘MapStack’ implementation
is not used at all from Groovy.

--8<---------------cut here---------------start------------->8---
    public static Binding getBinding(Map<String, Object> context, String 
expression) {
        Map<String, Object> vars = new HashMap<>();
        if (context != null) {
            vars.putAll(context);
            if (UtilValidate.isNotEmpty(expression)) {
              ...;
            }
            vars.put("context", context);
            ...;
        }
        return new Binding(vars);
    }

    public static Object runScriptAtLocation(String location, String 
methodName, Map<String, Object> context) throws GeneralException {
        Script script = 
InvokerHelper.createScript(getScriptClassFromLocation(location), 
getBinding(context));
        Object result = null;
        if (UtilValidate.isEmpty(methodName)) {
            result = script.run();
        } else {
            result = script.invokeMethod(methodName, new Object[] { context });
        }
        return result;
    }
--8<---------------cut here---------------end--------------->8---

What is nice is that the ‘getBinding’ method has a javadoc explaining
why the “context” binding is added explicitly.  Thanks to Adrian Crum
(RIP) for providing it.

--8<---------------cut here---------------start------------->8---
The ‘context’ Map is added to the ‘Binding’ as a variable called
"context" so that variables can be passed back to the caller.
--8<---------------cut here---------------end--------------->8---

Thanks for your input.

-- 
Mathieu Lirzin
GPG: F2A3 8D7E EB2B 6640 5761  070D 0ADE E100 9460 4D37

Reply via email to