上传底层编码库代码优化

This commit is contained in:
liwei1dao 2022-07-12 20:38:49 +08:00
parent fc28127008
commit 3e119d80df
11 changed files with 41 additions and 18 deletions

View File

@ -100,7 +100,7 @@ func (this *Codec) DecoderOf(typ reflect2.Type) core.IDecoder {
Encoders: map[reflect2.Type]core.IEncoder{}, Encoders: map[reflect2.Type]core.IEncoder{},
} }
ptrType := typ.(*reflect2.UnsafePtrType) ptrType := typ.(*reflect2.UnsafePtrType)
decoder = factory.DecoderOfType(ctx, ptrType) decoder = factory.DecoderOfType(ctx, ptrType.Elem())
this.addDecoderToCache(cacheKey, decoder) this.addDecoderToCache(cacheKey, decoder)
return decoder return decoder
} }

View File

@ -180,7 +180,7 @@ func (this *onePtrEncoder) EncodeToMapJson(ptr unsafe.Pointer) (ret map[string]s
err = fmt.Errorf("encoder %T not support EncodeToMapJson", this.encoder) err = fmt.Errorf("encoder %T not support EncodeToMapJson", this.encoder)
return return
} else { } else {
return encoderMapJson.EncodeToMapJson(ptr) return encoderMapJson.EncodeToMapJson(unsafe.Pointer(&ptr))
} }
} }

View File

@ -84,7 +84,7 @@ type mapEncoder struct {
elemEncoder core.IEncoder elemEncoder core.IEncoder
} }
func (codec *mapEncoder) GetType() reflect.Kind { func (this *mapEncoder) GetType() reflect.Kind {
return reflect.Map return reflect.Map
} }
func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
@ -106,7 +106,7 @@ func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteObjectEnd() stream.WriteObjectEnd()
} }
func (this *mapEncoder) EncodeToMapString(ptr unsafe.Pointer) (ret map[string]string, err error) { func (this *mapEncoder) EncodeToMapJson(ptr unsafe.Pointer) (ret map[string]string, err error) {
ret = make(map[string]string) ret = make(map[string]string)
keystream := this.codec.BorrowStream() keystream := this.codec.BorrowStream()
elemstream := this.codec.BorrowStream() elemstream := this.codec.BorrowStream()

View File

@ -81,7 +81,12 @@ func (this *OptionalEncoder) EncodeToMapJson(ptr unsafe.Pointer) (ret map[string
err = fmt.Errorf("encoder %T not support EncodeToMapJson", this.ValueEncoder) err = fmt.Errorf("encoder %T not support EncodeToMapJson", this.ValueEncoder)
return return
} else { } else {
return encoderMapJson.EncodeToMapJson(ptr) if *((*unsafe.Pointer)(ptr)) == nil {
err = fmt.Errorf("encoder ptr is nil")
return
} else {
return encoderMapJson.EncodeToMapJson(*((*unsafe.Pointer)(ptr)))
}
} }
} }

View File

@ -22,6 +22,22 @@ type Test1Data struct {
Value int Value int
} }
func Test_sys_slice(t *testing.T) {
if err := log.OnInit(nil); err != nil {
fmt.Printf("log init err:%v", err)
return
}
if sys, err := codec.NewSys(); err != nil {
fmt.Printf("gin init err:%v", err)
} else {
data := []*Test1Data{{"liwe", 1}, {"liwe2", 2}}
d, err := sys.MarshalJson(data)
fmt.Printf("codec Marshal d:%s err:%v", d, err)
data = []*Test1Data{}
err = sys.UnmarshalJson(d, &data)
fmt.Printf("codec UnmarshalJson data:%v err:%v", data, err)
}
}
func Test_sys_json(t *testing.T) { func Test_sys_json(t *testing.T) {
if err := log.OnInit(nil); err != nil { if err := log.OnInit(nil); err != nil {
fmt.Printf("log init err:%v", err) fmt.Printf("log init err:%v", err)

View File

@ -408,11 +408,13 @@ func (this *MCompModel) GetList(uid string, data interface{}) (err error) {
} }
keys[_id] = key keys[_id] = key
} }
if err = this.Redis.HMSetForMap(this.ukey(uid), keys); err != nil { if len(keys) > 0 {
if err = this.Redis.HMSet(this.ukey(uid), keys); err != nil {
return return
} }
} }
} }
}
return err return err
} }

View File

