2017-07-11 18:03:52 +02:00
|
|
|
package utils
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"reflect"
|
|
|
|
|
2022-01-29 20:17:23 +01:00
|
|
|
"github.com/danog/liquid"
|
2017-07-11 18:03:52 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// FollowDots applied to a property list ["a", "b", "c"] is equivalent to
|
|
|
|
// the Liquid data expression "data.a.b.c", except without special treatment
|
|
|
|
// of "first", "last", and "size".
|
|
|
|
func FollowDots(data interface{}, props []string) (interface{}, error) {
|
|
|
|
for _, name := range props {
|
|
|
|
data = liquid.FromDrop(data)
|
|
|
|
if reflect.TypeOf(data).Kind() == reflect.Map {
|
|
|
|
item := reflect.ValueOf(data).MapIndex(reflect.ValueOf(name))
|
2017-08-14 21:23:00 +02:00
|
|
|
if item.IsValid() && (item.Kind() != reflect.Ptr || !item.IsNil()) && item.CanInterface() {
|
2017-07-11 18:03:52 +02:00
|
|
|
data = item.Interface()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("no such property: %q", name)
|
|
|
|
}
|
|
|
|
return data, nil
|
|
|
|
}
|