1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-26 21:24:40 +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"
)
// 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,
// and contra Go, it does not return the size of a map.
func Length(value interface{}) int {

View File

@ -1,6 +1,46 @@
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.
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
import (
"fmt"
"math"
"reflect"
"strings"
"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{} {
return func(ctx Context) interface{} {
var proto int
b := evaluator.MustConvert(startFn(ctx), reflect.TypeOf(proto))
e := evaluator.MustConvert(endFn(ctx), reflect.TypeOf(proto))
r := rangeObj{b.(int), e.(int)}
fmt.Println("made a range", b, e, r, r.Len(), r.Index(1))
return r
return evaluator.NewRange(b.(int), e.(int))
}
}
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{} {
search, ok := e2((ctx)).(string)
s, ok := e2((ctx)).(string)
if !ok {
return false
}
switch container := e1((ctx)).(type) {
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
return evaluator.ContainsString(e1(ctx), s)
}
}
@ -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{} {
ref := reflect.ValueOf(objFn(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
return evaluator.Index(sequenceFn(ctx), indexFn(ctx))
}
}
func makeObjectPropertyExpr(objFn func(Context) interface{}, attr string) func(Context) interface{} { // nolint: gocyclo
const sizeString = "size"
func makeObjectPropertyExpr(objFn func(Context) interface{}, name string) func(Context) interface{} {
return func(ctx Context) interface{} {
ref := reflect.ValueOf(objFn(ctx))
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
return evaluator.ObjectProperty(objFn(ctx), name)
}
}

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"
"reflect"
"regexp"
"sort"
"strings"
"time"
"unicode"
@ -227,67 +226,6 @@ func reverseFilter(array []interface{}) interface{} {
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{} {
result := strings.Split(s, sep)
// This matches Jekyll's observed behavior.
@ -298,47 +236,35 @@ func splitFilter(s, sep string) interface{} {
return result
}
func uniqFilter(array []interface{}) []interface{} {
result := []interface{}{}
seenInts := map[int]bool{}
seenStrings := map[string]bool{}
func uniqFilter(array []interface{}) (result []interface{}) {
seenMap := map[interface{}]bool{}
seen := func(item interface{}) bool {
item = evaluator.ToLiquid(item)
switch v := item.(type) {
case int:
if seenInts[v] {
if k := reflect.TypeOf(item).Kind(); k < reflect.Array || k == reflect.Ptr || k == reflect.UnsafePointer {
if seenMap[item] {
return true
}
seenInts[v] = true
case string:
if seenStrings[v] {
seenMap[item] = true
return false
}
// the O(n^2) case:
for _, other := range result {
if eqItems(item, other) {
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
}
for _, e := range array {
if !seen(e) {
result = append(result, e)
for _, item := range array {
if !seen(item) {
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)
}