From 405c5bf6947445908b0dfa07df7547922dcc6a0b Mon Sep 17 00:00:00 2001 From: Oliver Steele Date: Tue, 4 Jul 2017 11:08:57 -0400 Subject: [PATCH] Rename Settings -> Config --- engine.go | 6 +++--- expressions/config.go | 16 ++++++++++++++++ expressions/context.go | 19 ++----------------- expressions/expressions_test.go | 4 ++-- filters/filter_test.go | 2 +- filters/filters.go | 2 +- render/blocks.go | 8 ++++---- render/config.go | 26 ++++++++++++++++++++++++++ render/context.go | 29 +++-------------------------- render/parser.go | 6 +++--- render/parser_test.go | 6 +++--- render/render_context.go | 2 +- render/render_test.go | 6 +++--- render/tags.go | 4 ++-- tags/tags.go | 2 +- tags/tags_test.go | 4 ++-- template.go | 2 +- 17 files changed, 74 insertions(+), 70 deletions(-) create mode 100644 expressions/config.go create mode 100644 render/config.go diff --git a/engine.go b/engine.go index 17d255b..6150d4a 100644 --- a/engine.go +++ b/engine.go @@ -8,12 +8,12 @@ import ( "github.com/osteele/liquid/tags" ) -type engine struct{ settings render.Settings } +type engine struct{ settings render.Config } // NewEngine returns a new template engine. func NewEngine() Engine { - e := engine{render.NewSettings()} - filters.AddStandardFilters(e.settings.ExpressionSettings) + e := engine{render.NewConfig()} + filters.AddStandardFilters(e.settings.ExpressionConfig) tags.AddStandardTags(e.settings) return e } diff --git a/expressions/config.go b/expressions/config.go new file mode 100644 index 0000000..c2d6283 --- /dev/null +++ b/expressions/config.go @@ -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) +} diff --git a/expressions/context.go b/expressions/context.go index fa6bbdc..7ce3a49 100644 --- a/expressions/context.go +++ b/expressions/context.go @@ -9,26 +9,11 @@ type Context interface { type context struct { bindings map[string]interface{} - Settings -} - -// 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) + Config } // 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} } diff --git a/expressions/expressions_test.go b/expressions/expressions_test.go index 7a077f0..9f924e6 100644 --- a/expressions/expressions_test.go +++ b/expressions/expressions_test.go @@ -112,7 +112,7 @@ var evaluatorTestBindings = (map[string]interface{}{ }) func TestEvaluator(t *testing.T) { - settings := NewSettings() + settings := NewConfig() settings.AddFilter("length", strings.Count) context := NewContext(evaluatorTestBindings, settings) for i, test := range evaluatorTests { @@ -125,7 +125,7 @@ func TestEvaluator(t *testing.T) { } func TestHelpers(t *testing.T) { - context := NewContext(map[string]interface{}{}, NewSettings()) + context := NewContext(map[string]interface{}{}, NewConfig()) k := Constant(10) v, err := k.Evaluate(context) diff --git a/filters/filter_test.go b/filters/filter_test.go index e86ba63..c23c9ea 100644 --- a/filters/filter_test.go +++ b/filters/filter_test.go @@ -160,7 +160,7 @@ var filterTestBindings = map[string]interface{}{ } func TestFilters(t *testing.T) { - settings := expressions.NewSettings() + settings := expressions.NewConfig() AddStandardFilters(settings) context := expressions.NewContext(filterTestBindings, settings) diff --git a/filters/filters.go b/filters/filters.go index f97ea51..4a74853 100644 --- a/filters/filters.go +++ b/filters/filters.go @@ -19,7 +19,7 @@ import ( ) // AddStandardFilters defines the standard Liquid filters. -func AddStandardFilters(settings expressions.Settings) { // nolint: gocyclo +func AddStandardFilters(settings expressions.Config) { // nolint: gocyclo // values settings.AddFilter("default", func(value, defaultValue interface{}) interface{} { if value == nil || value == false || generics.IsEmpty(value) { diff --git a/render/blocks.go b/render/blocks.go index 6eba32a..16f909c 100644 --- a/render/blocks.go +++ b/render/blocks.go @@ -35,22 +35,22 @@ func (c *blockDef) isStartTag() bool { return !c.isBranchTag && !c.isEndTag } -func (s Settings) addBlockDef(ct *blockDef) { +func (s Config) addBlockDef(ct *blockDef) { 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] return ct, found } type blockDefBuilder struct { - s Settings + s Config tag *blockDef } // 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} s.addBlockDef(ct) s.addBlockDef(&blockDef{name: "end" + name, isEndTag: true, parent: ct}) diff --git a/render/config.go b/render/config.go new file mode 100644 index 0000000..367e1b9 --- /dev/null +++ b/render/config.go @@ -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) +} diff --git a/render/context.go b/render/context.go index 95034f9..aa7b874 100644 --- a/render/context.go +++ b/render/context.go @@ -7,34 +7,11 @@ import ( // Context is the evaluation context for chunk AST rendering. type Context struct { bindings map[string]interface{} - settings Settings -} - -// 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 + settings Config } // 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. // TODO this isn't really the right place for this. 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)) } diff --git a/render/parser.go b/render/parser.go index 684194f..1a1e241 100644 --- a/render/parser.go +++ b/render/parser.go @@ -7,13 +7,13 @@ import ( ) // 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, "") return s.parseChunks(tokens) } // 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. type frame struct { cd *blockDef // saved local ccd @@ -106,7 +106,7 @@ func (s Settings) parseChunks(chunks []Chunk) (ASTNode, error) { // nolint: gocy } // nolint: gocyclo -func (s Settings) evaluateBuilders(n ASTNode) error { +func (s Config) evaluateBuilders(n ASTNode) error { switch n := n.(type) { case *ASTBlock: for _, child := range n.Body { diff --git a/render/parser_test.go b/render/parser_test.go index 232687f..4ddf398 100644 --- a/render/parser_test.go +++ b/render/parser_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/require" ) -func addParserTestTags(s Settings) { +func addParserTestTags(s Config) { s.AddBlock("case").Branch("when") s.AddBlock("comment") s.AddBlock("for").Governs([]string{"break"}) @@ -38,7 +38,7 @@ var parserTests = []struct{ in string }{ } func TestParseErrors(t *testing.T) { - settings := NewSettings() + settings := NewConfig() addParserTestTags(settings) for i, test := range parseErrorTests { 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) { - settings := NewSettings() + settings := NewConfig() addParserTestTags(settings) for i, test := range parserTests { t.Run(fmt.Sprintf("%02d", i+1), func(t *testing.T) { diff --git a/render/render_context.go b/render/render_context.go index a062c8b..834262f 100644 --- a/render/render_context.go +++ b/render/render_context.go @@ -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) { diff --git a/render/render_test.go b/render/render_test.go index 33d951a..2ec03ed 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -10,7 +10,7 @@ import ( "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) { a := c.Args return func(w io.Writer, c RenderContext) error { @@ -76,7 +76,7 @@ var renderTestBindings = map[string]interface{}{ } func TestRender(t *testing.T) { - settings := NewSettings() + settings := NewConfig() addRenderTestTags(settings) context := NewContext(renderTestBindings, settings) for i, test := range renderTests { @@ -92,7 +92,7 @@ func TestRender(t *testing.T) { } func TestRenderErrors(t *testing.T) { - settings := NewSettings() + settings := NewConfig() addRenderTestTags(settings) context := NewContext(renderTestBindings, settings) for i, test := range renderErrorTests { diff --git a/render/tags.go b/render/tags.go index e006954..a24ed35 100644 --- a/render/tags.go +++ b/render/tags.go @@ -17,12 +17,12 @@ func assignTagDef(source string) (func(io.Writer, RenderContext) error, error) { } // 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 } // 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] return td, ok } diff --git a/tags/tags.go b/tags/tags.go index 9f8d3db..1b0590d 100644 --- a/tags/tags.go +++ b/tags/tags.go @@ -10,7 +10,7 @@ import ( ) // 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, // but it ignores any syntax specified here. loopTags := []string{"break", "continue", "cycle"} diff --git a/tags/tags_test.go b/tags/tags_test.go index d44e4fe..d53df0d 100644 --- a/tags/tags_test.go +++ b/tags/tags_test.go @@ -133,7 +133,7 @@ var bindings = map[string]interface{}{ } func TestParseErrors(t *testing.T) { - settings := render.NewSettings() + settings := render.NewConfig() AddStandardTags(settings) for i, test := range parseErrorTests { 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) { - settings := render.NewSettings() + settings := render.NewConfig() AddStandardTags(settings) context := render.NewContext(bindings, settings) for i, test := range tagTests { diff --git a/template.go b/template.go index cd58c45..85ef302 100644 --- a/template.go +++ b/template.go @@ -8,7 +8,7 @@ import ( type template struct { ast render.ASTNode - settings render.Settings + settings render.Config } // Render executes the template within the bindings environment.