Skip to content

Commit

Permalink
Modernize
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed Oct 17, 2024
1 parent 57c2fea commit ee0a86f
Show file tree
Hide file tree
Showing 57 changed files with 286 additions and 286 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Contributions:

* Fix array[nil] ([e39a1fe](https://github.com/osteele/liquid/commit/e39a1fe))
* Fix file not found tests for Windows ([068afef](https://github.com/osteele/liquid/commit/068afef))
* Restore m['str'] where m map[interface{}]interface{}
* Restore m['str'] where m map[any]any
([9852226](https://github.com/osteele/liquid/commit/9852226))

### Docs
Expand Down
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ generator.
```go
engine := liquid.NewEngine()
template := `<h1>{{ page.title }}</h1>`
bindings := map[string]interface{}{
bindings := map[string]any{
"page": map[string]string{
"title": "Introduction",
},
Expand Down Expand Up @@ -84,15 +84,15 @@ These features of Shopify Liquid aren't implemented:

Drops have a different design from the Shopify (Ruby) implementation. A Ruby
drop sets `liquid_attributes` to a list of attributes that are exposed to
Liquid. A Go drop implements `ToLiquid() interface{}`, that returns a proxy
Liquid. A Go drop implements `ToLiquid() any`, that returns a proxy
object. Conventionally, the proxy is a `map` or `struct` that defines the
exposed properties. See <http://godoc.org/github.com/osteele/liquid#Drop> for
additional information.

### Value Types

`Render` and friends take a `Bindings` parameter. This is a map of `string` to
`interface{}`, that associates template variable names with Go values.
`any`, that associates template variable names with Go values.

Any Go value can be used as a variable value. These values have special meaning:

Expand Down
12 changes: 6 additions & 6 deletions cmd/liquid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ import (

// for testing
var (
stderr io.Writer = os.Stderr
stdout io.Writer = os.Stdout
stdin io.Reader = os.Stdin
exit func(int) = os.Exit
env func() []string = os.Environ
bindings map[string]interface{} = map[string]interface{}{}
stderr io.Writer = os.Stderr
stdout io.Writer = os.Stdout
stdin io.Reader = os.Stdin
exit func(int) = os.Exit
env func() []string = os.Environ
bindings map[string]any = map[string]any{}
strictVars bool
)

Expand Down
4 changes: 2 additions & 2 deletions cmd/liquid/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func TestMain(t *testing.T) {
stdin = os.Stdin
exit = os.Exit
env = os.Environ
bindings = map[string]interface{}{}
bindings = map[string]any{}
}()

exit = func(n int) {
Expand Down Expand Up @@ -58,7 +58,7 @@ func TestMain(t *testing.T) {
main()
require.True(t, envCalled)
require.Equal(t, "Hello, World!", buf.String())
bindings = make(map[string]interface{})
bindings = make(map[string]any)

// filename
stdin = os.Stdin
Expand Down
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 @@ -12,7 +12,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 @@ -28,7 +28,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 @@ -40,7 +40,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 @@ -57,7 +57,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 @@ -78,7 +78,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
8 changes: 4 additions & 4 deletions evaluator/evaluator.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,22 @@ import (
)

// Convert should be replaced by values.Convert.
func Convert(value interface{}, typ reflect.Type) (interface{}, error) {
func Convert(value any, typ reflect.Type) (any, error) {
return values.Convert(value, typ)
}

// MustConvertItem should be replaced by values.Convert.
func MustConvertItem(item interface{}, array interface{}) interface{} {
func MustConvertItem(item any, array any) any {
return values.MustConvertItem(item, array)
}

// Sort should be replaced by values.
func Sort(data []interface{}) {
func Sort(data []any) {
values.Sort(data)
}

// SortByProperty should be replaced by values.SortByProperty
func SortByProperty(data []interface{}, key string, nilFirst bool) {
func SortByProperty(data []any, key string, nilFirst bool) {
values.SortByProperty(data, key, nilFirst)
}

Expand Down
2 changes: 1 addition & 1 deletion expressions/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package expressions

// Config holds configuration information for expression interpretation.
type Config struct {
filters map[string]interface{}
filters map[string]any
}

// NewConfig creates a new Config.
Expand Down
16 changes: 8 additions & 8 deletions expressions/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import "github.com/osteele/liquid/values"

// Context is the expression evaluation context. It maps variables names to values.
type Context interface {
ApplyFilter(string, valueFn, []valueFn) (interface{}, error)
ApplyFilter(string, valueFn, []valueFn) (any, error)
// Clone returns a copy with a new variable binding map
// (so that copy.Set does effect the source context.)
Clone() Context
Get(string) interface{}
Set(string, interface{})
Get(string) any
Set(string, any)
}

type context struct {
Config
bindings map[string]interface{}
bindings map[string]any
}

// NewContext makes a new expression evaluation context.
func NewContext(vars map[string]interface{}, cfg Config) Context {
func NewContext(vars map[string]any, cfg Config) Context {
return &context{cfg, vars}
}

func (ctx *context) Clone() Context {
bindings := map[string]interface{}{}
bindings := map[string]any{}
for k, v := range ctx.bindings {
bindings[k] = v
}
return &context{ctx.Config, bindings}
}

// Get looks up a variable value in the expression context.
func (ctx *context) Get(name string) interface{} {
func (ctx *context) Get(name string) any {
return values.ToLiquid(ctx.bindings[name])
}

// Set sets a variable value in the expression context.
func (ctx *context) Set(name string, value interface{}) {
func (ctx *context) Set(name string, value any) {
ctx.bindings[name] = value
}
12 changes: 6 additions & 6 deletions expressions/expressions.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,38 +15,38 @@ import (
// An Expression is a compiled expression.
type Expression interface {
// Evaluate evaluates an expression in a context.
Evaluate(ctx Context) (interface{}, error)
Evaluate(ctx Context) (any, error)
}

// A Closure is an expression within a lexical environment.
// A closure may refer to variables that are not defined in the
// environment. (Therefore it's not a technically a closure.)
type Closure interface {
// Bind creates a new closure with a new binding.
Bind(name string, value interface{}) Closure
Evaluate() (interface{}, error)
Bind(name string, value any) Closure
Evaluate() (any, error)
}

type closure struct {
expr Expression
context Context
}

func (c closure) Bind(name string, value interface{}) Closure {
func (c closure) Bind(name string, value any) Closure {
ctx := c.context.Clone()
ctx.Set(name, value)
return closure{c.expr, ctx}
}

func (c closure) Evaluate() (interface{}, error) {
func (c closure) Evaluate() (any, error) {
return c.expr.Evaluate(c.context)
}

type expression struct {
evaluator func(Context) values.Value
}

func (e expression) Evaluate(ctx Context) (out interface{}, err error) {
func (e expression) Evaluate(ctx Context) (out any, err error) {
defer func() {
if r := recover(); r != nil {
switch e := r.(type) {
Expand Down
2 changes: 1 addition & 1 deletion expressions/expressions.y
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ func init() {
%}
%union {
name string
val interface{}
val any
f func(Context) values.Value
s string
ss []string
Expand Down
Loading

0 comments on commit ee0a86f

Please sign in to comment.