73 lines
1.1 KiB
Go
Raw Normal View History

2023-06-04 22:54:54 +08:00
package utils
import (
2023-06-04 23:26:48 +08:00
"gorm.io/gorm/utils"
2023-06-04 22:54:54 +08:00
)
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:26:48 +08:00
Anchor: ToString(anchor),
End: ToPtr(true),
2023-06-04 22:54:54 +08:00
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(),
}
}
type Code int
const ok Code = 0
const failed Code = -1
func (code Code) ToInt() int {
return int(code)
}