mirror of
https://github.com/danog/liquid.git
synced 2024-11-30 07:08:58 +01:00
Value layer recognizes, resolves drops
This commit is contained in:
parent
fa5de60b78
commit
560c55eb89
@ -13,3 +13,25 @@ func ToLiquid(value interface{}) interface{} {
|
|||||||
return value
|
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() }
|
||||||
|
@ -6,11 +6,19 @@ import (
|
|||||||
"github.com/stretchr/testify/require"
|
"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) {
|
func TestToLiquid(t *testing.T) {
|
||||||
require.Equal(t, 2, ToLiquid(2))
|
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())
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ func (s sortableByProperty) Swap(i, j int) {
|
|||||||
|
|
||||||
// Less is part of sort.Interface.
|
// Less is part of sort.Interface.
|
||||||
func (s sortableByProperty) Less(i, j int) bool {
|
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{} {
|
index := func(i int) interface{} {
|
||||||
value := ToLiquid(s.data[i])
|
value := ToLiquid(s.data[i])
|
||||||
rt := reflect.ValueOf(value)
|
rt := reflect.ValueOf(value)
|
||||||
|
@ -26,7 +26,8 @@ type Value interface {
|
|||||||
|
|
||||||
// ValueOf returns a Value that wraps its argument.
|
// ValueOf returns a Value that wraps its argument.
|
||||||
// If the argument is already a Value, it returns this.
|
// 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 {
|
switch value {
|
||||||
case nil:
|
case nil:
|
||||||
return nilValue
|
return nilValue
|
||||||
@ -35,7 +36,10 @@ func ValueOf(value interface{}) Value {
|
|||||||
case false:
|
case false:
|
||||||
return falseValue
|
return falseValue
|
||||||
}
|
}
|
||||||
if v, ok := value.(Value); ok {
|
switch v := value.(type) {
|
||||||
|
case drop:
|
||||||
|
return dropWrapper{d: v}
|
||||||
|
case Value:
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
rk := reflect.TypeOf(value).Kind()
|
rk := reflect.TypeOf(value).Kind()
|
||||||
|
Loading…
Reference in New Issue
Block a user