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