beegoinit/response/response.go

34 lines
881 B
Go

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
}