-
Notifications
You must be signed in to change notification settings - Fork 0
/
writer.go
97 lines (75 loc) · 1.78 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
package yod
import (
"errors"
"net/http"
)
const (
writerNotSupportDataType = "writer not support data type"
statusIsWrong = "status code is wrong"
)
type Writer struct {
w http.ResponseWriter
}
func (w *Writer) SetHeader(key, value string) {
w.w.Header().Set(key, value)
}
func (w *Writer) internalServerErrorString() error {
w.w.WriteHeader(http.StatusInternalServerError)
_, err := w.w.Write([]byte(writerNotSupportDataType))
return err
}
func (w *Writer) OK(v interface{}) error {
if s, ok := v.(string); ok {
w.w.WriteHeader(http.StatusOK)
_, err := w.w.Write([]byte(s))
return err
}
return w.InternalServerError(writerNotSupportDataType)
}
func (w *Writer) InternalServerError(v interface{}) error {
if s, ok := v.(string); ok {
w.w.WriteHeader(http.StatusInternalServerError)
_, err := w.w.Write([]byte(s))
return err
}
return w.internalServerErrorString()
}
func (w *Writer) String(code int, s string) error {
w.w.WriteHeader(code)
_, err := w.w.Write([]byte(s))
return err
}
func (w *Writer) Informational(code int, v interface{}) error {
if code > 199 {
return errors.New(statusIsWrong)
}
if s, ok := v.(string); ok {
return w.String(code, s)
}
return w.internalServerErrorString()
}
func (w *Writer) Successful(code int, v interface{}) error {
return nil
}
func (w *Writer) Redirection(code int, v interface{}) error {
return nil
}
func (w *Writer) ClientError(code int, v interface{}) error {
return nil
}
func (w *Writer) ServerError(code int, v interface{}) error {
return nil
}
type StringWriter = Writer
type JSONWriter struct {
ResponseWriter
}
func (JSONWriter) Value(code int, v interface{}) error {
return nil
}
type XMLWriter struct {
ResponseWriter
}
func (XMLWriter) Value(code int, v interface{}) error {
return nil
}