-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[play] Try built in template to replace pongo2
- for #2, we may not need that much feature on pongo2 - the main drawback of golang's built in template is its syntax is quite strange - golang's template does support remove new line, though it would also trim tab
- Loading branch information
Showing
11 changed files
with
109 additions
and
15 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,5 +14,4 @@ func TestRenderDocument(t *testing.T) { | |
//t.Log(err) | ||
assert.Nil(err) | ||
assert.Equal("bar and 1 and 1", out) | ||
|
||
} |
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,4 @@ | ||
# Playground | ||
|
||
For testing language semantics and small benchmarks. Prototype and library examples are also put here. | ||
It also keeps some minimal code for reproduce/solve issues. |
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,83 @@ | ||
package std | ||
|
||
import ( | ||
"os" | ||
"text/template" | ||
"testing" | ||
"bytes" | ||
|
||
asst "github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestTemplate_Funcs(t *testing.T) { | ||
assert := asst.New(t) | ||
const tmplText = ` | ||
{{env "HOME"}} | ||
{{var "foo"}} | ||
` | ||
vars := map[string]string{"foo": "bar"} | ||
|
||
funcMap := template.FuncMap{ | ||
"env": func(name string) string { | ||
return os.Getenv(name) | ||
}, | ||
"var": func(name string) string { | ||
return vars[name] | ||
}, | ||
} | ||
|
||
tmpl, err := template.New("funcs test").Funcs(funcMap).Parse(tmplText) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
var b bytes.Buffer | ||
assert.Nil(tmpl.Execute(&b, "")) | ||
rendered := b.String() | ||
assert.Contains(rendered, os.Getenv("HOME"), "env function in template should be called") | ||
assert.Contains(rendered, "bar") | ||
|
||
// now we update vars | ||
vars["foo"] = "bar2" | ||
b.Reset() | ||
assert.Nil(tmpl.Execute(&b, "")) | ||
rendered = b.String() | ||
assert.Contains(rendered, "bar2", "var function should be using the updated value") | ||
} | ||
|
||
|
||
func TestTemplate_Range(t *testing.T) { | ||
assert := asst.New(t) | ||
// NOTE: the `-` is used to remove the following new line https://golang.org/pkg/text/template/#hdr-Text_and_spaces | ||
// FIXME: - will also remove space which would break yaml indent | ||
const tmplText = ` | ||
{{ range $name := var "databases" }} | ||
{{ $name -}}: | ||
{{ $db := var $name }} | ||
name: {{ $name }} | ||
type: {{ $db.type -}} | ||
{{ end }} | ||
` | ||
|
||
vars := map[string]interface{}{"foo": "barr"} | ||
vars["databases"] = []string{"cassandra", "mysql", "xephonk"} | ||
vars["cassandra"] = map[string]string{"type": "nosql"} | ||
vars["mysql"] = map[string]string{"type": "sql"} | ||
vars["xephonk"] = map[string]string{"type": "tsdb"} | ||
|
||
|
||
funcMap := template.FuncMap{ | ||
"env": func(name string) string { | ||
return os.Getenv(name) | ||
}, | ||
"var": func(name string) interface{} { | ||
return vars[name] | ||
}, | ||
} | ||
|
||
tmpl, err := template.New("range test").Funcs(funcMap).Parse(tmplText) | ||
assert.Nil(err) | ||
err = tmpl.Execute(os.Stdout, "") | ||
t.Log(err) | ||
assert.Nil(err) | ||
} |
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,4 +1,4 @@ | ||
/* | ||
Package requests is a pythonic HTTP library for Gopher | ||
*/ | ||
*/ | ||
package requests |
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