Skip to content

Commit 92e4a87

Browse files
authored
Merge pull request #402 from leonklingele/fix-go1.5-regression-template
Bring back Go 1.4 and 1.5 support
2 parents adfd225 + 1432179 commit 92e4a87

File tree

3 files changed

+90
-11
lines changed

3 files changed

+90
-11
lines changed

src/app/spreed-webrtc-server/main.go

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ import (
2525
"bytes"
2626
"crypto/rand"
2727
"encoding/hex"
28-
"encoding/json"
2928
"flag"
3029
"fmt"
3130
"html/template"
@@ -209,17 +208,8 @@ func runner(runtime phoenix.Runtime) error {
209208
}
210209

211210
// Load templates.
212-
templateFuncMap := template.FuncMap{
213-
"json": func(obj interface{}) (template.HTML, error) {
214-
data, err := json.Marshal(obj)
215-
if err != nil {
216-
return "", err
217-
}
218-
return template.HTML(data), nil
219-
},
220-
}
221211
templates = template.New("")
222-
templates.Delims("<%", "%>").Funcs(templateFuncMap)
212+
templates.Delims("<%", "%>").Funcs(templateFuncMap())
223213

224214
// Load html templates folder
225215
err = filepath.Walk(path.Join(rootFolder, "html"), func(path string, info os.FileInfo, err error) error {
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Spreed WebRTC.
3+
* Copyright (C) 2013-2016 struktur AG
4+
*
5+
* This file is part of Spreed WebRTC.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package main
23+
24+
import (
25+
"encoding/json"
26+
"html/template"
27+
)
28+
29+
func templateFuncMap() template.FuncMap {
30+
return template.FuncMap{
31+
"json": func(obj interface{}) (template.JS, error) {
32+
data, err := json.Marshal(obj)
33+
if err != nil {
34+
return "", err
35+
}
36+
return template.JS(data), nil
37+
},
38+
}
39+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/*
2+
* Spreed WebRTC.
3+
* Copyright (C) 2013-2016 struktur AG
4+
*
5+
* This file is part of Spreed WebRTC.
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as published by
9+
* the Free Software Foundation, either version 3 of the License, or
10+
* (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*
20+
*/
21+
22+
package main
23+
24+
import (
25+
"bytes"
26+
"html/template"
27+
"testing"
28+
)
29+
30+
const (
31+
templateString = `<script type="application/json">{{json .}}</script>`
32+
expectedString = `<script type="application/json">{"name":"Peter"}</script>`
33+
)
34+
35+
type testPerson struct {
36+
Name string `json:"name"`
37+
}
38+
39+
func TestHTMLTemplateWithJSON(t *testing.T) {
40+
tmpl := template.New("").Funcs(templateFuncMap())
41+
if _, err := tmpl.Parse(templateString); err != nil {
42+
t.Fatalf("Could not parse template '%s': %s", templateString, err.Error())
43+
}
44+
buf := bytes.NewBuffer(nil)
45+
tmpl.Execute(buf, testPerson{Name: "Peter"})
46+
out := buf.String()
47+
if out != expectedString {
48+
t.Fatalf("Strings do not match: got '%s', want '%s'", out, expectedString)
49+
}
50+
}

0 commit comments

Comments
 (0)