mirror of https://github.com/gin-gonic/gin.git
Using Render() instead of Write()
This commit is contained in:
parent
92475baba6
commit
1f56e50083
|
@ -284,7 +284,7 @@ func (c *Context) Header(key, value string) {
|
||||||
|
|
||||||
func (c *Context) Render(code int, r render.Render) {
|
func (c *Context) Render(code int, r render.Render) {
|
||||||
c.writermem.WriteHeader(code)
|
c.writermem.WriteHeader(code)
|
||||||
if err := r.Write(c.Writer); err != nil {
|
if err := r.Render(c.Writer); err != nil {
|
||||||
debugPrintError(err)
|
debugPrintError(err)
|
||||||
c.AbortWithError(500, err).SetType(ErrorTypeRender)
|
c.AbortWithError(500, err).SetType(ErrorTypeRender)
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ type Data struct {
|
||||||
Data []byte
|
Data []byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r Data) Write(w http.ResponseWriter) error {
|
func (r Data) Render(w http.ResponseWriter) error {
|
||||||
if len(r.ContentType) > 0 {
|
if len(r.ContentType) > 0 {
|
||||||
w.Header()["Content-Type"] = []string{r.ContentType}
|
w.Header()["Content-Type"] = []string{r.ContentType}
|
||||||
}
|
}
|
||||||
|
|
|
@ -57,7 +57,7 @@ func (r HTMLDebug) loadTemplate() *template.Template {
|
||||||
panic("the HTML debug render was created without files or glob pattern")
|
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
|
w.Header()["Content-Type"] = htmlContentType
|
||||||
if len(r.Name) == 0 {
|
if len(r.Name) == 0 {
|
||||||
return r.Template.Execute(w, r.Data)
|
return r.Template.Execute(w, r.Data)
|
||||||
|
|
|
@ -21,12 +21,12 @@ type (
|
||||||
|
|
||||||
var jsonContentType = []string{"application/json; charset=utf-8"}
|
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
|
w.Header()["Content-Type"] = jsonContentType
|
||||||
return json.NewEncoder(w).Encode(r.Data)
|
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
|
w.Header()["Content-Type"] = jsonContentType
|
||||||
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
jsonBytes, err := json.MarshalIndent(r.Data, "", " ")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -15,7 +15,7 @@ type Redirect struct {
|
||||||
Location string
|
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 {
|
if r.Code < 300 || r.Code > 308 {
|
||||||
panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
|
panic(fmt.Sprintf("Cannot redirect with status code %d", r.Code))
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,7 @@ package render
|
||||||
import "net/http"
|
import "net/http"
|
||||||
|
|
||||||
type Render interface {
|
type Render interface {
|
||||||
Write(http.ResponseWriter) error
|
Render(http.ResponseWriter) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
|
|
|
@ -22,7 +22,7 @@ func TestRenderJSON(t *testing.T) {
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := (JSON{data}).Write(w)
|
err := (JSON{data}).Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
assert.Equal(t, w.Body.String(), "{\"foo\":\"bar\"}\n")
|
||||||
|
@ -36,7 +36,7 @@ func TestRenderIndentedJSON(t *testing.T) {
|
||||||
"bar": "foo",
|
"bar": "foo",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := (IndentedJSON{data}).Write(w)
|
err := (IndentedJSON{data}).Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}")
|
assert.Equal(t, w.Body.String(), "{\n \"bar\": \"foo\",\n \"foo\": \"bar\"\n}")
|
||||||
|
@ -75,7 +75,7 @@ func TestRenderXML(t *testing.T) {
|
||||||
"foo": "bar",
|
"foo": "bar",
|
||||||
}
|
}
|
||||||
|
|
||||||
err := (XML{data}).Write(w)
|
err := (XML{data}).Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
|
assert.Equal(t, w.Body.String(), "<map><foo>bar</foo></map>")
|
||||||
|
@ -93,7 +93,7 @@ func TestRenderData(t *testing.T) {
|
||||||
err := (Data{
|
err := (Data{
|
||||||
ContentType: "image/png",
|
ContentType: "image/png",
|
||||||
Data: data,
|
Data: data,
|
||||||
}).Write(w)
|
}).Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "#!PNG some raw data")
|
assert.Equal(t, w.Body.String(), "#!PNG some raw data")
|
||||||
|
@ -106,7 +106,7 @@ func TestRenderString(t *testing.T) {
|
||||||
err := (String{
|
err := (String{
|
||||||
Format: "hola %s %d",
|
Format: "hola %s %d",
|
||||||
Data: []interface{}{"manu", 2},
|
Data: []interface{}{"manu", 2},
|
||||||
}).Write(w)
|
}).Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "hola manu 2")
|
assert.Equal(t, w.Body.String(), "hola manu 2")
|
||||||
|
@ -122,7 +122,7 @@ func TestRenderHTMLTemplate(t *testing.T) {
|
||||||
"name": "alexandernyquist",
|
"name": "alexandernyquist",
|
||||||
})
|
})
|
||||||
|
|
||||||
err := instance.Write(w)
|
err := instance.Render(w)
|
||||||
|
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
assert.Equal(t, w.Body.String(), "Hello alexandernyquist")
|
||||||
|
|
|
@ -17,7 +17,7 @@ type String struct {
|
||||||
|
|
||||||
var plainContentType = []string{"text/plain; charset=utf-8"}
|
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()
|
header := w.Header()
|
||||||
if _, exist := header["Content-Type"]; !exist {
|
if _, exist := header["Content-Type"]; !exist {
|
||||||
header["Content-Type"] = plainContentType
|
header["Content-Type"] = plainContentType
|
||||||
|
|
|
@ -15,7 +15,7 @@ type XML struct {
|
||||||
|
|
||||||
var xmlContentType = []string{"application/xml; charset=utf-8"}
|
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
|
w.Header()["Content-Type"] = xmlContentType
|
||||||
return xml.NewEncoder(w).Encode(r.Data)
|
return xml.NewEncoder(w).Encode(r.Data)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue