1
0
mirror of https://github.com/danog/liquid.git synced 2024-12-02 14:27:46 +01:00

Rename Settings -> Config

This commit is contained in:
Oliver Steele 2017-07-04 11:08:57 -04:00
parent 6161e6df28
commit 405c5bf694
17 changed files with 74 additions and 70 deletions

View File

@ -8,12 +8,12 @@ import (
"github.com/osteele/liquid/tags" "github.com/osteele/liquid/tags"
) )
type engine struct{ settings render.Settings } type engine struct{ settings render.Config }
// NewEngine returns a new template engine. // NewEngine returns a new template engine.
func NewEngine() Engine { func NewEngine() Engine {
e := engine{render.NewSettings()} e := engine{render.NewConfig()}
filters.AddStandardFilters(e.settings.ExpressionSettings) filters.AddStandardFilters(e.settings.ExpressionConfig)
tags.AddStandardTags(e.settings) tags.AddStandardTags(e.settings)
return e return e
} }

16
expressions/config.go Normal file
View File

@ -0,0 +1,16 @@
package expressions
// Config holds configuration information for expression interpretation.
type Config struct {
filters *filterDictionary
}
// NewConfig creates a new Settings.
func NewConfig() Config {
return Config{newFilterDictionary()}
}
// AddFilter adds a filter function to settings.
func (s Config) AddFilter(name string, fn interface{}) {
s.filters.addFilter(name, fn)
}

View File

