Browse Source

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

tags/v1.0.11
liangzy 2 years ago
parent
commit
b120742220
4 changed files with 93 additions and 5 deletions
  1. +24
    -4
      response/json.go
  2. +9
    -0
      response/json_interface.go
  3. +28
    -1
      response/json_test.go
  4. +32
    -0
      response/response.go

+ 24
- 4
response/json.go 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,


+ 9
- 0
response/json_interface.go 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)
}

+ 28
- 1
response/json_test.go View File

@@ -1,10 +1,37 @@
package response

import (
"bytes"
"encoding/json"
"fmt"
"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) {
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")))
}

+ 32
- 0
response/response.go 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
}

Loading…
Cancel
Save