forked from go-ozzo/ozzo-routing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
39 lines (33 loc) · 978 Bytes
/
writer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
package routing
import (
"fmt"
"net/http"
)
// DataWriter is used by Context.Write() to write arbitrary data into an HTTP response.
type DataWriter interface {
// SetHeader sets necessary response headers.
SetHeader(http.ResponseWriter)
// Write writes the given data into the response.
Write(http.ResponseWriter, interface{}) error
}
// DefaultDataWriter writes the given data in an HTTP response.
// If the data is neither string nor byte array, it will use fmt.Fprint() to write it into the response.
var DefaultDataWriter DataWriter = &dataWriter{}
type dataWriter struct{}
func (w *dataWriter) SetHeader(res http.ResponseWriter) {}
func (w *dataWriter) Write(res http.ResponseWriter, data interface{}) error {
var bytes []byte
switch data.(type) {
case []byte:
bytes = data.([]byte)
case string:
bytes = []byte(data.(string))
default:
if data != nil {
_, err := fmt.Fprint(res, data)
return err
}
}
_, err := res.Write(bytes)
return err
}