Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for string unescape #89

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
*.out
/liquid
*.test
.idea
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
4 changes: 2 additions & 2 deletions 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 Expand Up @@ -120,7 +120,7 @@ func (e *Engine) ParseAndRenderString(source string, b Bindings) (string, Source
// ParseTemplate, ParseTemplateLocation, ParseAndRender, or ParseAndRenderString. An empty delimiter
// stands for the corresponding default: objectLeft = {{, objectRight = }}, tagLeft = {% , tagRight = %}
func (e *Engine) Delims(objectLeft, objectRight, tagLeft, tagRight string) *Engine {
e.cfg.Delims = []string{objectLeft, objectRight, tagLeft, tagRight}
e.cfg.Delims(objectLeft, objectRight, tagLeft, tagRight)
return e
}

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
14 changes: 7 additions & 7 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 @@ -55,14 +55,14 @@ func TestEngine_ParseAndFRender(t *testing.T) {
wr := capWriter{}
err := engine.ParseAndFRender(&wr, []byte(test.in), testBindings)
require.NoErrorf(t, err, test.in)
require.Equalf(t, strings.ToUpper(test.expected), wr.String(), test.in)
require.Equalf(t, test.expected, wr.String(), test.in)
})
}
}

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
Loading
Loading