/
response.go
55 lines (47 loc) · 1.43 KB
/
response.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
package think
import (
"encoding/json"
"errors"
"fmt"
"reflect"
"github.com/thinkoner/thinkgo/context"
)
// Json Create a new HTTP Response with JSON data
func Json(v interface{}) *context.Response {
c, _ := json.Marshal(v)
return context.NewResponse().SetContent(string(c)).SetContentType("application/json")
}
// Text Create a new HTTP Response with TEXT data
func Text(s string) *context.Response {
return context.NewResponse().SetContent(s).SetContentType("text/plain")
}
// Text Create a new HTTP Response with HTML data
func Html(s string) *context.Response {
return context.NewResponse().SetContent(s)
}
func Response(v interface{}) *context.Response {
r := context.NewResponse()
r.SetContentType("text/plain")
var content string
t := reflect.TypeOf(v)
switch t.Kind() {
case reflect.Bool:
content = fmt.Sprintf("%t", v)
case reflect.String:
content = fmt.Sprintf("%s", v)
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint16, reflect.Uint32, reflect.Uint64:
content = fmt.Sprintf("%d", v)
case reflect.Float32, reflect.Float64:
content = fmt.Sprintf("%v", v)
default:
r.SetContentType("application/json")
b, err := json.Marshal(v)
if err != nil {
panic(errors.New(`The Response content must be a string, numeric, boolean, slice, map or can be encoded as json a string, "` + t.Name() + `" given.`))
}
content = string(b)
}
r.SetContent(content)
return r
}