-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson.go
124 lines (117 loc) · 3.17 KB
/
json.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package bifrost
import (
"bytes"
"encoding/json"
"fmt"
"net/http"
"reflect"
"strings"
)
// JSONResponse set header content-type to json format
func JSONResponse(w http.ResponseWriter) {
w.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
}
// RequestJSONBody get json body format
func RequestJSONBody(r *http.Request, extract interface{}) error {
// check method
switch r.Method {
case http.MethodPut:
case http.MethodPost:
case http.MethodGet:
return fmt.Errorf("method is not allowed")
}
if r.Body == nil {
return fmt.Errorf("there is no content")
}
if err := json.NewDecoder(r.Body).Decode(&extract); err != nil {
return err
}
return nil
}
// ResponseJSONPayload set payload for response http
func ResponseJSONPayload(w http.ResponseWriter, r *http.Request, code int, responses ...interface{}) error {
w.Header().Set(HeaderContentType, MIMEApplicationJSONCharsetUTF8)
null := make(map[string]interface{})
resp := &Response{
Version: Version{
Label: "v1",
Number: "0.1.0",
},
Meta: Meta{Code: http.StatusText(code)},
Pagination: null,
}
if ver, ok := r.Context().Value(CtxVersion).(Version); ok {
resp.Version = ver
}
data := make(map[string]interface{}, 0)
for _, r := range responses {
switch r.(type) {
case Pagination:
resp.Pagination = r
case map[string]interface{}:
for k, v := range r.(map[string]interface{}) {
data[k] = v
}
default:
iType := reflect.TypeOf(r)
switch iType.Kind() {
case reflect.Slice, reflect.Array:
s := reflect.ValueOf(r)
if s.Len() < 1 {
continue
}
l := strings.Split(s.Index(0).Type().String(), ".")
dataList := make([]map[string]interface{}, 0)
for n := 0; n < s.Len(); n++ {
b, err := json.Marshal(s.Index(n).Interface())
if err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
tempData := make(map[string]interface{}, 0)
if err := json.Unmarshal(b, &tempData); err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
dataList = append(dataList, tempData)
}
data[ToDelimited(l[len(l)-1], '_')] = dataList
default:
b, err := json.Marshal(r)
if err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
tempData := make(map[string]interface{}, 0)
if err := json.Unmarshal(b, &tempData); err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
for k, v := range tempData {
data[k] = v
}
}
}
}
resp.Data = data
buf := &bytes.Buffer{}
enc := json.NewEncoder(buf)
enc.SetEscapeHTML(true)
if err := enc.Encode(resp); err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
w.WriteHeader(code)
_, err := w.Write(buf.Bytes())
if err != nil {
w.Header().Set("X-Content-Type-Options", "nosniff")
w.WriteHeader(http.StatusInternalServerError)
return err
}
return nil
}