From 54e6b83fcdbc844ffe546160562ac4094c904857 Mon Sep 17 00:00:00 2001 From: Qi Xiao Date: Mon, 22 Feb 2016 15:35:32 +0100 Subject: [PATCH] Clean up namespace resolution code. --- eval/eval.go | 36 ++++++++++++------------------------ 1 file changed, 12 insertions(+), 24 deletions(-) diff --git a/eval/eval.go b/eval/eval.go index 9f2bab73..efc03cc1 100644 --- a/eval/eval.go +++ b/eval/eval.go @@ -299,35 +299,23 @@ func (ev *Evaler) Global() map[string]Variable { // ResolveVar resolves a variable. When the variable cannot be found, nil is // returned. func (ec *EvalCtx) ResolveVar(ns, name string) Variable { - if ns == "env" || ns == "external" || ns == "E" || ns == "e" { + switch ns { + case "local": + return ec.local[name] + case "up": + return ec.up[name] + case "": + if v, ok := ec.local[name]; ok { + return v + } + return ec.up[name] + case "env", "external", "e", "E": if strings.HasPrefix(name, FnPrefix) { return NewRoVariable(ExternalCmd{name[len(FnPrefix):]}) } return envVariable{name} - } - - if ns == "" || ns == "local" { - if v, ok := ec.local[name]; ok { - return v - } - if ns == "local" { - return nil - } - } - - if ns == "" || ns == "up" { - if v, ok := ec.up[name]; ok { - return v - } - if ns == "up" { - return nil - } - } - - // External module. - if ns != "" { + default: use(ec, ns, nil) return ec.modules[ns][name] } - return nil }