/
expand.go
43 lines (37 loc) · 1022 Bytes
/
expand.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package template
import (
"bytes"
"fmt"
"text/template/parse"
)
// Expand parses a given string replacing template keys with attributes values.
func Expand(data string, attrs map[string]interface{}) (string, error) {
buf := bytes.NewBuffer(nil)
// Parse response for template keys
trees, err := parse.Parse("inline", data, "", "")
if err != nil {
return "", ErrInvalidTemplate
}
for _, node := range trees["inline"].Root.Nodes {
if text, ok := node.(*parse.TextNode); ok {
buf.Write(text.Text)
} else if action, ok := node.(*parse.ActionNode); ok {
if len(action.Pipe.Cmds) != 1 {
continue
}
for _, arg := range action.Pipe.Cmds[0].Args {
if field, ok := arg.(*parse.FieldNode); ok {
if len(field.Ident) == 1 {
// Replace template key with attribute value
if v, ok := attrs[field.Ident[0]]; ok && v != nil {
buf.WriteString(fmt.Sprintf("%v", v))
}
} else {
return "", ErrInvalidTemplate
}
}
}
}
}
return buf.String(), nil
}