优化返回json, 添加json response 生成器

This commit is contained in:
liangzy 2021-08-03 15:58:35 +08:00
parent 6a7da70791
commit b120742220
4 changed files with 94 additions and 6 deletions

View File

@ -12,11 +12,27 @@ const (
type Json struct {
Code int `json:"code"`
Data interface{} `json:"data"`
Data interface{} `json:"data,omitempty"`
Msg string `json:"msg"`
Status bool `json:"status"`
}
func (j *Json) SetCode(code int) {
j.Code = code
}
func (j *Json) SetMessage(msg string) {
j.Msg = msg
}
func (j *Json) SetData(data interface{}) {
j.Data = data
}
func (j *Json) SetStatus(status bool) {
j.Status = status
}
func (j *Json) String() string {
s, _ := j.ToString()
return s
@ -28,15 +44,19 @@ func (j *Json) ToString() (str string, err error) {
return bytes.NewBuffer(b).String(), err
}
func NewJsonByDefaultSuccess(data ...interface{}) *Json {
func NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
return NewJson(true, 0, JsonMsgDefaultOk, data...)
}
func NewJsonByDefaultFailed(data ...interface{}) *Json {
func NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
return NewJson(false, 1, JsonMsgDefaultFailed, data...)
}
func NewJson(status bool, code int, msg string, data ...interface{}) *Json {
func NewJonsByFailed(code int, msg string) JsonInterface {
return NewJson(false, code, msg)
}
func NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface {
if len(data) > 0 {
return &Json{
Code: code,

View File

@ -0,0 +1,9 @@
package response
type JsonInterface interface {
SetCode(code int)
SetMessage(msg string)
SetData(data interface{})
SetStatus(status bool)
ToString() (str string, err error)
}

View File

@ -1,10 +1,37 @@
package response
import (
"bytes"
"encoding/json"
"fmt"
"testing"
)
func TestNewJsonByDefaultSuccess(t *testing.T) {
fmt.Println(NewJsonByDefaultSuccess([]int{1, 2, 3}).String())
type A struct {
Code int `json:"ret"`
Json
}
func (j *A) String() string {
s, _ := j.ToString()
return s
}
func (a *A) ToString() (str string, err error) {
var b []byte
b, err = json.Marshal(a)
return bytes.NewBuffer(b).String(), err
}
func (a *A) SetCode(code int) {
a.Code = code
}
func TestNewJsonByDefaultSuccess(t *testing.T) {
fmt.Println(NewJsonByDefaultSuccess())
}
func TestName(t *testing.T) {
resp := JsonResponseGenerator{&A{Code: 232}}
fmt.Println(fmt.Sprintf("%+v", resp.NewJonsByFailed(123232, "hahah")))
}

View File

@ -1 +1,33 @@
package response
import "reflect"
type JsonResponseGenerator struct {
Interface JsonInterface
}
func (j JsonResponseGenerator) NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
return j.NewJson(true, 0, JsonMsgDefaultOk, data...)
}
func (j JsonResponseGenerator) NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
return j.NewJson(false, 1, JsonMsgDefaultFailed, data...)
}
func (j JsonResponseGenerator) NewJonsByFailed(code int, msg string) JsonInterface {
return j.NewJson(false, code, msg)
}
func (j JsonResponseGenerator) NewJson(status bool, code int, msg string, data ...interface{}) JsonInterface {
jPtr := reflect.New(reflect.TypeOf(j.Interface).Elem())
if b, ok := jPtr.Interface().(JsonInterface); ok {
b.SetStatus(status)
b.SetCode(code)
b.SetMessage(msg)
if len(data) > 0 {
b.SetData(data[0])
}
return b
}
return nil
}