1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 23:24:38 +01:00

Move interpreter ops into evaluator package

This commit is contained in:
Oliver Steele 2017-07-13 23:46:12 -04:00
parent bfc7ced272
commit c11cf2aa25
7 changed files with 238 additions and 213 deletions

81
evaluator/accessors.go Normal file
View File

@ -0,0 +1,81 @@
package evaluator
import (
"math"
"reflect"
)
// Index returns sequence[ix] according to Liquid semantics.
func Index(sequence, ix interface{}) interface{} { // nolint: gocyclo
ref := reflect.ValueOf(sequence)
ixRef := reflect.ValueOf(ix)
if !ref.IsValid() || !ixRef.IsValid() {
return nil
}
switch ref.Kind() {
case reflect.Array, reflect.Slice:
switch ixRef.Kind() {
case reflect.Float32, reflect.Float64:
if n, frac := math.Modf(ixRef.Float()); frac == 0 {
ix = int(n)
ixRef = reflect.ValueOf(ix)
}
}
switch ixRef.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int(ixRef.Int())
if n < 0 {
n = ref.Len() + n
}
if 0 <= n && n < ref.Len() {
return ToLiquid(ref.Index(n).Interface())
}
}
case reflect.Map:
if ixRef.Type().ConvertibleTo(ref.Type().Key()) {
item := ref.MapIndex(ixRef.Convert(ref.Type().Key()))
if item.IsValid() {
return ToLiquid(item.Interface())
}
}
}
return nil
}
const (
sizeProperty = "size"
firstProperty = "first"
lastProperty = "last"
)
// ObjectProperty object.name according to Liquid semantics.
func ObjectProperty(object interface{}, name string) interface{} { // nolint: gocyclo
ref := reflect.ValueOf(object)
switch ref.Kind() {
case reflect.Array, reflect.Slice:
if ref.Len() == 0 {
return nil
}
switch name {
case firstProperty:
return ToLiquid(ref.Index(0).Interface())
case lastProperty:
return ToLiquid(ref.Index(ref.Len() - 1).Interface())
case sizeProperty:
return ref.Len()
}
case reflect.String:
if name == sizeProperty {
return ref.Len()
}
case reflect.Map:
value := ref.MapIndex(reflect.ValueOf(name))
if value.Kind() != reflect.Invalid {
return ToLiquid(value.Interface())
}
if name == sizeProperty {
return reflect.ValueOf(name).Len()
}
}
return nil
}

View File

