81 lines
1.3 KiB
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package utils
import (
"code.freebrio.com/fb-go/lib/errors"
"code.freebrio.com/fb-go/lib/fbl"
)
type Result struct {
Code int `json:"code"`
Msg string `json:"msg"`
Data any `json:"data"`
}
// 空集合
var emptyList = make([]interface{}, 0)
type PageResult[T any] struct {
List []T `json:"list"`
Anchor string `json:"anchor,omitempty"`
End *bool `json:"end,omitempty"`
}
func Ok(Data any) Result {
return Result{
Code: ok.ToInt(),
Data: Data,
}
}
func OkPageResult[T any](anchor any, end bool, list []T) Result {
return Result{
Code: ok.ToInt(),
Data: PageResult[T]{
List: list,
2023-06-04 23:07:02 +08:00
Anchor: utils.ToString(anchor),
2023-06-04 22:54:54 +08:00
End: &end,
},
}
}
func OkEmptyPageResult(anchor any) Result {
return Result{
Code: ok.ToInt(),
Data: PageResult[interface{}]{
2023-06-04 23:07:02 +08:00
Anchor: utils.ToString(anchor),
2023-06-04 22:54:54 +08:00
End: fbl.ToPtr(true),
List: emptyList,
},
}
}
func Failed(msg string) Result {
return Result{
Code: failed.ToInt(),
Msg: msg,
}
}
func Error(err error) Result {
return Result{
Code: failed.ToInt(),
Msg: err.Error(),
}
}
func SysErr(err error) Result {
return Result{
Code: errors.DEFAULT_SYS_ERROR_CODE,
Msg: err.Error(),
}
}
type Code int
const ok Code = 0
const failed Code = -1
func (code Code) ToInt() int {
return int(code)
}