45 lines
1.1 KiB
Go
45 lines
1.1 KiB
Go
package logic
|
||
|
||
import (
|
||
"lone-services/pkg/utils"
|
||
"lone-services/pkg/validate"
|
||
admin "lone-services/rpc/admin/pb"
|
||
"reflect"
|
||
|
||
jsoniter "github.com/json-iterator/go"
|
||
)
|
||
|
||
type BaseLogic struct {
|
||
}
|
||
|
||
func (l *BaseLogic) checkParams(in interface{}, v validate.IValidator) *admin.Response {
|
||
rv := reflect.ValueOf(in)
|
||
if rv.Kind() != reflect.Ptr || rv.IsNil() {
|
||
return &admin.Response{
|
||
Code: utils.ErrorParams.Code,
|
||
Msg: "request must be non‑nil proto pointer",
|
||
}
|
||
}
|
||
|
||
resp := validate.ValidateFromProto(in, v)
|
||
if resp != utils.StringEmpty {
|
||
return &admin.Response{
|
||
Code: utils.ErrorParams.Code,
|
||
Msg: resp,
|
||
}
|
||
}
|
||
return nil
|
||
}
|
||
|
||
func (l *BaseLogic) fail(status utils.Status) (*admin.Response, error) {
|
||
return l.out(status, status.Msg)
|
||
}
|
||
func (l *BaseLogic) out(status utils.Status, msg string) (*admin.Response, error) {
|
||
return &admin.Response{Code: status.Code, Msg: msg}, nil
|
||
}
|
||
|
||
func (l *BaseLogic) ok(data any) (*admin.Response, error) {
|
||
buf, _ := jsoniter.Marshal(data)
|
||
return &admin.Response{Code: utils.Ok.Code, Msg: utils.Ok.Msg, Data: string(buf)}, nil
|
||
}
|