template/interpolate: user variables

This commit is contained in:
Mitchell Hashimoto 2015-05-15 21:18:27 -07:00
parent a4b5e08fe4
commit 1e745d9508
3 changed files with 44 additions and 2 deletions

View File

@ -94,9 +94,13 @@ func funcGenTimestamp(ctx *Context) interface{} {
}
func funcGenUser(ctx *Context) interface{} {
return func() string {
return func(k string) string {
if ctx == nil || ctx.UserVariables == nil {
return ""
}
return ctx.UserVariables[k]
}
}
func funcGenUuid(ctx *Context) interface{} {

View File

@ -142,3 +142,37 @@ func TestFuncTimestamp(t *testing.T) {
}
}
}
func TestFuncUser(t *testing.T) {
cases := []struct {
Input string
Output string
}{
{
`{{user "foo"}}`,
`foo`,
},
{
`{{user "what"}}`,
``,
},
}
ctx := &Context{
UserVariables: map[string]string{
"foo": "foo",
},
}
for _, tc := range cases {
i := &I{Value: tc.Input}
result, err := i.Render(ctx)
if err != nil {
t.Fatalf("Input: %s\n\nerr: %s", tc.Input, err)
}
if result != tc.Output {
t.Fatalf("Input: %s\n\nGot: %s", tc.Input, result)
}
}
}

View File

@ -11,6 +11,10 @@ type Context struct {
// Data is the data for the template that is available
Data interface{}
// UserVariables is the mapping of user variables that the
// "user" function reads from.
UserVariables map[string]string
// DisableEnv disables the env function
DisableEnv bool
}