5 changed files with 71 additions and 46 deletions
@ -0,0 +1,51 @@ |
|||
package response |
|||
|
|||
import ( |
|||
"bytes" |
|||
"encoding/json" |
|||
"golib.gaore.com/GaoreGo/grlogs" |
|||
) |
|||
|
|||
const ( |
|||
JsonMsgDefaultOk = "ok" |
|||
JsonMsgDefaultFailed = "unknown" |
|||
) |
|||
|
|||
type Json struct { |
|||
Code int `json:"code"` |
|||
Data interface{} `json:"data"` |
|||
Msg string `json:"msg"` |
|||
Status bool `json:"status"` |
|||
} |
|||
|
|||
func (j *Json) String() string { |
|||
b, err := json.Marshal(j) |
|||
grlogs.Debug(err) |
|||
return bytes.NewBuffer(b).String() |
|||
} |
|||
|
|||
func NewJsonByDefaultSuccess(data ...interface{}) *Json { |
|||
return NewJson(true, 0, JsonMsgDefaultOk, data...) |
|||
} |
|||
|
|||
func NewJsonByDefaultFailed(data ...interface{}) *Json { |
|||
return NewJson(false, 1, JsonMsgDefaultFailed, data...) |
|||
} |
|||
|
|||
func NewJson(status bool, code int, msg string, data ...interface{}) *Json { |
|||
if len(data) > 0 { |
|||
return &Json{ |
|||
Code: code, |
|||
Data: data[0], |
|||
Msg: msg, |
|||
Status: status, |
|||
} |
|||
} else { |
|||
return &Json{ |
|||
Code: code, |
|||
Data: nil, |
|||
Msg: msg, |
|||
Status: status, |
|||
} |
|||
} |
|||
} |
@ -1,44 +0,0 @@ |
|||
package json |
|||
|
|||
type Json struct { |
|||
Code int `json:"code"` |
|||
Data interface{} `json:"data"` |
|||
Msg string `json:"msg"` |
|||
Status bool `json:"status"` |
|||
} |
|||
|
|||
func GetDefaultJsonSuccessResponse() *Json { |
|||
return &Json{ |
|||
Code: 0, |
|||
Data: nil, |
|||
Msg: "ok", |
|||
Status: true, |
|||
} |
|||
} |
|||
|
|||
func GetDefaultJsonFailedResponse() *Json { |
|||
return &Json{ |
|||
Code: 1, |
|||
Data: nil, |
|||
Msg: "unknow", |
|||
Status: false, |
|||
} |
|||
} |
|||
|
|||
func New(status bool, code int, msg string, data ...interface{}) *Json { |
|||
if len(data) > 0 { |
|||
return &Json{ |
|||
Code: code, |
|||
Data: data[0], |
|||
Msg: msg, |
|||
Status: status, |
|||
} |
|||
} else { |
|||
return &Json{ |
|||
Code: code, |
|||
Data: nil, |
|||
Msg: msg, |
|||
Status: status, |
|||
} |
|||
} |
|||
} |
@ -0,0 +1,10 @@ |
|||
package response |
|||
|
|||
import ( |
|||
"fmt" |
|||
"testing" |
|||
) |
|||
|
|||
func TestNewJsonByDefaultSuccess(t *testing.T) { |
|||
fmt.Println(NewJsonByDefaultSuccess([]int{1, 2, 3}).String()) |
|||
} |
@ -0,0 +1,8 @@ |
|||
package response |
|||
|
|||
type Xml struct { |
|||
Code int `xml:"code"` |
|||
Data interface{} `xml:"data"` |
|||
Msg string `xml:"msg"` |
|||
Status bool `xml:"status"` |
|||
} |
Loading…
Reference in new issue