@ -11,21 +11,6 @@ import (
"reflect" "reflect"
) )
// Contains returns a boolean indicating whether array is a sequence that contains item.
func Contains(array interface{}, item interface{}) bool {
item = ToLiquid(item)
ref := reflect.ValueOf(array)
switch ref.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < ref.Len(); i++ {
if ref.Index(i).Interface() == item {
return true
}
}
}
return false
}
// Length returns the length of a string or array. In keeping with Liquid semantics, // Length returns the length of a string or array. In keeping with Liquid semantics,
// and contra Go, it does not return the size of a map. // and contra Go, it does not return the size of a map.
func Length(value interface{}) int { func Length(value interface{}) int {

View File

@ -1,6 +1,46 @@
package evaluator package evaluator
import "reflect" import (
"reflect"
"strings"
)
// Contains returns a boolean indicating whether array is a sequence that contains item.
func Contains(array interface{}, item interface{}) bool {
ref := reflect.ValueOf(array)
switch ref.Kind() {
case reflect.Array, reflect.Slice:
for i := 0; i < ref.Len(); i++ {
if ref.Index(i).Interface() == item {
return true
}
}
}
return false
}
// ContainsString returns a bool indicating whether a string or array contains an object.
func ContainsString(container interface{}, item string) bool {
switch container := container.(type) {
case string:
return strings.Contains(container, item)
case []string:
for _, s := range container {
if s == item {
return true
}
}
case []interface{}:
for _, k := range container {
if s, ok := k.(string); ok && s == item {
return true
}
}
default:
return false
}
return false
}
// IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics. // IsEmpty returns a bool indicating whether the value is empty according to Liquid semantics.
func IsEmpty(value interface{}) bool { func IsEmpty(value interface{}) bool {

17
evaluator/range.go Normal file
View File

@ -0,0 +1,17 @@
package evaluator
// A Range is the range of integers from b to e inclusive.
type Range struct {
b, e int
}
// NewRange returns a new Range
func NewRange(b, e int) Range {
return Range{b, e}
}
// Len is in the iteration interface
func (r Range) Len() int { return r.e + 1 - r.b }
// Index is in the iteration interface
func (r Range) Index(i int) interface{} { return r.b + i }

View File

@ -1,60 +1,27 @@
package expressions package expressions
import ( import (
"fmt"
"math"
"reflect" "reflect"
"strings"
"github.com/osteele/liquid/evaluator" "github.com/osteele/liquid/evaluator"
) )
type rangeObj struct {
b, e int
}
// Len is in the iteration interface
func (r rangeObj) Len() int { return r.e + 1 - r.b }
// Index is in the iteration interface
func (r rangeObj) Index(i int) interface{} { return r.b + i }
func makeRangeExpr(startFn, endFn func(Context) interface{}) func(Context) interface{} { func makeRangeExpr(startFn, endFn func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} { return func(ctx Context) interface{} {
var proto int var proto int
b := evaluator.MustConvert(startFn(ctx), reflect.TypeOf(proto)) b := evaluator.MustConvert(startFn(ctx), reflect.TypeOf(proto))
e := evaluator.MustConvert(endFn(ctx), reflect.TypeOf(proto)) e := evaluator.MustConvert(endFn(ctx), reflect.TypeOf(proto))
r := rangeObj{b.(int), e.(int)} return evaluator.NewRange(b.(int), e.(int))
fmt.Println("made a range", b, e, r, r.Len(), r.Index(1))
return r
} }
} }
func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} { // nolint: gocyclo func makeContainsExpr(e1, e2 func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} { return func(ctx Context) interface{} {
search, ok := e2((ctx)).(string) s, ok := e2((ctx)).(string)
if !ok { if !ok {
return false return false
} }
switch container := e1((ctx)).(type) { return evaluator.ContainsString(e1(ctx), s)
case string:
return strings.Contains(container, search)
case []string:
for _, s := range container {
if s == search {
return true
}
}
case []interface{}:
for _, k := range container {
if s, ok := k.(string); ok && s == search {
return true
}
}
default:
return false
}
return false
} }
} }
@ -64,75 +31,14 @@ func makeFilter(fn valueFn, name string, args []valueFn) valueFn {
} }
} }
func makeIndexExpr(objFn, indexFn func(Context) interface{}) func(Context) interface{} { // nolint: gocyclo func makeIndexExpr(sequenceFn, indexFn func(Context) interface{}) func(Context) interface{} {
return func(ctx Context) interface{} { return func(ctx Context) interface{} {
ref := reflect.ValueOf(objFn(ctx)) return evaluator.Index(sequenceFn(ctx), indexFn(ctx))
ix := indexFn(ctx)
ixRef := reflect.ValueOf(ix)
if !ref.IsValid() || !ixRef.IsValid() {
return nil
}
switch ref.Kind() {
case reflect.Array, reflect.Slice:
switch ixRef.Kind() {
case reflect.Float32, reflect.Float64:
if n, frac := math.Modf(ixRef.Float()); frac == 0 {
ix = int(n)
ixRef = reflect.ValueOf(ix)
}
}
switch ixRef.Kind() {
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int(ixRef.Int())
if n < 0 {
n = ref.Len() + n
}
if 0 <= n && n < ref.Len() {
return ToLiquid(ref.Index(n).Interface())
}
}
case reflect.Map:
if ixRef.Type().ConvertibleTo(ref.Type().Key()) {
item := ref.MapIndex(ixRef.Convert(ref.Type().Key()))
if item.IsValid() {
return ToLiquid(item.Interface())
}
}
}
return nil
} }
} }
func makeObjectPropertyExpr(objFn func(Context) interface{}, attr string) func(Context) interface{} { // nolint: gocyclo func makeObjectPropertyExpr(objFn func(Context) interface{}, name string) func(Context) interface{} {
const sizeString = "size"
return func(ctx Context) interface{} { return func(ctx Context) interface{} {
ref := reflect.ValueOf(objFn(ctx)) return evaluator.ObjectProperty(objFn(ctx), name)
switch ref.Kind() {
case reflect.Array, reflect.Slice:
if ref.Len() == 0 {
return nil
}
switch attr {
case "first":
return ToLiquid(ref.Index(0).Interface())
case "last":
return ToLiquid(ref.Index(ref.Len() - 1).Interface())
case sizeString:
return ToLiquid(ref.Len())
}
case reflect.String:
if attr == sizeString {
return ToLiquid(ref.Len())
}
case reflect.Map:
value := ref.MapIndex(reflect.ValueOf(attr))
if value.Kind() != reflect.Invalid {
return ToLiquid(value.Interface())
}
if attr == sizeString {
return reflect.ValueOf(attr).Len()
}
}
return nil
} }
} }

70
filters/sort_filters.go Normal file
View File

