Skip to content

Commit

Permalink
ParseTemplate -> Parse
Browse files Browse the repository at this point in the history
  • Loading branch information
nicksnyder committed Jan 30, 2024
1 parent ae4eb8a commit c1404ca
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
10 changes: 6 additions & 4 deletions v2/i18n/template/template_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ type ParsedTemplate interface {
Execute(data any) (string, error)
}

// Parser parses strings into executable templates.
type Parser interface {
ParseTemplate(src, leftDelim, rightDelim string) (ParsedTemplate, error)
// Parse parses src and returns a ParsedTemplate.
Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error)

// Cacheable returns true if the ParsedTemplate returned by ParseTemplate is safe to cache.
// Cacheable returns true if ParsedTemplates returned by ParseTemplate are always safe to cache.
Cacheable() bool
}

Expand All @@ -25,7 +27,7 @@ func (IdentityParser) Cacheable() bool {
return false
}

func (IdentityParser) ParseTemplate(src, leftDelim, rightDelim string) (ParsedTemplate, error) {
func (IdentityParser) Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error) {
return &identityParsedTemplate{src: src}, nil
}

Expand All @@ -49,7 +51,7 @@ func (te *TextParser) Cacheable() bool {
return te.Funcs == nil
}

func (te *TextParser) ParseTemplate(src, leftDelim, rightDelim string) (ParsedTemplate, error) {
func (te *TextParser) Parse(src, leftDelim, rightDelim string) (ParsedTemplate, error) {
if leftDelim == "" {
leftDelim = te.LeftDelim
}
Expand Down
6 changes: 3 additions & 3 deletions v2/internal/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"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
Expand All @@ -22,11 +22,11 @@ func (t *Template) Execute(parser template.Parser, data interface{}) (string, er
var err error
if parser.Cacheable() {
t.parseOnce.Do(func() {
t.parsedTemplate, t.parseError = parser.ParseTemplate(t.Src, t.LeftDelim, t.RightDelim)
t.parsedTemplate, t.parseError = parser.Parse(t.Src, t.LeftDelim, t.RightDelim)
})
pt, err = t.parsedTemplate, t.parseError
} else {
pt, err = parser.ParseTemplate(t.Src, t.LeftDelim, t.RightDelim)
pt, err = parser.Parse(t.Src, t.LeftDelim, t.RightDelim)
}

if err != nil {
Expand Down

0 comments on commit c1404ca

Please sign in to comment.