1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 19:24:45 +01:00

Actually cache the drop resolution

This commit is contained in:
Oliver Steele 2017-08-03 15:11:34 +02:00
parent 7f501ce512
commit 83652f59db
2 changed files with 15 additions and 22 deletions

View File

@ -1,6 +1,8 @@
package values
import "sync"
import (
"sync"
)
type drop interface {
ToLiquid() interface{}
@ -16,31 +18,22 @@ func ToLiquid(value interface{}) interface{} {
}
}
// embed this in a struct to give it default implementations of the Value interface
type dropWrapper struct {
d drop
v Value
sync.Once
}
// Single mutex, until proven finer granularity helps.
var dropResolverMu sync.Mutex
func (w dropWrapper) Resolve() Value {
if w.v == nil {
dropResolverMu.Lock()
defer dropResolverMu.Unlock()
if w.v == nil {
w.v = ValueOf(w.d.ToLiquid())
}
}
func (w *dropWrapper) Resolve() Value {
w.Do(func() { w.v = ValueOf(w.d.ToLiquid()) })
return w.v
}
func (w dropWrapper) Equal(o Value) bool { return w.Resolve().Equal(o) }
func (w dropWrapper) Less(o Value) bool { return w.Resolve().Less(o) }
func (w dropWrapper) IndexValue(i Value) Value { return w.Resolve().IndexValue(i) }
func (w dropWrapper) Contains(o Value) bool { return w.Resolve().Contains(o) }
func (w dropWrapper) Int() int { return w.Resolve().Int() }
func (w dropWrapper) Interface() interface{} { return w.Resolve().Interface() }
func (w dropWrapper) PropertyValue(k Value) Value { return w.Resolve().PropertyValue(k) }
func (w dropWrapper) Test() bool { return w.Resolve().Test() }
func (w *dropWrapper) Equal(o Value) bool { return w.Resolve().Equal(o) }
func (w *dropWrapper) Less(o Value) bool { return w.Resolve().Less(o) }
func (w *dropWrapper) IndexValue(i Value) Value { return w.Resolve().IndexValue(i) }
func (w *dropWrapper) Contains(o Value) bool { return w.Resolve().Contains(o) }
func (w *dropWrapper) Int() int { return w.Resolve().Int() }
func (w *dropWrapper) Interface() interface{} { return w.Resolve().Interface() }
func (w *dropWrapper) PropertyValue(k Value) Value { return w.Resolve().PropertyValue(k) }
func (w *dropWrapper) Test() bool { return w.Resolve().Test() }

View File

@ -41,7 +41,7 @@ func ValueOf(value interface{}) Value { // nolint: gocyclo
// interfaces
switch v := value.(type) {
case drop:
return dropWrapper{d: v}
return &dropWrapper{d: v}
case yaml.MapSlice:
return mapSliceValue{slice: v}
case Value: