beegoinit/response/response.go

42 lines
1.1 KiB
Go

package response
import "reflect"
type JsonResponseGenerator struct {
Interface JsonInterface
}
func (j JsonResponseGenerator) NewJsonByDefaultSuccess(data ...interface{}) JsonInterface {
ptr := j.NewJson(true, 0, JsonMsgDefaultOk, data...)
ptr.SetCode(ptr.GetDefaultSuccessCode())
return ptr
}
func (j JsonResponseGenerator) NewJsonByDefaultFailed(data ...interface{}) JsonInterface {
ptr := j.NewJson(false, 1, JsonMsgDefaultFailed, data...)
ptr.SetCode(ptr.GetDefaultFailedCode())
return ptr
}
func (j JsonResponseGenerator) NewJsonByFailed(code int, msg string) JsonInterface {
return j.NewJson(false, code, msg)
}
func (j JsonResponseGenerator) NewJsonByFailedWithData(code int, msg string, data interface{}) JsonInterface {
return j.NewJson(false, code, msg, data)
}
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
}