Use map[string]any for vars
This commit is contained in:
		
							
								
								
									
										20
									
								
								namespace.go
									
									
									
									
									
								
							
							
						
						
									
										20
									
								
								namespace.go
									
									
									
									
									
								
							| @@ -18,16 +18,13 @@ | ||||
|  | ||||
| package salix | ||||
|  | ||||
| import ( | ||||
| 	"reflect" | ||||
| 	"sync" | ||||
| ) | ||||
| import "sync" | ||||
|  | ||||
| // Namespace represents a collection of templates that can include each other | ||||
| type Namespace struct { | ||||
| 	mu    sync.Mutex | ||||
| 	tmpls map[string]Template | ||||
| 	vars  map[string]reflect.Value | ||||
| 	vars  map[string]any | ||||
| 	tags  map[string]Tag | ||||
|  | ||||
| 	escapeHTML *bool | ||||
| @@ -37,7 +34,7 @@ type Namespace struct { | ||||
| func New() *Namespace { | ||||
| 	return &Namespace{ | ||||
| 		tmpls: map[string]Template{}, | ||||
| 		vars:  map[string]reflect.Value{}, | ||||
| 		vars:  map[string]any{}, | ||||
| 		tags:  map[string]Tag{}, | ||||
| 	} | ||||
| } | ||||
| @@ -46,14 +43,7 @@ func New() *Namespace { | ||||
| func (n *Namespace) WithVarMap(m map[string]any) *Namespace { | ||||
| 	n.mu.Lock() | ||||
| 	defer n.mu.Unlock() | ||||
|  | ||||
| 	n.vars = map[string]reflect.Value{} | ||||
| 	if m != nil { | ||||
| 		for k, v := range m { | ||||
| 			n.vars[k] = reflect.ValueOf(v) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	n.vars = m | ||||
| 	return n | ||||
| } | ||||
|  | ||||
| @@ -90,7 +80,7 @@ func (n *Namespace) GetTemplate(name string) (Template, bool) { | ||||
| } | ||||
|  | ||||
| // getVar tries to get a variable from the namespace's variable map | ||||
| func (n *Namespace) getVar(name string) (reflect.Value, bool) { | ||||
| func (n *Namespace) getVar(name string) (any, bool) { | ||||
| 	n.mu.Lock() | ||||
| 	defer n.mu.Unlock() | ||||
| 	v, ok := n.vars[name] | ||||
|   | ||||
							
								
								
									
										3
									
								
								parse.go
									
									
									
									
									
								
							
							
						
						
									
										3
									
								
								parse.go
									
									
									
									
									
								
							| @@ -24,7 +24,6 @@ import ( | ||||
| 	"io/fs" | ||||
| 	"os" | ||||
| 	"path/filepath" | ||||
| 	"reflect" | ||||
| 	"strings" | ||||
|  | ||||
| 	"go.elara.ws/salix/ast" | ||||
| @@ -55,7 +54,7 @@ func (n *Namespace) ParseWithName(name string, r io.Reader) (Template, error) { | ||||
| 		name: name, | ||||
| 		ast:  astVal.([]ast.Node), | ||||
| 		tags: map[string]Tag{}, | ||||
| 		vars: map[string]reflect.Value{}, | ||||
| 		vars: map[string]any{}, | ||||
| 	} | ||||
|  | ||||
| 	performWhitespaceMutations(t.ast) | ||||
|   | ||||
							
								
								
									
										19
									
								
								salix.go
									
									
									
									
									
								
							
							
						
						
									
										19
									
								
								salix.go
									
									
									
									
									
								
							| @@ -61,18 +61,13 @@ type Template struct { | ||||
| 	escapeHTML *bool | ||||
|  | ||||
| 	tags   map[string]Tag | ||||
| 	vars   map[string]reflect.Value | ||||
| 	vars   map[string]any | ||||
| 	macros map[string][]ast.Node | ||||
| } | ||||
|  | ||||
| // WithVarMap returns a copy of the template with its variable map set to m. | ||||
| func (t Template) WithVarMap(m map[string]any) Template { | ||||
| 	t.vars = map[string]reflect.Value{} | ||||
| 	if m != nil { | ||||
| 		for k, v := range m { | ||||
| 			t.vars[k] = reflect.ValueOf(v) | ||||
| 		} | ||||
| 	} | ||||
| 	t.vars = m | ||||
| 	return t | ||||
| } | ||||
|  | ||||
| @@ -202,7 +197,7 @@ func (t *Template) getValue(node ast.Node, local map[string]any) (any, error) { | ||||
| 		if err != nil { | ||||
| 			return nil, err | ||||
| 		} | ||||
| 		return val.Interface(), nil | ||||
| 		return val, nil | ||||
| 	case ast.String: | ||||
| 		return node.Value, nil | ||||
| 	case ast.Float: | ||||
| @@ -253,11 +248,11 @@ func (t *Template) unwrapASTValue(node ast.Value, local map[string]any) (any, er | ||||
| // getVar tries to get a variable from the local map. If it's not found, | ||||
| // it'll try the global variable map. If it doesn't exist in either map, | ||||
| // it will return an error. | ||||
| func (t *Template) getVar(id ast.Ident, local map[string]any) (reflect.Value, error) { | ||||
| func (t *Template) getVar(id ast.Ident, local map[string]any) (any, error) { | ||||
| 	if local != nil { | ||||
| 		v, ok := local[id.Value] | ||||
| 		if ok { | ||||
| 			return reflect.ValueOf(v), nil | ||||
| 			return v, nil | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| @@ -327,7 +322,7 @@ func (t *Template) execFuncCall(fc ast.FuncCall, local map[string]any) (any, err | ||||
| 	if err != nil { | ||||
| 		return nil, t.posError(fc, "%w: %s", ErrNoSuchFunc, fc.Name.Value) | ||||
| 	} | ||||
| 	return t.execFunc(fn, fc, fc.Params, local) | ||||
| 	return t.execFunc(reflect.ValueOf(fn), fc, fc.Params, local) | ||||
| } | ||||
|  | ||||
| // getIndex tries to evaluate an ast.Index node by indexing the underlying value. | ||||
| @@ -471,7 +466,7 @@ func (t *Template) evalVariableOr(vo ast.VariableOr, local map[string]any) (any, | ||||
| 	if err != nil { | ||||
| 		return t.getValue(vo.Or, local) | ||||
| 	} | ||||
| 	return val.Interface(), nil | ||||
| 	return val, nil | ||||
| } | ||||
|  | ||||
| func (t *Template) handleAssignment(a ast.Assignment, local map[string]any) error { | ||||
|   | ||||
							
								
								
									
										26
									
								
								vars.go
									
									
									
									
									
								
							
							
						
						
									
										26
									
								
								vars.go
									
									
									
									
									
								
							| @@ -23,19 +23,19 @@ import ( | ||||
| 	"strings" | ||||
| ) | ||||
|  | ||||
| var globalVars = map[string]reflect.Value{ | ||||
| 	"len":        reflect.ValueOf(tmplLen), | ||||
| 	"toUpper":    reflect.ValueOf(strings.ToUpper), | ||||
| 	"toLower":    reflect.ValueOf(strings.ToLower), | ||||
| 	"hasPrefix":  reflect.ValueOf(strings.HasPrefix), | ||||
| 	"trimPrefix": reflect.ValueOf(strings.TrimPrefix), | ||||
| 	"hasSuffix":  reflect.ValueOf(strings.HasSuffix), | ||||
| 	"trimSuffix": reflect.ValueOf(strings.TrimSuffix), | ||||
| 	"trimSpace":  reflect.ValueOf(strings.TrimSpace), | ||||
| 	"equalFold":  reflect.ValueOf(strings.EqualFold), | ||||
| 	"count":      reflect.ValueOf(strings.Count), | ||||
| 	"split":      reflect.ValueOf(strings.Split), | ||||
| 	"join":       reflect.ValueOf(strings.Join), | ||||
| var globalVars = map[string]any{ | ||||
| 	"len":        tmplLen, | ||||
| 	"toUpper":    strings.ToUpper, | ||||
| 	"toLower":    strings.ToLower, | ||||
| 	"hasPrefix":  strings.HasPrefix, | ||||
| 	"trimPrefix": strings.TrimPrefix, | ||||
| 	"hasSuffix":  strings.HasSuffix, | ||||
| 	"trimSuffix": strings.TrimSuffix, | ||||
| 	"trimSpace":  strings.TrimSpace, | ||||
| 	"equalFold":  strings.EqualFold, | ||||
| 	"count":      strings.Count, | ||||
| 	"split":      strings.Split, | ||||
| 	"join":       strings.Join, | ||||
| } | ||||
|  | ||||
| func tmplLen(v any) int { | ||||
|   | ||||
		Reference in New Issue
	
	Block a user