2020-07-04 12:54:35 +02:00
|
|
|
package i18n
|
|
|
|
|
|
|
|
import "strings"
|
|
|
|
|
|
|
|
type Response struct {
|
2020-07-07 10:51:55 +02:00
|
|
|
Code int `json:"code"`
|
|
|
|
Err string `json:"error,omitempty"`
|
|
|
|
Msg string `json:"message,omitempty"`
|
|
|
|
Details string `json:"details,omitempty"`
|
2020-07-04 12:54:35 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r Response) String() string {
|
|
|
|
if r.Err != "" {
|
|
|
|
return r.Err
|
|
|
|
} else {
|
|
|
|
return r.Msg
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Response) LowerString() string {
|
|
|
|
return strings.ToLower(r.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r Response) Error() string {
|
|
|
|
return r.Err
|
|
|
|
}
|
|
|
|
|
2020-07-07 10:51:55 +02:00
|
|
|
func (r Response) Success() bool {
|
|
|
|
return r.Err == "" && r.Code < 400
|
|
|
|
}
|
|
|
|
|
2020-07-04 12:54:35 +02:00
|
|
|
func NewResponse(code int, id Message, params ...interface{}) Response {
|
|
|
|
if code < 400 {
|
|
|
|
return Response{Code: code, Msg: Msg(id, params...)}
|
|
|
|
} else {
|
|
|
|
return Response{Code: code, Err: Msg(id, params...)}
|
|
|
|
}
|
|
|
|
}
|