@@ -12,11 +12,27 @@ const ( | |||||
type Json struct { | type Json struct { | ||||
Code int `json:"code"` | Code int `json:"code"` | ||||
Data interface{} `json:"data"` | |||||
Data interface{} `json:"data,omitempty"` | |||||
Msg string `json:"msg"` | Msg string `json:"msg"` | ||||
Status bool `json:"status"` | 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 { | func (j *Json) String() string { | ||||
s, _ := j.ToString() | s, _ := j.ToString() | ||||
return s | return s | ||||
@@ -28,15 +44,19 @@ func (j *Json) ToString() (str string, err error) { | |||||
return bytes.NewBuffer(b).String(), err | return bytes.NewBuffer(b).String(), err | ||||
} | } | ||||
func NewJsonByDefaultSuccess(data ...interface{}) *Json { | |||||
func NewJsonByDefaultSuccess(data ...interface{}) JsonInterface { | |||||
return NewJson(true, 0, JsonMsgDefaultOk, data...) | return NewJson(true, 0, JsonMsgDefaultOk, data...) | ||||
} | } | ||||
func NewJsonByDefaultFailed(data ...interface{}) *Json { | |||||
func NewJsonByDefaultFailed(data ...interface{}) JsonInterface { | |||||
return NewJson(false, 1, JsonMsgDefaultFailed, data...) | 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 { | if len(data) > 0 { | ||||
return &Json{ | return &Json{ | ||||
Code: code, | Code: code, | ||||
@@ -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) | |||||
} |
@@ -1,10 +1,37 @@ | |||||
package response | package response | ||||
import ( | import ( | ||||
"bytes" | |||||
"encoding/json" | |||||
"fmt" | "fmt" | ||||
"testing" | "testing" | ||||
) | ) | ||||
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) { | func TestNewJsonByDefaultSuccess(t *testing.T) { | ||||
fmt.Println(NewJsonByDefaultSuccess([]int{1, 2, 3}).String()) | |||||
fmt.Println(NewJsonByDefaultSuccess()) | |||||
} | |||||
func TestName(t *testing.T) { | |||||
resp := JsonResponseGenerator{&A{Code: 232}} | |||||
fmt.Println(fmt.Sprintf("%+v", resp.NewJonsByFailed(123232, "hahah"))) | |||||
} | } |
@@ -1 +1,33 @@ | |||||
package response | 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 | |||||
} |