Browse Source

Merge pull request #402 from leonklingele/fix-go1.5-regression-template

Bring back Go 1.4 and 1.5 support
pull/404/head
Joachim Bauch 9 years ago committed by GitHub
parent
commit
92e4a87c5d
  1. 12
      src/app/spreed-webrtc-server/main.go
  2. 39
      src/app/spreed-webrtc-server/template.go
  3. 50
      src/app/spreed-webrtc-server/template_test.go

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

@ -25,7 +25,6 @@ import ( @@ -25,7 +25,6 @@ import (
"bytes"
"crypto/rand"
"encoding/hex"
"encoding/json"
"flag"
"fmt"
"html/template"
@ -209,17 +208,8 @@ func runner(runtime phoenix.Runtime) error { @@ -209,17 +208,8 @@ func runner(runtime phoenix.Runtime) error {
}
// Load templates.
templateFuncMap := template.FuncMap{
"json": func(obj interface{}) (template.HTML, error) {
data, err := json.Marshal(obj)
if err != nil {
return "", err
}
return template.HTML(data), nil
},
}
templates = template.New("")
templates.Delims("<%", "%>").Funcs(templateFuncMap)
templates.Delims("<%", "%>").Funcs(templateFuncMap())
// Load html templates folder
err = filepath.Walk(path.Join(rootFolder, "html"), func(path string, info os.FileInfo, err error) error {

39
src/app/spreed-webrtc-server/template.go

@ -0,0 +1,39 @@ @@ -0,0 +1,39 @@
/*
* Spreed WebRTC.
* Copyright (C) 2013-2016 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"encoding/json"
"html/template"
)
func templateFuncMap() template.FuncMap {
return template.FuncMap{
"json": func(obj interface{}) (template.JS, error) {
data, err := json.Marshal(obj)
if err != nil {
return "", err
}
return template.JS(data), nil
},
}
}

50
src/app/spreed-webrtc-server/template_test.go

@ -0,0 +1,50 @@ @@ -0,0 +1,50 @@
/*
* Spreed WebRTC.
* Copyright (C) 2013-2016 struktur AG
*
* This file is part of Spreed WebRTC.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
package main
import (
"bytes"
"html/template"
"testing"
)
const (
templateString = `<script type="application/json">{{json .}}</script>`
expectedString = `<script type="application/json">{"name":"Peter"}</script>`
)
type testPerson struct {
Name string `json:"name"`
}
func TestHTMLTemplateWithJSON(t *testing.T) {
tmpl := template.New("").Funcs(templateFuncMap())
if _, err := tmpl.Parse(templateString); err != nil {
t.Fatalf("Could not parse template '%s': %s", templateString, err.Error())
}
buf := bytes.NewBuffer(nil)
tmpl.Execute(buf, testPerson{Name: "Peter"})
out := buf.String()
if out != expectedString {
t.Fatalf("Strings do not match: got '%s', want '%s'", out, expectedString)
}
}
Loading…
Cancel
Save