1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 21:34:44 +01:00

Value layer recognizes, resolves drops

This commit is contained in:
Oliver Steele 2017-07-21 16:26:04 -04:00
parent fa5de60b78
commit 560c55eb89
4 changed files with 40 additions and 6 deletions

View File

@ -13,3 +13,25 @@ func ToLiquid(value interface{}) interface{} {
return value
}
}
// embed this in a struct to give it default implementations of the Value interface
type dropWrapper struct {
d drop
v Value
}
func (w dropWrapper) Resolve() Value {
if w.v == nil {
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() }

View File

@ -6,11 +6,19 @@ import (
"github.com/stretchr/testify/require"
)
type testDrop struct{}
type testDrop struct{ proxy interface{} }
func (d testDrop) ToLiquid() interface{} { return 3 }
func (d testDrop) ToLiquid() interface{} { return d.proxy }
func TestToLiquid(t *testing.T) {
require.Equal(t, 2, ToLiquid(2))
require.Equal(t, 3, ToLiquid(testDrop{}))
require.Equal(t, 3, ToLiquid(testDrop{3}))
}
func TestValue_drop(t *testing.T) {
dv := ValueOf(testDrop{"seafood"})
require.Equal(t, "seafood", dv.Interface())
require.Equal(t, true, dv.Contains(ValueOf("foo")))
require.Equal(t, true, dv.Contains(ValueOf(testDrop{"foo"})))
require.Equal(t, 7, dv.PropertyValue(ValueOf("size")).Interface())
}

View File

@ -51,7 +51,7 @@ func (s sortableByProperty) Swap(i, j int) {
// Less is part of sort.Interface.
func (s sortableByProperty) Less(i, j int) bool {
// index returns the value at the s.key, if in is a map that contains this key
// index returns the value at s.key, if in is a map that contains this key
index := func(i int) interface{} {
value := ToLiquid(s.data[i])
rt := reflect.ValueOf(value)

View File

@ -26,7 +26,8 @@ type Value interface {
// ValueOf returns a Value that wraps its argument.
// If the argument is already a Value, it returns this.
func ValueOf(value interface{}) Value {
func ValueOf(value interface{}) Value { // nolint: gocyclo
// interned values
switch value {
case nil:
return nilValue
@ -35,7 +36,10 @@ func ValueOf(value interface{}) Value {
case false:
return falseValue
}
if v, ok := value.(Value); ok {
switch v := value.(type) {
case drop:
return dropWrapper{d: v}
case Value:
return v
}
rk := reflect.TypeOf(value).Kind()