-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
184 additions
and
5 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package gtemplate | ||
|
||
import ( | ||
"errors" | ||
ghash "github.com/snail007/gmc/util/hash" | ||
"sync" | ||
) | ||
|
||
var DefaultRender = NewRender() | ||
|
||
type Render struct { | ||
tplCache map[string]*Template | ||
tplLock sync.Mutex | ||
funcMap map[string]interface{} | ||
leftDelims, | ||
rightDelims string | ||
} | ||
|
||
func NewRender() *Render { | ||
return &Render{ | ||
tplCache: map[string]*Template{}, | ||
funcMap: map[string]interface{}{}, | ||
} | ||
} | ||
|
||
func (s *Render) clearTemplateCache() { | ||
s.tplLock.Lock() | ||
defer s.tplLock.Unlock() | ||
s.tplCache = map[string]*Template{} | ||
} | ||
|
||
func (s *Render) Delims(left, right string) *Render { | ||
s.clearTemplateCache() | ||
s.leftDelims = left | ||
s.rightDelims = right | ||
return s | ||
} | ||
|
||
func (s *Render) AddFuncMap(m map[string]interface{}) *Render { | ||
if len(m) == 0 { | ||
return s | ||
} | ||
s.clearTemplateCache() | ||
for k, v := range m { | ||
s.funcMap[k] = v | ||
} | ||
return s | ||
} | ||
|
||
func (s *Render) getTemplate(tplBytes []byte) (*Template, error) { | ||
s.tplLock.Lock() | ||
defer s.tplLock.Unlock() | ||
id := ghash.Md5Bytes(tplBytes) | ||
if t, ok := s.tplCache[id]; ok { | ||
return t, nil | ||
} | ||
t := New() | ||
t.SetCtx(ctx) | ||
t.DdisableLogging() | ||
t.DisableLoadDefaultBinData() | ||
t.SetBinBytes(map[string][]byte{"tpl": tplBytes}) | ||
if s.leftDelims != "" && s.rightDelims != "" { | ||
t.Delims(s.leftDelims, s.rightDelims) | ||
} | ||
if len(s.funcMap) > 0 { | ||
t.Funcs(s.funcMap) | ||
} | ||
err := t.Parse() | ||
if err != nil { | ||
return nil, err | ||
} | ||
s.tplCache[id] = t | ||
return t, nil | ||
} | ||
|
||
// Parse the template, tplBytesOrString is []byte or string template | ||
func (s *Render) Parse(tplBytesOrString interface{}, tplData map[string]interface{}) (d []byte, err error) { | ||
var b []byte | ||
switch v := tplBytesOrString.(type) { | ||
case []byte: | ||
b = v | ||
case string: | ||
b = []byte(v) | ||
default: | ||
return nil, errors.New("wrong type of tpl data") | ||
} | ||
t, err := s.getTemplate(b) | ||
if err != nil { | ||
return | ||
} | ||
return t.Execute("tpl", tplData) | ||
} |
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,76 @@ | ||
package gtemplate | ||
|
||
import ( | ||
"github.com/stretchr/testify/assert" | ||
"testing" | ||
) | ||
|
||
func TestRender_Parse(t *testing.T) { | ||
// 创建一个 Render 实例 | ||
renderer := NewRender() | ||
|
||
// 定义模板内容 | ||
tplContent := `Hello, {{.Name}}!` | ||
|
||
// 定义模板数据 | ||
tplData := map[string]interface{}{ | ||
"Name": "World", | ||
} | ||
|
||
// 调用 Parse 方法解析模板 | ||
result, err := renderer.Parse([]byte(tplContent), tplData) | ||
if err != nil { | ||
t.Fatalf("Error parsing template: %v", err) | ||
} | ||
|
||
// 验证结果是否符合预期 | ||
expectedResult := "Hello, World!" | ||
if string(result) != expectedResult { | ||
t.Errorf("Expected result: %s, but got: %s", expectedResult, result) | ||
} | ||
|
||
// 测试 Delims 方法 | ||
renderer.Delims("[[", "]]") | ||
result, err = renderer.Parse([]byte(`Hello, [[.Name]]!`), tplData) | ||
if err != nil { | ||
t.Fatalf("Error parsing template with custom delimiters: %v", err) | ||
} | ||
|
||
expectedResult = "Hello, World!" | ||
if string(result) != expectedResult { | ||
t.Errorf("Expected result with custom delimiters: %s, but got: %s", expectedResult, result) | ||
} | ||
renderer.Delims("{{", "}}") | ||
|
||
// 测试 AddFuncMap 方法 | ||
funcMap := map[string]interface{}{ | ||
"Double": func(s string) string { | ||
return s + s | ||
}, | ||
} | ||
renderer.AddFuncMap(funcMap) | ||
result, err = renderer.Parse(`Doubled: {{Double .Name}}`, tplData) | ||
if err != nil { | ||
t.Fatalf("Error parsing template with added function: %v", err) | ||
} | ||
|
||
expectedResult = "Doubled: WorldWorld" | ||
if string(result) != expectedResult { | ||
t.Errorf("Expected result with added function: %s, but got: %s", expectedResult, result) | ||
} | ||
|
||
_, err = renderer.Parse(`Doubled: {{Double .Name}}`, tplData) | ||
assert.Nil(t, err) | ||
|
||
_, err = renderer.Parse(123, nil) | ||
assert.NotNil(t, err) | ||
|
||
_, err = renderer.Parse(`{{none .Name}}`, nil) | ||
assert.NotNil(t, err) | ||
|
||
_, err = renderer.Parse(`{{none .Name}`, nil) | ||
assert.NotNil(t, err) | ||
|
||
renderer.AddFuncMap(nil) | ||
assert.NotEmpty(t, renderer.funcMap) | ||
} |
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