Skip to content

Commit

Permalink
Move generics to own package
Browse files Browse the repository at this point in the history
  • Loading branch information
osteele committed Jun 27, 2017
1 parent 612f456 commit f52d00f
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 268 deletions.
22 changes: 12 additions & 10 deletions expressions/expressions.y
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@ package expressions
import (
"fmt"
"reflect"
"github.com/osteele/liquid/generics"
)

func init() {
// This allows adding and removing references to fmt in the rules below,
// without having to edit the import statement to avoid erorrs each time.
_ = fmt.Sprint("")
}

Expand Down Expand Up @@ -79,25 +82,24 @@ expr1:
rel:
expr1
| expr EQ expr {
a, b := $1, $3
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
aref, bref := reflect.ValueOf(a(ctx)), reflect.ValueOf(b(ctx))
return GenericCompare(aref, bref) == 0
a, b := fa(ctx), fb(ctx)
return generics.Equal(a, b)
}
}
| expr '<' expr {
a, b := $1, $3
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
aref, bref := reflect.ValueOf(a(ctx)), reflect.ValueOf(b(ctx))
return GenericCompare(aref, bref) < 0
a, b := fa(ctx), fb(ctx)
return generics.Less(a, b)
}
}
| expr '>' expr {
a, b := $1, $3
fa, fb := $1, $3
$$ = func(ctx Context) interface{} {
aref, bref := reflect.ValueOf(a(ctx)), reflect.ValueOf(b(ctx))
return GenericCompare(aref, bref) > 0
a, b := fa(ctx), fb(ctx)
return generics.Less(b, a)
}
}
;

9 changes: 5 additions & 4 deletions expressions/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"

"github.com/osteele/liquid/errors"
"github.com/osteele/liquid/generics"
)

type InterpreterError string
Expand All @@ -33,9 +34,9 @@ func sortFilter(in []interface{}, key interface{}) []interface{} {
out[i] = v
}
if key == nil {
genericSort(out)
generics.Sort(out)
} else {
sortByProperty(out, key.(string))
generics.SortByProperty(out, key.(string))
}
return out
}
Expand Down Expand Up @@ -87,7 +88,7 @@ func makeFilter(f valueFn, name string, param valueFn) valueFn {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
case genericError:
case generics.GenericError:
panic(InterpreterError(e.Error()))
default:
// fmt.Println(string(debug.Stack()))
Expand All @@ -99,7 +100,7 @@ func makeFilter(f valueFn, name string, param valueFn) valueFn {
if param != nil {
args = append(args, param(ctx))
}
out, err := genericApply(fr, args)
out, err := generics.Apply(fr, args)
if err != nil {
panic(err)
}
Expand Down
233 changes: 0 additions & 233 deletions expressions/generics.go

This file was deleted.

Loading

0 comments on commit f52d00f

Please sign in to comment.