-
Notifications
You must be signed in to change notification settings - Fork 0
/
JsonResponse.go
52 lines (43 loc) · 1017 Bytes
/
JsonResponse.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
package barnacle
import "encoding/json"
type JsonResponse interface {
ToByteArray() []byte
}
func jsonResponseToByteArray(jr JsonResponse) []byte {
var err error
ba, err := json.Marshal(jr)
if err != nil {
return []byte("")
}
return ba
}
type OkJsonResponse struct {
Status string `json:"status"`
Body interface{} `json:"body"`
}
func (jr OkJsonResponse) ToByteArray() []byte {
return jsonResponseToByteArray(jr)
}
// NewJsonResponse() is a constructor for JsonResponse
func NewOkJsonResponse(b string) JsonResponse {
js := new(interface{})
json.Unmarshal([]byte(b), &js)
return &OkJsonResponse{
Status: "ok",
Body: js,
}
}
type FailJsonResponse struct {
Status string `json:"status"`
Message string `json:"message"`
}
func (jr FailJsonResponse) ToByteArray() []byte {
return jsonResponseToByteArray(jr)
}
// NewJsonResponse() is a constructor for JsonResponse
func NewFailJsonResponse(m string) JsonResponse {
return &FailJsonResponse{
Status: "fail",
Message: m,
}
}