@ -44,7 +44,7 @@ func (this *apiComp) Getlist(session comm.IUserSession, req *pb.ItemsGetlistReq)
tempgrids = this.module.configure.GetPackItemByType(items, req.IType) tempgrids = this.module.configure.GetPackItemByType(items, req.IType)
modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids)) modifys = make([]*pb.DB_UserItemData, 0, len(tempgrids))
dels = make([]string, 0, len(tempgrids)) dels = make([]string, 0, len(tempgrids))
grids = make([]*pb.DB_UserItemData, 0, len(grids)) grids = make([]*pb.DB_UserItemData, 0, len(items))
nt = time.Now().Unix() nt = time.Now().Unix()
for _, v := range tempgrids { for _, v := range tempgrids {
if v.ETime > 0 && v.ETime < nt { //已经过期 if v.ETime > 0 && v.ETime < nt { //已经过期

View File

@ -83,10 +83,10 @@ func TestMain(m *testing.M) {
} }
func Test_Modules(t *testing.T) { func Test_Modules(t *testing.T) {
data, _ := ptypes.MarshalAny(&pb.ItemsGetlistReq{}) data, _ := ptypes.MarshalAny(&pb.ItemsGetlistReq{IType: 9})
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62bd7bc609320345a244f372", MainType: "items", SubType: "getlist", Message: data}, &pb.RPCMessageReply{}) reply := &pb.RPCMessageReply{}
// items, err := module.db_comp.Pack_QueryUserPack("liwei1dao") s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62cd5952f72cc4bdc2d85f6b", MainType: "items", SubType: "getlist", Message: data}, reply)
log.Debugf("data:%v", data) log.Debugf("reply:%v", reply)
} }
func Test_Modules_AddItems(t *testing.T) { func Test_Modules_AddItems(t *testing.T) {
@ -95,5 +95,5 @@ func Test_Modules_AddItems(t *testing.T) {
FuncName: "Test_Modules_AddItems", FuncName: "Test_Modules_AddItems",
Describe: "测试模块接口", Describe: "测试模块接口",
}, "0_62c259916d8cf3e4e06311a8", map[int32]int32{10001: 1000}) }, "0_62c259916d8cf3e4e06311a8", map[int32]int32{10001: 1000})
log.Debugf("Test_Modules_AddItems:%v code:%v", code) log.Debugf("Test_Modules_AddItems code:%v", code)
} }

View File

@ -9,7 +9,7 @@ import (
) )
func (this *apiComp) AddResCheck(session comm.IUserSession, req *pb.UserAddResReq) (code pb.ErrorCode) { func (this *apiComp) AddResCheck(session comm.IUserSession, req *pb.UserAddResReq) (code pb.ErrorCode) {
if req.Res.A == "" || req.Res.T == "" || req.Res.N > 0 { if req.Res.A == "" || req.Res.T == "" || req.Res.N <= 0 {
code = pb.ErrorCode_ReqParameterError code = pb.ErrorCode_ReqParameterError
} }
return return

View File

@ -95,7 +95,7 @@ func (this *SCompGateRoute) RegisterRoute(methodName string, comp reflect.Value,
func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessage, reply *pb.RPCMessageReply) (err error) { func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessage, reply *pb.RPCMessageReply) (err error) {
defer func() { //程序异常 收集异常信息传递给前端显示 defer func() { //程序异常 收集异常信息传递给前端显示
if r := recover(); r != nil { if r := recover(); r != nil {
buf := make([]byte, 1024) buf := make([]byte, 4096)
l := runtime.Stack(buf, false) l := runtime.Stack(buf, false)
reply.Code = pb.ErrorCode_Exception reply.Code = pb.ErrorCode_Exception
reply.ErrorMessage = fmt.Sprintf("%v: %s", r, buf[:l]) reply.ErrorMessage = fmt.Sprintf("%v: %s", r, buf[:l])
@ -130,7 +130,7 @@ func (this *SCompGateRoute) ReceiveMsg(ctx context.Context, args *pb.AgentMessag
errcode := pb.ErrorCode(handlereturn[0].Int()) errcode := pb.ErrorCode(handlereturn[0].Int())
errdata := handlereturn[1].Interface() errdata := handlereturn[1].Interface()
if errcode != pb.ErrorCode_Success { //处理返货错误码 返回用户错误信息 if errcode != pb.ErrorCode_Success { //处理返货错误码 返回用户错误信息
log.Errorf("[Handle Api] method:%s uid:%s msg:%v code:%d", method, msg, errcode) log.Errorf("[Handle Api] method:%s msg:%v code:%d", method, msg, errcode)
reply.Code = errcode reply.Code = errcode
reply.ErrorMessage = pb.GetErrorCodeMsg(errcode) reply.ErrorMessage = pb.GetErrorCodeMsg(errcode)
if errdata != nil { if errdata != nil {

View File

@ -54,8 +54,8 @@ func Test_GetList(t *testing.T) {
GetList(&heroes) GetList(&heroes)
} }
func Test_GetList0(t *testing.T) { func Test_GetList0(t *testing.T) {
heroes := []*DBHero{} // heroes := []*DBHero{}
GetListO(&heroes) // GetListO(&heroes)
} }
func BenchmarkMarsh(b *testing.B) { func BenchmarkMarsh(b *testing.B) {