1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-11 16:19:38 +01:00
liquid/values/structvalue.go

102 lines
2.1 KiB
Go
Raw Normal View History

2017-07-28 00:11:37 +02:00
package values
2017-07-22 14:36:40 +02:00
2017-07-22 14:57:02 +02:00
import (
"reflect"
)
type structValue struct{ wrapperValue }
2017-07-22 14:36:40 +02:00
2017-08-23 21:56:18 +02:00
func (sv structValue) IndexValue(index Value) Value {
return sv.PropertyValue(index)
2017-07-22 14:36:40 +02:00
}
2017-08-23 21:56:18 +02:00
func (sv structValue) Contains(elem Value) bool {
2017-07-22 14:36:40 +02:00
name, ok := elem.Interface().(string)
if !ok {
return false
}
2017-08-23 21:56:18 +02:00
st := reflect.TypeOf(sv.value)
if st.Kind() == reflect.Ptr {
if _, found := st.MethodByName(name); found {
2017-07-22 14:57:02 +02:00
return true
}
2017-08-23 21:56:18 +02:00
st = st.Elem()
2017-07-22 14:36:40 +02:00
}
2017-08-23 21:56:18 +02:00
if _, found := st.MethodByName(name); found {
2017-07-22 14:36:40 +02:00
return true
}
2017-08-23 21:56:18 +02:00
if _, found := sv.findField(name); found {
2017-07-22 14:36:40 +02:00
return true
}
return false
}
2017-08-23 21:56:18 +02:00
func (sv structValue) PropertyValue(index Value) Value {
2017-07-22 14:36:40 +02:00
name, ok := index.Interface().(string)
if !ok {
return nilValue
}
2017-08-23 21:56:18 +02:00
sr := reflect.ValueOf(sv.value)
st := reflect.TypeOf(sv.value)
if st.Kind() == reflect.Ptr {
if _, found := st.MethodByName(name); found {
m := sr.MethodByName(name)
return sv.invoke(m)
2017-07-22 14:57:02 +02:00
}
2017-08-23 21:56:18 +02:00
st = st.Elem()
sr = sr.Elem()
if !sr.IsValid() {
return nilValue
}
2017-07-22 14:36:40 +02:00
}
2017-08-23 21:56:18 +02:00
if _, ok := st.MethodByName(name); ok {
m := sr.MethodByName(name)
return sv.invoke(m)
2017-07-22 14:57:02 +02:00
}
2017-08-23 21:56:18 +02:00
if field, ok := sv.findField(name); ok {
fv := sr.FieldByName(field.Name)
2017-07-22 14:36:40 +02:00
if fv.Kind() == reflect.Func {
2017-08-23 21:56:18 +02:00
return sv.invoke(fv)
2017-07-22 14:36:40 +02:00
}
return ValueOf(fv.Interface())
}
return nilValue
}
2017-07-22 14:57:02 +02:00
const tagKey = "liquid"
// like FieldByName, but obeys `liquid:"name"` tags
2017-08-23 21:56:18 +02:00
func (sv structValue) findField(name string) (*reflect.StructField, bool) {
sr := reflect.TypeOf(sv.value)
if sr.Kind() == reflect.Ptr {
sr = sr.Elem()
2017-07-22 14:57:02 +02:00
}
2017-08-23 21:56:18 +02:00
if field, ok := sr.FieldByName(name); ok {
2017-07-22 14:57:02 +02:00
if _, ok := field.Tag.Lookup(tagKey); !ok {
return &field, true
}
}
2017-08-23 21:56:18 +02:00
for i, n := 0, sr.NumField(); i < n; i++ {
field := sr.Field(i)
2017-07-22 14:57:02 +02:00
if field.Tag.Get(tagKey) == name {
return &field, true
}
}
return nil, false
}
2017-08-23 21:56:18 +02:00
func (sv structValue) invoke(fv reflect.Value) Value {
2017-07-22 14:36:40 +02:00
if fv.IsNil() {
return nilValue
}
mt := fv.Type()
if mt.NumIn() > 0 || mt.NumOut() > 2 {
return nilValue
}
results := fv.Call([]reflect.Value{})
if len(results) > 1 && !results[1].IsNil() {
panic(results[1].Interface())
}
return ValueOf(results[0].Interface())
}