New Render API

This commit is contained in:
Manu Mtz-Almeida
2015-05-18 15:45:24 +02:00
parent 3066c35754
commit 947b53d4a2
13 changed files with 190 additions and 229 deletions
+14 -15
View File
@@ -5,21 +5,20 @@ import (
"net/http"
)
type plainTextRender struct{}
type String struct {
Format string
Data []interface{}
}
func (_ plainTextRender) Render(w http.ResponseWriter, code int, data ...interface{}) error {
format := data[0].(string)
values := data[1].([]interface{})
WritePlainText(w, code, format, values)
func (r String) Write(w http.ResponseWriter) error {
header := w.Header()
if _, exist := header["Content-Type"]; !exist {
header.Set("Content-Type", "text/plain; charset=utf-8")
}
if len(r.Data) > 0 {
fmt.Fprintf(w, r.Format, r.Data...)
} else {
w.Write([]byte(r.Format))
}
return nil
}
func WritePlainText(w http.ResponseWriter, code int, format string, values []interface{}) {
writeHeader(w, code, "text/plain; charset=utf-8")
// we assume w.Write can not fail, is that right?
if len(values) > 0 {
fmt.Fprintf(w, format, values...)
} else {
w.Write([]byte(format))
}
}