Skip to content

Commit

Permalink
improve number filters
Browse files Browse the repository at this point in the history
  • Loading branch information
steve-ky committed May 26, 2024
1 parent 67ba9a5 commit f408f70
Show file tree
Hide file tree
Showing 22 changed files with 358 additions and 186 deletions.
4 changes: 2 additions & 2 deletions drops.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ package liquid

// Drop indicates that the object will present to templates as its ToLiquid value.
type Drop interface {
ToLiquid() interface{}
ToLiquid() any
}

// FromDrop returns returns object.ToLiquid() if object's type implement this function;
// else the object itself.
func FromDrop(object interface{}) interface{} {
func FromDrop(object any) any {
switch object := object.(type) {
case Drop:
return object.ToLiquid()
Expand Down
18 changes: 9 additions & 9 deletions drops_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (

type dropTest struct{}

func (d dropTest) ToLiquid() interface{} { return "drop" }
func (d dropTest) ToLiquid() any { return "drop" }

func TestDrops(t *testing.T) {
require.Equal(t, "drop", FromDrop(dropTest{}))
Expand All @@ -20,22 +20,22 @@ func TestDrops(t *testing.T) {

type redConvertible struct{}

func (c redConvertible) ToLiquid() interface{} {
return map[string]interface{}{
func (c redConvertible) ToLiquid() any {
return map[string]any{
"color": "red",
}
}

func ExampleDrop_map() {
// type redConvertible struct{}
//
// func (c redConvertible) ToLiquid() interface{} {
// return map[string]interface{}{
// func (c redConvertible) ToLiquid() any {
// return map[string]any{
// "color": "red",
// }
// }
engine := NewEngine()
bindings := map[string]interface{}{
bindings := map[string]any{
"car": redConvertible{},
}
template := `{{ car.color }}`
Expand All @@ -49,7 +49,7 @@ func ExampleDrop_map() {

type car struct{ color, model string }

func (c car) ToLiquid() interface{} {
func (c car) ToLiquid() any {
return carDrop{c.model, c.color}
}

Expand All @@ -65,7 +65,7 @@ func (c carDrop) Drive() string {
func ExampleDrop_struct() {
// type car struct{ color, model string }
//
// func (c car) ToLiquid() interface{} {
// func (c car) ToLiquid() any {
// return carDrop{c.model, c.color}
// }
//
Expand All @@ -79,7 +79,7 @@ func ExampleDrop_struct() {
// }

engine := NewEngine()
bindings := map[string]interface{}{
bindings := map[string]any{
"car": car{"blue", "S85"},
}
template := `{{ car.color }} {{ car.Drive }} Model {{ car.Model }}`
Expand Down
2 changes: 1 addition & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (e *Engine) RegisterBlock(name string, td Renderer) {
// * https://github.com/osteele/liquid/blob/main/filters/standard_filters.go
//
// * https://github.com/osteele/gojekyll/blob/master/filters/filters.go
func (e *Engine) RegisterFilter(name string, fn interface{}) {
func (e *Engine) RegisterFilter(name string, fn any) {
e.cfg.AddFilter(name, fn)
}

Expand Down
10 changes: 5 additions & 5 deletions engine_examples_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
func Example() {
engine := NewEngine()
source := `<h1>{{ page.title }}</h1>`
bindings := map[string]interface{}{
bindings := map[string]any{
"page": map[string]string{
"title": "Introduction",
},
Expand All @@ -27,7 +27,7 @@ func Example() {
func ExampleEngine_ParseAndRenderString() {
engine := NewEngine()
source := `{{ hello | capitalize | append: " Mundo" }}`
bindings := map[string]interface{}{"hello": "hola"}
bindings := map[string]any{"hello": "hola"}
out, err := engine.ParseAndRenderString(source, bindings)
if err != nil {
log.Fatalln(err)
Expand All @@ -38,7 +38,7 @@ func ExampleEngine_ParseAndRenderString() {
func ExampleEngine_ParseTemplate() {
engine := NewEngine()
source := `{{ hello | capitalize | append: " Mundo" }}`
bindings := map[string]interface{}{"hello": "hola"}
bindings := map[string]any{"hello": "hola"}
tpl, err := engine.ParseString(source)
if err != nil {
log.Fatalln(err)
Expand All @@ -54,7 +54,7 @@ func ExampleEngine_RegisterFilter() {
engine := NewEngine()
engine.RegisterFilter("has_prefix", strings.HasPrefix)
template := `{{ title | has_prefix: "Intro" }}`
bindings := map[string]interface{}{
bindings := map[string]any{
"title": "Introduction",
}
out, err := engine.ParseAndRenderString(template, bindings)
Expand All @@ -74,7 +74,7 @@ func ExampleEngine_RegisterFilter_optional_argument() {
return a + b(1)
})
template := `10 + 1 = {{ m | inc }}; 20 + 5 = {{ n | inc: 5 }}`
bindings := map[string]interface{}{
bindings := map[string]any{
"m": 10,
"n": "20",
}
Expand Down
12 changes: 6 additions & 6 deletions engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"github.com/stretchr/testify/require"
)

var emptyBindings = map[string]interface{}{}
var emptyBindings = map[string]any{}

// There's a lot more tests in the filters and tags sub-packages.
// This collects a minimal set for testing end-to-end.
Expand All @@ -21,10 +21,10 @@ var liquidTests = []struct{ in, expected string }{
{`{{ "upper" | upcase }}`, "UPPER"},
}

var testBindings = map[string]interface{}{
var testBindings = map[string]any{
"x": 123,
"ar": []string{"first", "second", "third"},
"page": map[string]interface{}{
"page": map[string]any{
"title": "Introduction",
},
}
Expand Down Expand Up @@ -61,8 +61,8 @@ func TestEngine_ParseAndFRender(t *testing.T) {
}

func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) {
params := map[string]interface{}{
"message": &map[string]interface{}{
params := map[string]any{
"message": &map[string]any{
"Text": "hello",
"jsonNumber": json.Number("123"),
},
Expand All @@ -77,7 +77,7 @@ func TestEngine_ParseAndRenderString_ptr_to_hash(t *testing.T) {
type testStruct struct{ Text string }

func TestEngine_ParseAndRenderString_struct(t *testing.T) {
params := map[string]interface{}{
params := map[string]any{
"message": testStruct{
Text: "hello",
},
Expand Down
34 changes: 0 additions & 34 deletions evaluator/evaluator.go

This file was deleted.

4 changes: 2 additions & 2 deletions expressions/expressions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ var evaluatorTests = []struct {
expected interface{}
}{
// Literals
{`12`, 12},
{`12`, int64(12)},
{`12.3`, 12.3},
{`true`, true},
{`false`, false},
Expand Down Expand Up @@ -53,7 +53,7 @@ var evaluatorTests = []struct {
{`(range.begin..range.end)`, values.NewRange(1, 5)},

// Expressions
{`(1)`, 1},
{`(1)`, int64(1)},
{`(n)`, 123},

// Operators
Expand Down
2 changes: 1 addition & 1 deletion expressions/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ var parseTests = []struct {
{`true`, true},
{`false`, false},
{`nil`, nil},
{`2`, 2},
{`2`, int64(2)},
{`"s"`, "s"},
{`a`, 1},
{`obj.prop`, 2},
Expand Down
4 changes: 2 additions & 2 deletions expressions/scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,7 @@ func (lex *lexer) Lex(out *yySymType) int {
if err != nil {
panic(err)
}
out.val = int(n)
out.val = n
(lex.p)++
goto _out

Expand Down Expand Up @@ -556,7 +556,7 @@ func (lex *lexer) Lex(out *yySymType) int {
if err != nil {
panic(err)
}
out.val = int(n)
out.val = n
(lex.p)++
goto _out

Expand Down
2 changes: 1 addition & 1 deletion expressions/scanner.rl
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func (lex *lexer) Lex(out *yySymType) int {
if err != nil {
panic(err)
}
out.val = int(n)
out.val = n
fbreak;
}
action Float {
Expand Down
4 changes: 2 additions & 2 deletions expressions/scanner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestLex(t *testing.T) {
require.Equal(t, IDENTIFIER, ts[0].tok)
require.Equal(t, "abc", ts[0].typ.name)
require.Equal(t, LITERAL, ts[2].tok)
require.Equal(t, 123, ts[2].typ.val)
require.Equal(t, int64(123), ts[2].typ.val)

// verify these don't match "for", "or", or "false"
ts, _ = scanExpression("forage")
Expand Down Expand Up @@ -67,7 +67,7 @@ func TestLex(t *testing.T) {
require.Equal(t, true, ts[0].typ.val)
require.Equal(t, false, ts[1].typ.val)
require.Equal(t, nil, ts[2].typ.val)
require.Equal(t, 2, ts[3].typ.val)
require.Equal(t, int64(2), ts[3].typ.val)
require.Equal(t, 2.3, ts[4].typ.val)
require.Equal(t, "abc", ts[5].typ.val)
require.Equal(t, "abc", ts[6].typ.val)
Expand Down
Loading

0 comments on commit f408f70

Please sign in to comment.