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) {
|
||||
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)
|
||||
}
|
||||
|
|
|
@ -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}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
|
|
|
@ -7,7 +7,7 @@ package render
|
|||
import "net/http"
|
||||
|
||||
type Render interface {
|
||||
Write(http.ResponseWriter) error
|
||||
Render(http.ResponseWriter) error
|
||||
}
|
||||
|
||||
var (
|
||||
|
|
|
@ -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(), "<map><foo>bar</foo></map>")
|
||||
|
@ -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")
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue