-
Notifications
You must be signed in to change notification settings - Fork 277
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support configurable template parsers (#317)
- Loading branch information
1 parent
5257e26
commit 62e3978
Showing
9 changed files
with
218 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package template | ||
|
||
// IdentityParser is an Parser that does no parsing and returns tempalte string unchanged. | ||
type IdentityParser struct{} | ||
|
||
func (IdentityParser) Cacheable() bool { | ||
// Caching is not necessary because Parse is cheap. | ||
return false | ||
} | ||
|
||
func (IdentityParser) Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error) { | ||
return &identityParsedTemplate{src: src}, nil | ||
} | ||
|
||
type identityParsedTemplate struct { | ||
src string | ||
} | ||
|
||
func (t *identityParsedTemplate) Execute(data any) (string, error) { | ||
return t.src, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
// Package template defines a generic interface for template parsers and implementations of that interface. | ||
package template | ||
|
||
// Parser parses strings into executable templates. | ||
type Parser interface { | ||
// Parse parses src and returns a ParsedTemplate. | ||
Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error) | ||
|
||
// Cacheable returns true if Parse returns ParsedTemplates that are always safe to cache. | ||
Cacheable() bool | ||
} | ||
|
||
// ParsedTemplate is an executable template. | ||
type ParsedTemplate interface { | ||
// Execute applies a parsed template to the specified data. | ||
Execute(data any) (string, error) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package template | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"text/template" | ||
) | ||
|
||
// TextParser is a Parser that uses text/template. | ||
type TextParser struct { | ||
LeftDelim string | ||
RightDelim string | ||
Funcs template.FuncMap | ||
Option string | ||
} | ||
|
||
func (te *TextParser) Cacheable() bool { | ||
return te.Funcs == nil | ||
} | ||
|
||
func (te *TextParser) Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error) { | ||
if leftDelim == "" { | ||
leftDelim = te.LeftDelim | ||
} | ||
if leftDelim == "" { | ||
leftDelim = "{{" | ||
} | ||
if !strings.Contains(src, leftDelim) { | ||
// Fast path to avoid parsing a template that has no actions. | ||
return &identityParsedTemplate{src: src}, nil | ||
} | ||
|
||
if rightDelim == "" { | ||
rightDelim = te.RightDelim | ||
} | ||
if rightDelim == "" { | ||
rightDelim = "}}" | ||
} | ||
|
||
tmpl, err := template.New("").Delims(leftDelim, rightDelim).Funcs(te.Funcs).Parse(src) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return &parsedTextTemplate{tmpl: tmpl}, nil | ||
} | ||
|
||
type parsedTextTemplate struct { | ||
tmpl *template.Template | ||
} | ||
|
||
func (t *parsedTextTemplate) Execute(data any) (string, error) { | ||
var buf bytes.Buffer | ||
if err := t.tmpl.Execute(&buf, data); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,36 @@ | ||
package internal | ||
|
||
import ( | ||
"bytes" | ||
"strings" | ||
"sync" | ||
gotemplate "text/template" | ||
|
||
"github.com/nicksnyder/go-i18n/v2/i18n/template" | ||
) | ||
|
||
// Template stores the template for a string. | ||
// Template stores the template for a string and a cached version of the parsed template if they are cacheable. | ||
type Template struct { | ||
Src string | ||
LeftDelim string | ||
RightDelim string | ||
|
||
parseOnce sync.Once | ||
parsedTemplate *gotemplate.Template | ||
parsedTemplate template.ParsedTemplate | ||
parseError error | ||
} | ||
|
||
func (t *Template) Execute(funcs gotemplate.FuncMap, data interface{}) (string, error) { | ||
leftDelim := t.LeftDelim | ||
if leftDelim == "" { | ||
leftDelim = "{{" | ||
} | ||
if !strings.Contains(t.Src, leftDelim) { | ||
// Fast path to avoid parsing a template that has no actions. | ||
return t.Src, nil | ||
} | ||
|
||
var gt *gotemplate.Template | ||
func (t *Template) Execute(parser template.Parser, data interface{}) (string, error) { | ||
var pt template.ParsedTemplate | ||
var err error | ||
if funcs == nil { | ||
if parser.Cacheable() { | ||
t.parseOnce.Do(func() { | ||
// If funcs is nil, then we only need to parse this template once. | ||
t.parsedTemplate, t.parseError = gotemplate.New("").Delims(t.LeftDelim, t.RightDelim).Parse(t.Src) | ||
t.parsedTemplate, t.parseError = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) | ||
}) | ||
gt, err = t.parsedTemplate, t.parseError | ||
pt, err = t.parsedTemplate, t.parseError | ||
} else { | ||
gt, err = gotemplate.New("").Delims(t.LeftDelim, t.RightDelim).Funcs(funcs).Parse(t.Src) | ||
pt, err = parser.Parse(t.Src, t.LeftDelim, t.RightDelim) | ||
} | ||
|
||
if err != nil { | ||
return "", err | ||
} | ||
var buf bytes.Buffer | ||
if err := gt.Execute(&buf, data); err != nil { | ||
return "", err | ||
} | ||
return buf.String(), nil | ||
return pt.Execute(data) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters