Skip to content

Commit

Permalink
variable names
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Aug 23, 2017
1 parent 1a12f12 commit d27c839
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 74 deletions.
66 changes: 33 additions & 33 deletions values/structvalue.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,57 +6,57 @@ import (

type structValue struct{ wrapperValue }

func (v structValue) IndexValue(index Value) Value {
return v.PropertyValue(index)
func (sv structValue) IndexValue(index Value) Value {
return sv.PropertyValue(index)
}

func (v structValue) Contains(elem Value) bool {
func (sv structValue) Contains(elem Value) bool {
name, ok := elem.Interface().(string)
if !ok {
return false
}
rt := reflect.TypeOf(v.value)
if rt.Kind() == reflect.Ptr {
if _, found := rt.MethodByName(name); found {
st := reflect.TypeOf(sv.value)
if st.Kind() == reflect.Ptr {
if _, found := st.MethodByName(name); found {
return true
}
rt = rt.Elem()
st = st.Elem()
}
if _, found := rt.MethodByName(name); found {
if _, found := st.MethodByName(name); found {
return true
}
if _, found := v.findField(name); found {
if _, found := sv.findField(name); found {
return true
}
return false
}

func (v structValue) PropertyValue(index Value) Value {
func (sv structValue) PropertyValue(index Value) Value {
name, ok := index.Interface().(string)
if !ok {
return nilValue
}
rv := reflect.ValueOf(v.value)
rt := reflect.TypeOf(v.value)
if rt.Kind() == reflect.Ptr {
if _, found := rt.MethodByName(name); found {
m := rv.MethodByName(name)
return v.invoke(m)
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)
}
rt = rt.Elem()
rv = rv.Elem()
if !rv.IsValid() {
st = st.Elem()
sr = sr.Elem()
if !sr.IsValid() {
return nilValue
}
}
if _, found := rt.MethodByName(name); found {
m := rv.MethodByName(name)
return v.invoke(m)
if _, ok := st.MethodByName(name); ok {
m := sr.MethodByName(name)
return sv.invoke(m)
}
if field, found := v.findField(name); found {
fv := rv.FieldByName(field.Name)
if field, ok := sv.findField(name); ok {
fv := sr.FieldByName(field.Name)
if fv.Kind() == reflect.Func {
return v.invoke(fv)
return sv.invoke(fv)
}
return ValueOf(fv.Interface())
}
Expand All @@ -66,26 +66,26 @@ func (v structValue) PropertyValue(index Value) Value {
const tagKey = "liquid"

// like FieldByName, but obeys `liquid:"name"` tags
func (v structValue) findField(name string) (*reflect.StructField, bool) {
rt := reflect.TypeOf(v.value)
if rt.Kind() == reflect.Ptr {
rt = rt.Elem()
func (sv structValue) findField(name string) (*reflect.StructField, bool) {
sr := reflect.TypeOf(sv.value)
if sr.Kind() == reflect.Ptr {
sr = sr.Elem()
}
if field, found := rt.FieldByName(name); found {
if field, ok := sr.FieldByName(name); ok {
if _, ok := field.Tag.Lookup(tagKey); !ok {
return &field, true
}
}
for i, n := 0, rt.NumField(); i < n; i++ {
field := rt.Field(i)
for i, n := 0, sr.NumField(); i < n; i++ {
field := sr.Field(i)
if field.Tag.Get(tagKey) == name {
return &field, true
}
}
return nil, false
}

func (v structValue) invoke(fv reflect.Value) Value {
func (sv structValue) invoke(fv reflect.Value) Value {
if fv.IsNil() {
return nilValue
}
Expand Down
82 changes: 41 additions & 41 deletions values/value.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,21 +118,21 @@ type arrayValue struct{ wrapperValue }
type mapValue struct{ wrapperValue }
type stringValue struct{ wrapperValue }

func (v arrayValue) Contains(elem Value) bool {
rv := reflect.ValueOf(v.value)
e := elem.Interface()
for i, len := 0, rv.Len(); i < len; i++ {
if Equal(rv.Index(i).Interface(), e) {
func (av arrayValue) Contains(ev Value) bool {
ar := reflect.ValueOf(av.value)
e := ev.Interface()
for i, len := 0, ar.Len(); i < len; i++ {
if Equal(ar.Index(i).Interface(), e) {
return true
}
}
return false
}

func (v arrayValue) IndexValue(index Value) Value {
rv := reflect.ValueOf(v.value)
func (av arrayValue) IndexValue(iv Value) Value {
ar := reflect.ValueOf(av.value)
var n int
switch ix := index.Interface().(type) {
switch ix := iv.Interface().(type) {
case int:
n = ix
case float32:
Expand All @@ -144,80 +144,80 @@ func (v arrayValue) IndexValue(index Value) Value {
return nilValue
}
if n < 0 {
n += rv.Len()
n += ar.Len()
}
if 0 <= n && n < rv.Len() {
return ValueOf(rv.Index(n).Interface())
if 0 <= n && n < ar.Len() {
return ValueOf(ar.Index(n).Interface())
}
return nilValue
}

func (v arrayValue) PropertyValue(index Value) Value {
rv := reflect.ValueOf(v.value)
switch index.Interface() {
func (av arrayValue) PropertyValue(iv Value) Value {
ar := reflect.ValueOf(av.value)
switch iv.Interface() {
case firstKey:
if rv.Len() > 0 {
return ValueOf(rv.Index(0).Interface())
if ar.Len() > 0 {
return ValueOf(ar.Index(0).Interface())
}
case lastKey:
if rv.Len() > 0 {
return ValueOf(rv.Index(rv.Len() - 1).Interface())
if ar.Len() > 0 {
return ValueOf(ar.Index(ar.Len() - 1).Interface())
}
case sizeKey:
return ValueOf(rv.Len())
return ValueOf(ar.Len())
}
return nilValue
}

func (v mapValue) Contains(index Value) bool {
rv := reflect.ValueOf(v.value)
iv := reflect.ValueOf(index.Interface())
if iv.IsValid() && rv.Type().Key() == iv.Type() {
return rv.MapIndex(iv).IsValid()
func (mv mapValue) Contains(iv Value) bool {
mr := reflect.ValueOf(mv.value)
ir := reflect.ValueOf(iv.Interface())
if ir.IsValid() && mr.Type().Key() == ir.Type() {
return mr.MapIndex(ir).IsValid()
}
return false
}

func (v mapValue) IndexValue(index Value) Value {
rv := reflect.ValueOf(v.value)
iv := reflect.ValueOf(index.Interface())
if iv.IsValid() && iv.Type().ConvertibleTo(rv.Type().Key()) {
ev := rv.MapIndex(iv.Convert(rv.Type().Key()))
func (mv mapValue) IndexValue(iv Value) Value {
mr := reflect.ValueOf(mv.value)
ir := reflect.ValueOf(iv.Interface())
if ir.IsValid() && ir.Type().ConvertibleTo(mr.Type().Key()) {
ev := mr.MapIndex(ir.Convert(mr.Type().Key()))
if ev.IsValid() {
return ValueOf(ev.Interface())
}
}
return nilValue
}

func (v mapValue) PropertyValue(index Value) Value {
rv := reflect.ValueOf(v.Interface())
iv := reflect.ValueOf(index.Interface())
if !iv.IsValid() {
func (mv mapValue) PropertyValue(iv Value) Value {
mr := reflect.ValueOf(mv.Interface())
ir := reflect.ValueOf(iv.Interface())
if !ir.IsValid() {
return nilValue
}
ev := rv.MapIndex(iv)
ev := mr.MapIndex(ir)
switch {
case ev.IsValid():
return ValueOf(ev.Interface())
case index.Interface() == sizeKey:
return ValueOf(rv.Len())
case iv.Interface() == sizeKey:
return ValueOf(mr.Len())
default:
return nilValue
}
}

func (v stringValue) Contains(substr Value) bool {
func (sv stringValue) Contains(substr Value) bool {
s, ok := substr.Interface().(string)
if !ok {
s = fmt.Sprint(substr.Interface())
}
return strings.Contains(v.value.(string), s)
return strings.Contains(sv.value.(string), s)
}

func (v stringValue) PropertyValue(index Value) Value {
if index.Interface() == sizeKey {
return ValueOf(len(v.value.(string)))
func (sv stringValue) PropertyValue(iv Value) Value {
if iv.Interface() == sizeKey {
return ValueOf(len(sv.value.(string)))
}
return nilValue
}

0 comments on commit d27c839

Please sign in to comment.