@ -0,0 +1,70 @@
package filters
import (
"reflect"
"sort"
"strings"
"github.com/osteele/liquid/evaluator"
)
func sortFilter(array []interface{}, key interface{}) []interface{} {
result := make([]interface{}, len(array))
copy(result, array)
if key == nil {
evaluator.Sort(result)
} else {
evaluator.SortByProperty(result, key.(string), true)
}
return result
}
func sortNaturalFilter(array []interface{}, key interface{}) interface{} {
result := make([]interface{}, len(array))
copy(result, array)
switch {
case reflect.ValueOf(array).Len() == 0:
case key != nil:
sort.Sort(keySortable{result, func(m interface{}) string {
rv := reflect.ValueOf(m)
if rv.Kind() != reflect.Map {
return ""
}
ev := rv.MapIndex(reflect.ValueOf(key))
if ev.CanInterface() {
if s, ok := ev.Interface().(string); ok {
return strings.ToLower(s)
}
}
return ""
}})
case reflect.TypeOf(array[0]).Kind() == reflect.String:
sort.Sort(keySortable{result, func(s interface{}) string {
return strings.ToUpper(s.(string))
}})
}
return result
}
type keySortable struct {
slice []interface{}
keyFn func(interface{}) string
}
// Len is part of sort.Interface.
func (s keySortable) Len() int {
return len(s.slice)
}
// Swap is part of sort.Interface.
func (s keySortable) Swap(i, j int) {
a := s.slice
a[i], a[j] = a[j], a[i]
}
// Less is part of sort.Interface.
func (s keySortable) Less(i, j int) bool {
k, sl := s.keyFn, s.slice
a, b := k(sl[i]), k(sl[j])
return a < b
}

View File

@ -9,7 +9,6 @@ import (
"net/url" "net/url"
"reflect" "reflect"
"regexp" "regexp"
"sort"
"strings" "strings"
"time" "time"
"unicode" "unicode"
@ -227,67 +226,6 @@ func reverseFilter(array []interface{}) interface{} {
return result return result
} }
func sortFilter(array []interface{}, key interface{}) []interface{} {
result := make([]interface{}, len(array))
copy(result, array)
if key == nil {
evaluator.Sort(result)
} else {
evaluator.SortByProperty(result, key.(string), true)
}
return result
}
func sortNaturalFilter(array []interface{}, key interface{}) interface{} {
result := make([]interface{}, len(array))
copy(result, array)
switch {
case reflect.ValueOf(array).Len() == 0:
case key != nil:
sort.Sort(keySortable{result, func(m interface{}) string {
rv := reflect.ValueOf(m)
if rv.Kind() != reflect.Map {
return ""
}
ev := rv.MapIndex(reflect.ValueOf(key))
if ev.CanInterface() {
if s, ok := ev.Interface().(string); ok {
return strings.ToLower(s)
}
}
return ""
}})
case reflect.TypeOf(array[0]).Kind() == reflect.String:
sort.Sort(keySortable{result, func(s interface{}) string {
return strings.ToUpper(s.(string))
}})
}
return result
}
type keySortable struct {
slice []interface{}
keyFn func(interface{}) string
}
// Len is part of sort.Interface.
func (s keySortable) Len() int {
return len(s.slice)
}
// Swap is part of sort.Interface.
func (s keySortable) Swap(i, j int) {
a := s.slice
a[i], a[j] = a[j], a[i]
}
// Less is part of sort.Interface.
func (s keySortable) Less(i, j int) bool {
k, sl := s.keyFn, s.slice
a, b := k(sl[i]), k(sl[j])
return a < b
}
func splitFilter(s, sep string) interface{} { func splitFilter(s, sep string) interface{} {
result := strings.Split(s, sep) result := strings.Split(s, sep)
// This matches Jekyll's observed behavior. // This matches Jekyll's observed behavior.
@ -298,47 +236,35 @@ func splitFilter(s, sep string) interface{} {
return result return result
} }
func uniqFilter(array []interface{}) []interface{} { func uniqFilter(array []interface{}) (result []interface{}) {
result := []interface{}{} seenMap := map[interface{}]bool{}
seenInts := map[int]bool{}
seenStrings := map[string]bool{}
seen := func(item interface{}) bool { seen := func(item interface{}) bool {
item = evaluator.ToLiquid(item) if k := reflect.TypeOf(item).Kind(); k < reflect.Array || k == reflect.Ptr || k == reflect.UnsafePointer {
switch v := item.(type) { if seenMap[item] {
case int:
if seenInts[v] {
return true return true
} }
seenInts[v] = true seenMap[item] = true
case string: return false
if seenStrings[v] { }
// the O(n^2) case:
for _, other := range result {
if eqItems(item, other) {
return true return true
} }
seenStrings[v] = true
default:
// switch reflect.TypeOf(item).Kind() {
// case reflect.Array, reflect.Map, reflect.Slice, reflect.Struct:
// // addr is never dereferenced, and false negatives are okay
// addr := reflect.ValueOf(item).UnsafeAddr()
// if seenAddrs[addr] {
// return true
// }
// seenAddrs[addr] = true
// }
// the O(n^2) case:
// TODO use == if the values are comparable
for _, v := range result {
if reflect.DeepEqual(item, v) {
return true
}
}
} }
return false return false
} }
for _, e := range array { for _, item := range array {
if !seen(e) { if !seen(item) {
result = append(result, e) result = append(result, item)
} }
} }
return result return
}
func eqItems(a, b interface{}) bool {
if reflect.TypeOf(a).Comparable() && reflect.TypeOf(b).Comparable() {
return a == b
}
return reflect.DeepEqual(a, b)
} }