-
Notifications
You must be signed in to change notification settings - Fork 2
/
result_vo.go
79 lines (63 loc) · 1.55 KB
/
result_vo.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
package ghelp
import (
"net/http"
"reflect"
"github.com/gin-gonic/gin"
)
const (
SUCCESS = 0
FAILED = 1
)
type (
ResultVO struct {
Data interface{} `json:"data"`
Code int8 `json:"code"`
Success bool `json:"success"`
Msg string `json:"msg"`
}
)
var success = ResultVO{Code: SUCCESS, Success: true}
var failed = ResultVO{Code: FAILED, Success: false}
func SuccessWith(data interface{}) ResultVO {
return ResultVO{Data: data, Code: SUCCESS, Success: true}
}
func Success() ResultVO {
return success
}
func Failed() ResultVO {
return failed
}
func FailedWithMsg(msg string) ResultVO {
return ResultVO{Msg: msg, Code: FAILED, Success: false}
}
func (result ResultVO) IsSuccess() bool {
return result.Code == SUCCESS
}
func (result ResultVO) IsFailed() bool {
return !result.IsSuccess()
}
// web util
func GinFailed(c *gin.Context) {
c.JSON(http.StatusOK, failed)
}
func GinFailedWithMsg(c *gin.Context, msg string) {
c.JSON(http.StatusOK, FailedWithMsg(msg))
}
func GinResultVO(c *gin.Context, result ResultVO) {
c.JSON(http.StatusOK, result)
}
func GinSuccess(c *gin.Context) {
c.JSON(http.StatusOK, success)
}
func GinSuccessWith(c *gin.Context, data interface{}) {
c.JSON(http.StatusOK, SuccessWith(data))
}
// https://stackoverflow.com/questions/13476349/check-for-nil-and-nil-interface-in-go
func GinResult(c *gin.Context, result interface{}) {
//
if result == nil || (reflect.ValueOf(result).Kind() == reflect.Ptr && reflect.ValueOf(result).IsNil()) {
GinFailed(c)
} else {
GinSuccessWith(c, result)
}
}