You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
133 lines
3.0 KiB
133 lines
3.0 KiB
package result
|
|
|
|
import (
|
|
"errors"
|
|
"github.com/gin-gonic/gin"
|
|
"net/http"
|
|
"strconv"
|
|
)
|
|
|
|
var (
|
|
Ok = NewResponse(WithCode(200), WithMsg("success"))
|
|
Created = NewResponse(WithCode(201), WithFlag(true), WithMsg("created"))
|
|
InvalidParams = NewResponse(WithCode(400), WithFlag(false), WithMsg("invalid params"))
|
|
Unauthorized = NewResponse(WithCode(401), WithFlag(false), WithMsg("unauthorized"))
|
|
Forbidden = NewResponse(WithCode(403), WithFlag(false), WithMsg("forbidden"))
|
|
NotFound = NewResponse(WithCode(404), WithFlag(false), WithMsg("not found"))
|
|
TooManyRequests = NewResponse(WithCode(429), WithFlag(false), WithMsg("too many requests"))
|
|
Wrong = NewResponse(WithCode(500), WithFlag(false), WithMsg("something wrong"))
|
|
)
|
|
|
|
type Response struct {
|
|
Code int `json:"code"`
|
|
Msg string `json:"msg"`
|
|
Data interface{} `json:"data"`
|
|
}
|
|
|
|
type ResponseOption func(*Response)
|
|
|
|
func NewResponse(options ...ResponseOption) *Response {
|
|
resp := &Response{}
|
|
for _, option := range options {
|
|
option(resp)
|
|
}
|
|
return resp
|
|
}
|
|
|
|
// WithCode code:code
|
|
func (r *Response) WithCode(code int) *Response {
|
|
r.Code = code
|
|
return r
|
|
}
|
|
|
|
// WithMsg msg:msg
|
|
func (r *Response) WithMsg(msg string) *Response {
|
|
r.Msg = msg
|
|
return r
|
|
}
|
|
|
|
// WithData data:data
|
|
func (r *Response) WithData(data interface{}) *Response {
|
|
r.Data = data
|
|
return r
|
|
}
|
|
|
|
// WithVoidData data:nil
|
|
func (r *Response) WithVoidData() *Response {
|
|
r.Data = nil
|
|
return r
|
|
}
|
|
|
|
// WithFlag data:{"flag":flag}
|
|
func (r *Response) WithFlag(flag bool) *Response {
|
|
r.Data = gin.H{"flag": flag}
|
|
return r
|
|
}
|
|
|
|
// WithFV data:{"flag":flag, "value":value}
|
|
func (r *Response) WithFV(flag bool, value interface{}) *Response {
|
|
r.Data = gin.H{"flag": flag, "value": value}
|
|
return r
|
|
}
|
|
|
|
// WithVoidFV data:{"flag":false, "value":nil}
|
|
func (r *Response) WithVoidFV() *Response {
|
|
r.Data = gin.H{"flag": false, "value": nil}
|
|
return r
|
|
}
|
|
|
|
// WithFullFV data:{"flag":true, "value":value}
|
|
func (r *Response) WithFullFV(value interface{}) *Response {
|
|
r.Data = gin.H{"flag": true, "value": value}
|
|
return r
|
|
}
|
|
|
|
func WithCode(code int) ResponseOption {
|
|
return func(resp *Response) {
|
|
resp.Code = code
|
|
}
|
|
}
|
|
|
|
func WithMsg(msg string) ResponseOption {
|
|
return func(resp *Response) {
|
|
resp.Msg = msg
|
|
}
|
|
}
|
|
|
|
func WithData(data interface{}) ResponseOption {
|
|
return func(resp *Response) {
|
|
resp.Data = data
|
|
}
|
|
}
|
|
|
|
func WithFlag(flag bool) ResponseOption {
|
|
return func(resp *Response) {
|
|
resp.Data = gin.H{"flag": flag}
|
|
}
|
|
}
|
|
|
|
func WithFlagValue(flag bool, value interface{}) ResponseOption {
|
|
return func(resp *Response) {
|
|
resp.Data = gin.H{"flag": flag, "value": value}
|
|
}
|
|
}
|
|
|
|
func (r *Response) Response(c *gin.Context, httpCode int) {
|
|
c.JSON(httpCode, &r)
|
|
}
|
|
|
|
func (r *Response) Success(c *gin.Context) {
|
|
r.Response(c, http.StatusOK)
|
|
}
|
|
|
|
func (r *Response) Failure(c *gin.Context) {
|
|
r.Response(c, http.StatusOK)
|
|
}
|
|
|
|
func (r *Response) Error(c *gin.Context) {
|
|
r.Response(c, http.StatusInternalServerError)
|
|
}
|
|
|
|
func (r *Response) ToError() error {
|
|
return errors.New("code:" + strconv.Itoa(r.Code) + ", msg:" + r.Msg)
|
|
}
|
|
|