variable node should be a struct

master
Jordan Orelli 10 years ago
parent 239b63b3d6
commit 16f02b80ef

@ -412,7 +412,9 @@ func (o *objectNode) eval(ctx map[string]interface{}) (interface{}, error) {
return out, nil return out, nil
} }
type variableNode string type variableNode struct {
name string
}
func (v *variableNode) Type() nodeType { func (v *variableNode) Type() nodeType {
return n_variable return n_variable
@ -423,20 +425,24 @@ func (v *variableNode) parse(p *parser) error {
if t.t != t_name { if t.t != t_name {
return fmt.Errorf("unexpected %s token when parsing variable", t.t) return fmt.Errorf("unexpected %s token when parsing variable", t.t)
} }
*v = variableNode(t.s) v.name = t.s
return nil return nil
} }
func (v *variableNode) pretty(w io.Writer, prefix string) error { func (v *variableNode) pretty(w io.Writer, prefix string) error {
fmt.Fprintf(w, "%svariable:\n", prefix) fmt.Fprintf(w, "%svariable:\n", prefix)
fmt.Fprintf(w, "%s%s\n", prefix+indent, string(*v)) fmt.Fprintf(w, "%s%s\n", prefix+indent, v.name)
return nil return nil
} }
func (v *variableNode) eval(ctx map[string]interface{}) (interface{}, error) { func (v *variableNode) eval(ctx map[string]interface{}) (interface{}, error) {
value, ok := ctx[string(*v)] value, ok := ctx[v.name]
if !ok { if !ok {
return nil, fmt.Errorf("undefined variable: %s", *v) return nil, fmt.Errorf("undefined variable: %s", *v)
} }
return value, nil return value, nil
} }
func (v *variableNode) isHidden() bool {
return strings.HasPrefix(v.name, ".")
}

Loading…
Cancel
Save