Skip to content

Commit 0b5b297

Browse files
author
Jing Dong
committed
Merge pull request #115 from gomes/master
Add common string functions
2 parents b645fbf + 490116d commit 0b5b297

File tree

2 files changed

+75
-1
lines changed

2 files changed

+75
-1
lines changed

services/template/template.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"text/template"
66
"github.com/QubitProducts/bamboo/services/service"
7+
"strings"
78
)
89

910
func hasKey(data map[string]service.Service, appId string) bool {
@@ -20,7 +21,14 @@ func getService(data map[string]service.Service, appId string) service.Service {
2021
Returns string content of a rendered template
2122
*/
2223
func RenderTemplate(templateName string, templateContent string, data interface{}) (string, error) {
23-
funcMap := template.FuncMap{ "hasKey": hasKey, "getService": getService }
24+
funcMap := template.FuncMap{
25+
"hasKey": hasKey,
26+
"getService": getService,
27+
"Split": strings.Split,
28+
"Join": strings.Join,
29+
"Replace": strings.Replace,
30+
"ToUpper": strings.ToUpper,
31+
"ToLower": strings.ToLower}
2432

2533
tpl := template.Must(template.New(templateName).Funcs(funcMap).Parse(templateContent))
2634

services/template/template_test.go

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,69 @@ func TestTemplateWriter(t *testing.T) {
1717
})
1818
})
1919
}
20+
21+
func TestTemplateSplitFunction(t *testing.T) {
22+
Convey("#RenderTemplate", t, func() {
23+
templateName := "templateName"
24+
params := map[string]string{"id": "app", "domain": "example.com"}
25+
26+
templateContent := "{{.id}}{{range Split .domain \".\"}} {{.}}{{end}}"
27+
Convey("should split domain as two different strings", func() {
28+
content, _ := RenderTemplate(templateName, templateContent, params)
29+
So(content, ShouldEqual, "app example com")
30+
})
31+
})
32+
}
33+
34+
func TestTemplateJoinFunction(t *testing.T) {
35+
Convey("#RenderTemplate", t, func() {
36+
templateName := "templateName"
37+
domains := []string{"example.com", "example.net"}
38+
params := map[string][]string{"domains": domains}
39+
40+
templateContent := "{{Join .domains \", \"}}"
41+
Convey("should create a list of two domains separated by comma", func() {
42+
content, _ := RenderTemplate(templateName, templateContent, params)
43+
So(content, ShouldEqual, "example.com, example.net")
44+
})
45+
})
46+
}
47+
48+
func TestTemplateReplaceFunction(t *testing.T) {
49+
Convey("#RenderTemplate", t, func() {
50+
templateName := "templateName"
51+
params := map[string]string{"domain": "example.com"}
52+
53+
templateContent := "{{Replace .domain \"com\" \"net\" 1}}"
54+
Convey("should replace example.com into example.net", func() {
55+
content, _ := RenderTemplate(templateName, templateContent, params)
56+
So(content, ShouldEqual, "example.net")
57+
})
58+
})
59+
}
60+
61+
func TestTemplateToUpperFunction(t *testing.T) {
62+
Convey("#RenderTemplate", t, func() {
63+
templateName := "templateName"
64+
params := map[string]string{"id": "app", "domain": "example.com"}
65+
66+
templateContent := "{{.id}} {{ToUpper .domain}}"
67+
Convey("should transform example.com upper case", func() {
68+
content, _ := RenderTemplate(templateName, templateContent, params)
69+
So(content, ShouldEqual, "app EXAMPLE.COM")
70+
})
71+
})
72+
}
73+
74+
func TestTemplateToLowerFunction(t *testing.T) {
75+
Convey("#RenderTemplate", t, func() {
76+
templateName := "templateName"
77+
params := map[string]string{"domain": "EXAMPLE.COM"}
78+
79+
templateContent := "{{ToLower .domain}}"
80+
Convey("should transform example.com lower case", func() {
81+
content, _ := RenderTemplate(templateName, templateContent, params)
82+
So(content, ShouldEqual, "example.com")
83+
})
84+
})
85+
}

0 commit comments

Comments
 (0)