@ -9,26 +9,11 @@ type Context interface {
type context struct { type context struct {
bindings map[string]interface{} bindings map[string]interface{}
Settings Config
}
// Settings holds configuration information for expression interpretation.
type Settings struct {
filters *filterDictionary
}
// NewSettings creates a new Settings.
func NewSettings() Settings {
return Settings{newFilterDictionary()}
}
// AddFilter adds a filter function to settings.
func (s Settings) AddFilter(name string, fn interface{}) {
s.filters.addFilter(name, fn)
} }
// NewContext makes a new expression evaluation context. // NewContext makes a new expression evaluation context.
func NewContext(vars map[string]interface{}, s Settings) Context { func NewContext(vars map[string]interface{}, s Config) Context {
return &context{vars, s} return &context{vars, s}
} }

View File

@ -112,7 +112,7 @@ var evaluatorTestBindings = (map[string]interface{}{
}) })
func TestEvaluator(t *testing.T) { func TestEvaluator(t *testing.T) {
settings := NewSettings() settings := NewConfig()
settings.AddFilter("length", strings.Count) settings.AddFilter("length", strings.Count)
context := NewContext(evaluatorTestBindings, settings) context := NewContext(evaluatorTestBindings, settings)
for i, test := range evaluatorTests { for i, test := range evaluatorTests {
@ -125,7 +125,7 @@ func TestEvaluator(t *testing.T) {
} }
func TestHelpers(t *testing.T) { func TestHelpers(t *testing.T) {
context := NewContext(map[string]interface{}{}, NewSettings()) context := NewContext(map[string]interface{}{}, NewConfig())
k := Constant(10) k := Constant(10)
v, err := k.Evaluate(context) v, err := k.Evaluate(context)

View File

@ -160,7 +160,7 @@ var filterTestBindings = map[string]interface{}{
} }
func TestFilters(t *testing.T) { func TestFilters(t *testing.T) {
settings := expressions.NewSettings() settings := expressions.NewConfig()
AddStandardFilters(settings) AddStandardFilters(settings)
context := expressions.NewContext(filterTestBindings, settings) context := expressions.NewContext(filterTestBindings, settings)

View File

@ -19,7 +19,7 @@ import (
) )
// AddStandardFilters defines the standard Liquid filters. // AddStandardFilters defines the standard Liquid filters.
func AddStandardFilters(settings expressions.Settings) { // nolint: gocyclo func AddStandardFilters(settings expressions.Config) { // nolint: gocyclo
// values // values
settings.AddFilter("default", func(value, defaultValue interface{}) interface{} { settings.AddFilter("default", func(value, defaultValue interface{}) interface{} {
if value == nil || value == false || generics.IsEmpty(value) { if value == nil || value == false || generics.IsEmpty(value) {

View File

@ -35,22 +35,22 @@ func (c *blockDef) isStartTag() bool {
return !c.isBranchTag && !c.isEndTag return !c.isBranchTag && !c.isEndTag
} }
func (s Settings) addBlockDef(ct *blockDef) { func (s Config) addBlockDef(ct *blockDef) {
s.controlTags[ct.name] = ct s.controlTags[ct.name] = ct
} }
func (s Settings) findBlockDef(name string) (*blockDef, bool) { func (s Config) findBlockDef(name string) (*blockDef, bool) {
ct, found := s.controlTags[name] ct, found := s.controlTags[name]
return ct, found return ct, found
} }
type blockDefBuilder struct { type blockDefBuilder struct {
s Settings s Config
tag *blockDef tag *blockDef
} }
// AddBlock defines a control tag and its matching end tag. // AddBlock defines a control tag and its matching end tag.
func (s Settings) AddBlock(name string) blockDefBuilder { // nolint: golint func (s Config) AddBlock(name string) blockDefBuilder { // nolint: golint
ct := &blockDef{name: name} ct := &blockDef{name: name}
s.addBlockDef(ct) s.addBlockDef(ct)
s.addBlockDef(&blockDef{name: "end" + name, isEndTag: true, parent: ct}) s.addBlockDef(&blockDef{name: "end" + name, isEndTag: true, parent: ct})

26
render/config.go Normal file
View File

@ -0,0 +1,26 @@
package render
import "github.com/osteele/liquid/expressions"
// Config holds configuration information for parsing and rendering.
type Config struct {
ExpressionConfig expressions.Config
tags map[string]TagDefinition
controlTags map[string]*blockDef
}
// NewConfig creates a new Settings.
func NewConfig() Config {
s := Config{
expressions.NewConfig(),
map[string]TagDefinition{},
map[string]*blockDef{},
}
s.AddTag("assign", assignTagDef)
return s
}
// AddFilter adds a filter to settings.
func (s Config) AddFilter(name string, fn interface{}) {
s.ExpressionConfig.AddFilter(name, fn)
}

View File

@ -7,34 +7,11 @@ import (
// Context is the evaluation context for chunk AST rendering. // Context is the evaluation context for chunk AST rendering.
type Context struct { type Context struct {
bindings map[string]interface{} bindings map[string]interface{}
settings Settings settings Config
}
// Settings holds configuration information for parsing and rendering.
type Settings struct {
ExpressionSettings expressions.Settings
tags map[string]TagDefinition
controlTags map[string]*blockDef
}
// AddFilter adds a filter to settings.
func (s Settings) AddFilter(name string, fn interface{}) {
s.ExpressionSettings.AddFilter(name, fn)
}
// NewSettings creates a new Settings.
func NewSettings() Settings {
s := Settings{
expressions.NewSettings(),
map[string]TagDefinition{},
map[string]*blockDef{},
}
s.AddTag("assign", assignTagDef)
return s
} }
// NewContext creates a new evaluation context. // NewContext creates a new evaluation context.
func NewContext(scope map[string]interface{}, s Settings) Context { func NewContext(scope map[string]interface{}, s Config) Context {
// The assign tag modifies the scope, so make a copy first. // The assign tag modifies the scope, so make a copy first.
// TODO this isn't really the right place for this. // TODO this isn't really the right place for this.
vars := map[string]interface{}{} vars := map[string]interface{}{}
@ -66,5 +43,5 @@ func (c Context) Evaluate(expr expressions.Expression) (out interface{}, err err
} }
} }
}() }()
return expr.Evaluate(expressions.NewContext(c.bindings, c.settings.ExpressionSettings)) return expr.Evaluate(expressions.NewContext(c.bindings, c.settings.ExpressionConfig))
} }

View File

@ -7,13 +7,13 @@ import (
) )
// Parse parses a source template. It returns an AST root, that can be evaluated. // Parse parses a source template. It returns an AST root, that can be evaluated.
func (s Settings) Parse(source string) (ASTNode, error) { func (s Config) Parse(source string) (ASTNode, error) {
tokens := Scan(source, "") tokens := Scan(source, "")
return s.parseChunks(tokens) return s.parseChunks(tokens)
} }
// Parse creates an AST from a sequence of Chunks. // Parse creates an AST from a sequence of Chunks.
func (s Settings) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocyclo func (s Config) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocyclo
// a stack of control tag state, for matching nested {%if}{%endif%} etc. // a stack of control tag state, for matching nested {%if}{%endif%} etc.
type frame struct { type frame struct {
cd *blockDef // saved local ccd cd *blockDef // saved local ccd
@ -106,7 +106,7 @@ func (s Settings) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocy
} }
// nolint: gocyclo // nolint: gocyclo
func (s Settings) evaluateBuilders(n ASTNode) error { func (s Config) evaluateBuilders(n ASTNode) error {
switch n := n.(type) { switch n := n.(type) {
case *ASTBlock: case *ASTBlock:
for _, child := range n.Body { for _, child := range n.Body {

View File

@ -8,7 +8,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func addParserTestTags(s Settings) { func addParserTestTags(s Config) {
s.AddBlock("case").Branch("when") s.AddBlock("case").Branch("when")
s.AddBlock("comment") s.AddBlock("comment")
s.AddBlock("for").Governs([]string{"break"}) s.AddBlock("for").Governs([]string{"break"})
@ -38,7 +38,7 @@ var parserTests = []struct{ in string }{
} }
func TestParseErrors(t *testing.T) { func TestParseErrors(t *testing.T) {
settings := NewSettings() settings := NewConfig()
addParserTestTags(settings) addParserTestTags(settings)
for i, test := range parseErrorTests { for i, test := range parseErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) { t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
@ -51,7 +51,7 @@ func TestParseErrors(t *testing.T) {
} }
func TestParser(t *testing.T) { func TestParser(t *testing.T) {
settings := NewSettings() settings := NewConfig()
addParserTestTags(settings) addParserTestTags(settings)
for i, test := range parserTests { for i, test := range parserTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) { t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {

View File

@ -56,7 +56,7 @@ func (c renderContext) EvaluateString(source string) (out interface{}, err error
} }
} }
}() }()
return expressions.EvaluateString(source, expressions.NewContext(c.ctx.bindings, c.ctx.settings.ExpressionSettings)) return expressions.EvaluateString(source, expressions.NewContext(c.ctx.bindings, c.ctx.settings.ExpressionConfig))
} }
func (c renderContext) EvaluateStatement(tag, source string) (interface{}, error) { func (c renderContext) EvaluateStatement(tag, source string) (interface{}, error) {

View File

@ -10,7 +10,7 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
func addRenderTestTags(s Settings) { func addRenderTestTags(s Config) {
s.AddBlock("parse").Parser(func(c ASTBlock) (func(io.Writer, RenderContext) error, error) { s.AddBlock("parse").Parser(func(c ASTBlock) (func(io.Writer, RenderContext) error, error) {
a := c.Args a := c.Args
return func(w io.Writer, c RenderContext) error { return func(w io.Writer, c RenderContext) error {
@ -76,7 +76,7 @@ var renderTestBindings = map[string]interface{}{
} }
func TestRender(t *testing.T) { func TestRender(t *testing.T) {
settings := NewSettings() settings := NewConfig()
addRenderTestTags(settings) addRenderTestTags(settings)
context := NewContext(renderTestBindings, settings) context := NewContext(renderTestBindings, settings)
for i, test := range renderTests { for i, test := range renderTests {
@ -92,7 +92,7 @@ func TestRender(t *testing.T) {
} }
func TestRenderErrors(t *testing.T) { func TestRenderErrors(t *testing.T) {
settings := NewSettings() settings := NewConfig()
addRenderTestTags(settings) addRenderTestTags(settings)
context := NewContext(renderTestBindings, settings) context := NewContext(renderTestBindings, settings)
for i, test := range renderErrorTests { for i, test := range renderErrorTests {

View File

@ -17,12 +17,12 @@ func assignTagDef(source string) (func(io.Writer, RenderContext) error, error) {
} }
// AddTag creates a tag definition. // AddTag creates a tag definition.
func (s *Settings) AddTag(name string, td TagDefinition) { func (s *Config) AddTag(name string, td TagDefinition) {
s.tags[name] = td s.tags[name] = td
} }
// FindTagDefinition looks up a tag definition. // FindTagDefinition looks up a tag definition.
func (s *Settings) FindTagDefinition(name string) (TagDefinition, bool) { func (s *Config) FindTagDefinition(name string) (TagDefinition, bool) {
td, ok := s.tags[name] td, ok := s.tags[name]
return td, ok return td, ok
} }

View File

@ -10,7 +10,7 @@ import (
) )
// AddStandardTags defines the standard Liquid tags. // AddStandardTags defines the standard Liquid tags.
func AddStandardTags(settings render.Settings) { func AddStandardTags(settings render.Config) {
// The parser only recognize the comment and raw tags if they've been defined, // The parser only recognize the comment and raw tags if they've been defined,
// but it ignores any syntax specified here. // but it ignores any syntax specified here.
loopTags := []string{"break", "continue", "cycle"} loopTags := []string{"break", "continue", "cycle"}

View File

@ -133,7 +133,7 @@ var bindings = map[string]interface{}{
} }
func TestParseErrors(t *testing.T) { func TestParseErrors(t *testing.T) {
settings := render.NewSettings() settings := render.NewConfig()
AddStandardTags(settings) AddStandardTags(settings)
for i, test := range parseErrorTests { for i, test := range parseErrorTests {
t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) { t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) {
@ -145,7 +145,7 @@ func TestParseErrors(t *testing.T) {
} }
} }
func TestRender(t *testing.T) { func TestRender(t *testing.T) {
settings := render.NewSettings() settings := render.NewConfig()
AddStandardTags(settings) AddStandardTags(settings)
context := render.NewContext(bindings, settings) context := render.NewContext(bindings, settings)
for i, test := range tagTests { for i, test := range tagTests {

View File

@ -8,7 +8,7 @@ import (
type template struct { type template struct {
ast render.ASTNode ast render.ASTNode
settings render.Settings settings render.Config
} }
// Render executes the template within the bindings environment. // Render executes the template within the bindings environment.