From 1f56e500839e7fab34e15f97d57ff0acfada4272 Mon Sep 17 00:00:00 2001 From: Manu Mtz-Almeida Date: Thu, 4 Jun 2015 05:25:21 +0200 Subject: [PATCH] Using Render() instead of Write() --- context.go | 2 +- render/data.go | 2 +- render/html.go | 2 +- render/json.go | 4 ++-- render/redirect.go | 2 +- render/render.go | 2 +- render/render_test.go | 12 ++++++------ render/text.go | 2 +- render/xml.go | 2 +- 9 files changed, 15 insertions(+), 15 deletions(-) diff --git a/context.go b/context.go index 5ff915da..b54db507 100644 --- a/context.go +++ b/context.go @@ -284,7 +284,7 @@ func (c *Context) Header(key, value string) { func (c *Context) Render(code int, r render.Render) { c.writermem.WriteHeader(code) - if err := r.Write(c.Writer); err != nil { + if err := r.Render(c.Writer); err != nil { debugPrintError(err) c.AbortWithError(500, err).SetType(ErrorTypeRender) } diff --git a/render/data.go b/render/data.go index 1e86a43f..efa75d55 100644 --- a/render/data.go +++ b/render/data.go @@ -11,7 +11,7 @@ type Data struct { Data []byte } -func (r Data) Write(w http.ResponseWriter) error { +func (r Data) Render(w http.ResponseWriter) error { if len(r.ContentType) > 0 { w.Header()["Content-Type"] = []string{r.ContentType} } diff --git a/render/html.go b/render/html.go index 160b13cd..3595ec5a 100644 --- a/render/html.go +++ b/render/html.go @@ -57,7 +57,7 @@ func (r HTMLDebug) loadTemplate() *template.Template { panic("the HTML debug render was created without files or glob pattern") } -func (r HTML) Write(w http.ResponseWriter) error { +func (r HTML) Render(w http.ResponseWriter) error { w.Header()["Content-Type"] = htmlContentType if len(r.Name) == 0 { return r.Template.Execute(w, r.Data) diff --git a/render/json.go b/render/json.go index 2fc4dfd0..38474cbb 100644 --- a/render/json.go +++ b/render/json.go @@ -21,12 +21,12 @@ type ( var jsonContentType = []string{"application/json; charset=utf-8"} -func (r JSON) Write(w http.ResponseWriter) error { +func (r JSON) Render(w http.ResponseWriter) error { w.Header()["Content-Type"] = jsonContentType return json.NewEncoder(w).Encode(r.Data) } -func (r IndentedJSON) Write(w http.ResponseWriter) error { +func (r IndentedJSON) Render(w http.ResponseWriter) error { w.Header()["Content-Type"] = jsonContentType jsonBytes, err := json.MarshalIndent(r.Data, "", " ") if err != nil { diff --git a/render/redirect.go b/render/redirect.go index e82b6532..d64e4d75 100644 --- a/render/redirect.go +++ b/render/redirect.go @@ -15,7 +15,7 @@ type Redirect struct { Location string } -func (r Redirect) Write(w http.ResponseWriter) error { +func (r Redirect) Render(w http.ResponseWriter) error { if r.Code < 300 || r.Code > 308 { panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code)) } diff --git a/render/render.go b/render/render.go index 1bcae933..eadc31b8 100644 --- a/render/render.go +++ b/render/render.go @@ -7,7 +7,7 @@ package render import "net/http" type Render interface { - Write(http.ResponseWriter) error + Render(http.ResponseWriter) error } var ( diff --git a/render/render_test.go b/render/render_test.go index d0bff547..7a6ffb7d 100644 --- a/render/render_test.go +++ b/render/render_test.go @@ -22,7 +22,7 @@ func TestRenderJSON(t *testing.T) { "foo": "bar", } - err := (JSON{data}).Write(w) + err := (JSON{data}).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n") @@ -36,7 +36,7 @@ func TestRenderIndentedJSON(t *testing.T) { "bar": "foo", } - err := (IndentedJSON{data}).Write(w) + err := (IndentedJSON{data}).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}") @@ -75,7 +75,7 @@ func TestRenderXML(t *testing.T) { "foo": "bar", } - err := (XML{data}).Write(w) + err := (XML{data}).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "bar") @@ -93,7 +93,7 @@ func TestRenderData(t *testing.T) { err := (Data{ ContentType: "image/png", Data: data, - }).Write(w) + }).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "#!PNG some raw data") @@ -106,7 +106,7 @@ func TestRenderString(t *testing.T) { err := (String{ Format: "hola %s %d", Data: []interface{}{"manu", 2}, - }).Write(w) + }).Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "hola manu 2") @@ -122,7 +122,7 @@ func TestRenderHTMLTemplate(t *testing.T) { "name": "alexandernyquist", }) - err := instance.Write(w) + err := instance.Render(w) assert.NoError(t, err) assert.Equal(t, w.Body.String(), "Hello alexandernyquist") diff --git a/render/text.go b/render/text.go index af76007b..97d60bb5 100644 --- a/render/text.go +++ b/render/text.go @@ -17,7 +17,7 @@ type String struct { var plainContentType = []string{"text/plain; charset=utf-8"} -func (r String) Write(w http.ResponseWriter) error { +func (r String) Render(w http.ResponseWriter) error { header := w.Header() if _, exist := header["Content-Type"]; !exist { header["Content-Type"] = plainContentType diff --git a/render/xml.go b/render/xml.go index 58eeecc4..eac0589f 100644 --- a/render/xml.go +++ b/render/xml.go @@ -15,7 +15,7 @@ type XML struct { var xmlContentType = []string{"application/xml; charset=utf-8"} -func (r XML) Write(w http.ResponseWriter) error { +func (r XML) Render(w http.ResponseWriter) error { w.Header()["Content-Type"] = xmlContentType return xml.NewEncoder(w).Encode(r.Data) }