上传编解码系统优化

This commit is contained in:
liwei1dao 2022-07-12 14:24:28 +08:00
parent e2ee6c421e
commit afecd0e065
13 changed files with 195 additions and 26 deletions

View File

@ -1,6 +1,7 @@
package core package core
import ( import (
"reflect"
"unsafe" "unsafe"
"github.com/modern-go/reflect2" "github.com/modern-go/reflect2"
@ -113,6 +114,7 @@ type (
} }
//Json 编码器 //Json 编码器
IEncoder interface { IEncoder interface {
GetType() reflect.Kind
IsEmpty(ptr unsafe.Pointer) bool IsEmpty(ptr unsafe.Pointer) bool
Encode(ptr unsafe.Pointer, stream IStream) Encode(ptr unsafe.Pointer, stream IStream)
} }
@ -127,6 +129,7 @@ type (
//Json 解码器 //Json 解码器
IDecoder interface { IDecoder interface {
GetType() reflect.Kind
Decode(ptr unsafe.Pointer, extra IExtractor) Decode(ptr unsafe.Pointer, extra IExtractor)
} }
//MapJson 解码器 //MapJson 解码器

View File

@ -132,6 +132,9 @@ type rootDecoder struct {
decoder core.IDecoder decoder core.IDecoder
} }
func (this *rootDecoder) GetType() reflect.Kind {
return reflect.Ptr
}
func (this *rootDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *rootDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
this.decoder.Decode(ptr, extra) this.decoder.Decode(ptr, extra)
} }
@ -140,6 +143,9 @@ type rootEncoder struct {
encoder core.IEncoder encoder core.IEncoder
} }
func (this *rootEncoder) GetType() reflect.Kind {
return reflect.Ptr
}
func (this *rootEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *rootEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
this.encoder.Encode(ptr, stream) this.encoder.Encode(ptr, stream)
} }
@ -157,6 +163,10 @@ type onePtrEncoder struct {
encoder core.IEncoder encoder core.IEncoder
} }
func (this *onePtrEncoder) GetType() reflect.Kind {
return reflect.Ptr
}
func (this *onePtrEncoder) IsEmpty(ptr unsafe.Pointer) bool { func (this *onePtrEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return this.encoder.IsEmpty(unsafe.Pointer(&ptr)) return this.encoder.IsEmpty(unsafe.Pointer(&ptr))
} }
@ -179,6 +189,9 @@ type lazyErrorDecoder struct {
err error err error
} }
func (this *lazyErrorDecoder) GetType() reflect.Kind {
return reflect.Ptr
}
func (this *lazyErrorDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *lazyErrorDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if extra.Error() == nil { if extra.Error() == nil {
extra.SetErr(this.err) extra.SetErr(this.err)
@ -189,6 +202,10 @@ type lazyErrorEncoder struct {
err error err error
} }
func (this *lazyErrorEncoder) GetType() reflect.Kind {
return reflect.Ptr
}
func (this *lazyErrorEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *lazyErrorEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
if ptr == nil { if ptr == nil {
stream.WriteNil() stream.WriteNil()

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"reflect"
"unsafe" "unsafe"
"go_dreamfactory/lego/sys/codec/core" "go_dreamfactory/lego/sys/codec/core"
@ -20,7 +21,7 @@ func decoderOfArray(ctx *core.Ctx, typ reflect2.Type) core.IDecoder {
func encoderOfArray(ctx *core.Ctx, typ reflect2.Type) core.IEncoder { func encoderOfArray(ctx *core.Ctx, typ reflect2.Type) core.IEncoder {
arrayType := typ.(*reflect2.UnsafeArrayType) arrayType := typ.(*reflect2.UnsafeArrayType)
if arrayType.Len() == 0 { if arrayType.Len() == 0 {
return emptyArrayEncoder{} return &emptyArrayEncoder{}
} }
encoder := EncoderOfType(ctx.Append("[arrayElem]"), arrayType.Elem()) encoder := EncoderOfType(ctx.Append("[arrayElem]"), arrayType.Elem())
return &arrayEncoder{ctx.ICodec, arrayType, encoder} return &arrayEncoder{ctx.ICodec, arrayType, encoder}
@ -33,6 +34,9 @@ type arrayEncoder struct {
elemEncoder core.IEncoder elemEncoder core.IEncoder
} }
func (codec *arrayEncoder) GetType() reflect.Kind {
return reflect.Array
}
func (this *arrayEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *arrayEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteArrayStart() stream.WriteArrayStart()
elemPtr := unsafe.Pointer(ptr) elemPtr := unsafe.Pointer(ptr)
@ -75,6 +79,9 @@ type arrayDecoder struct {
elemDecoder core.IDecoder elemDecoder core.IDecoder
} }
func (codec *arrayDecoder) GetType() reflect.Kind {
return reflect.Array
}
func (this *arrayDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *arrayDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
arrayType := this.arrayType arrayType := this.arrayType
if extra.ReadNil() { if extra.ReadNil() {
@ -125,10 +132,14 @@ func (this *arrayDecoder) DecodeForSliceJson(ptr unsafe.Pointer, data []string)
type emptyArrayEncoder struct{} type emptyArrayEncoder struct{}
func (this emptyArrayEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *emptyArrayEncoder) GetType() reflect.Kind {
return reflect.Array
}
func (this *emptyArrayEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteEmptyArray() stream.WriteEmptyArray()
} }
func (this emptyArrayEncoder) IsEmpty(ptr unsafe.Pointer) bool { func (this *emptyArrayEncoder) IsEmpty(ptr unsafe.Pointer) bool {
return true return true
} }

View File

@ -14,6 +14,9 @@ type dynamicEncoder struct {
valType reflect2.Type valType reflect2.Type
} }
func (codec *dynamicEncoder) GetType() reflect.Kind {
return reflect.Interface
}
func (encoder *dynamicEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (encoder *dynamicEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
obj := encoder.valType.UnsafeIndirect(ptr) obj := encoder.valType.UnsafeIndirect(ptr)
stream.WriteVal(obj) stream.WriteVal(obj)
@ -26,6 +29,9 @@ func (encoder *dynamicEncoder) IsEmpty(ptr unsafe.Pointer) bool {
type efaceDecoder struct { type efaceDecoder struct {
} }
func (codec *efaceDecoder) GetType() reflect.Kind {
return reflect.Interface
}
func (decoder *efaceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (decoder *efaceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
pObj := (*interface{})(ptr) pObj := (*interface{})(ptr)
obj := *pObj obj := *pObj
@ -60,6 +66,9 @@ type ifaceDecoder struct {
valType *reflect2.UnsafeIFaceType valType *reflect2.UnsafeIFaceType
} }
func (codec *ifaceDecoder) GetType() reflect.Kind {
return reflect.Interface
}
func (decoder *ifaceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (decoder *ifaceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if extra.ReadNil() { if extra.ReadNil() {
decoder.valType.UnsafeSet(ptr, decoder.valType.UnsafeNew()) decoder.valType.UnsafeSet(ptr, decoder.valType.UnsafeNew())

View File

@ -84,6 +84,9 @@ type mapEncoder struct {
elemEncoder core.IEncoder elemEncoder core.IEncoder
} }
func (codec *mapEncoder) GetType() reflect.Kind {
return reflect.Map
}
func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *mapEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
if *(*unsafe.Pointer)(ptr) == nil { if *(*unsafe.Pointer)(ptr) == nil {
stream.WriteNil() stream.WriteNil()
@ -141,6 +144,9 @@ type mapDecoder struct {
elemDecoder core.IDecoder elemDecoder core.IDecoder
} }
func (codec *mapDecoder) GetType() reflect.Kind {
return reflect.Map
}
func (this *mapDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *mapDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
mapType := this.mapType mapType := this.mapType
if extra.ReadNil() { if extra.ReadNil() {
@ -184,20 +190,28 @@ func (this *mapDecoder) DecodeForMapJson(ptr unsafe.Pointer, extra map[string]st
keyext := this.codec.BorrowExtractor([]byte{}) keyext := this.codec.BorrowExtractor([]byte{})
elemext := this.codec.BorrowExtractor([]byte{}) elemext := this.codec.BorrowExtractor([]byte{})
for k, v := range extra { for k, v := range extra {
keyext.ResetBytes(StringToBytes(k))
key := this.keyType.UnsafeNew() key := this.keyType.UnsafeNew()
this.keyDecoder.Decode(key, keyext) if this.keyDecoder.GetType() != reflect.String {
if keyext.Error() != nil && keyext.Error() != io.EOF { keyext.ResetBytes(StringToBytes(k))
err = keyext.Error() this.keyDecoder.Decode(key, keyext)
return if keyext.Error() != nil && keyext.Error() != io.EOF {
err = keyext.Error()
return
}
} else {
*((*string)(key)) = k
} }
elemext.ResetBytes(StringToBytes(v))
elem := this.elemType.UnsafeNew() elem := this.elemType.UnsafeNew()
this.elemDecoder.Decode(elem, elemext) if this.elemDecoder.GetType() != reflect.String {
this.mapType.UnsafeSetIndex(ptr, key, elem) elemext.ResetBytes(StringToBytes(v))
if elemext.Error() != nil && elemext.Error() != io.EOF { this.elemDecoder.Decode(elem, elemext)
err = elemext.Error() this.mapType.UnsafeSetIndex(ptr, key, elem)
return if elemext.Error() != nil && elemext.Error() != io.EOF {
err = elemext.Error()
return
}
} else {
*((*string)(elem)) = v
} }
} }
return return
@ -208,6 +222,9 @@ type numericMapKeyDecoder struct {
decoder core.IDecoder decoder core.IDecoder
} }
func (this *numericMapKeyDecoder) GetType() reflect.Kind {
return this.decoder.GetType()
}
func (this *numericMapKeyDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *numericMapKeyDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if extra.ReadKeyStart() { if extra.ReadKeyStart() {
return return
@ -222,6 +239,9 @@ type numericMapKeyEncoder struct {
encoder core.IEncoder encoder core.IEncoder
} }
func (this *numericMapKeyEncoder) GetType() reflect.Kind {
return this.encoder.GetType()
}
func (this *numericMapKeyEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *numericMapKeyEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteKeyStart() stream.WriteKeyStart()
this.encoder.Encode(ptr, stream) this.encoder.Encode(ptr, stream)
@ -238,6 +258,10 @@ type dynamicMapKeyEncoder struct {
valType reflect2.Type valType reflect2.Type
} }
func (this *dynamicMapKeyEncoder) GetType() reflect.Kind {
return reflect.Interface
}
func (this *dynamicMapKeyEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *dynamicMapKeyEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
obj := this.valType.UnsafeIndirect(ptr) obj := this.valType.UnsafeIndirect(ptr)
encoderOfMapKey(this.ctx, reflect2.TypeOf(obj)).Encode(reflect2.PtrOf(obj), stream) encoderOfMapKey(this.ctx, reflect2.TypeOf(obj)).Encode(reflect2.PtrOf(obj), stream)

View File

@ -198,6 +198,9 @@ func createEncoderOfNative(ctx *core.Ctx, typ reflect2.Type) core.IEncoder {
type stringCodec struct { type stringCodec struct {
} }
func (codec *stringCodec) GetType() reflect.Kind {
return reflect.String
}
func (codec *stringCodec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *stringCodec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
*((*string)(ptr)) = extra.ReadString() *((*string)(ptr)) = extra.ReadString()
} }
@ -214,6 +217,10 @@ func (codec *stringCodec) IsEmpty(ptr unsafe.Pointer) bool {
type int8Codec struct { type int8Codec struct {
} }
func (codec *int8Codec) GetType() reflect.Kind {
return reflect.Int8
}
func (codec *int8Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *int8Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*int8)(ptr)) = extra.ReadInt8() *((*int8)(ptr)) = extra.ReadInt8()
@ -231,6 +238,9 @@ func (codec *int8Codec) IsEmpty(ptr unsafe.Pointer) bool {
type int16Codec struct { type int16Codec struct {
} }
func (codec *int16Codec) GetType() reflect.Kind {
return reflect.Int16
}
func (codec *int16Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *int16Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*int16)(ptr)) = extra.ReadInt16() *((*int16)(ptr)) = extra.ReadInt16()
@ -248,6 +258,9 @@ func (codec *int16Codec) IsEmpty(ptr unsafe.Pointer) bool {
type int32Codec struct { type int32Codec struct {
} }
func (codec *int32Codec) GetType() reflect.Kind {
return reflect.Int32
}
func (codec *int32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *int32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*int32)(ptr)) = extra.ReadInt32() *((*int32)(ptr)) = extra.ReadInt32()
@ -264,6 +277,9 @@ func (codec *int32Codec) IsEmpty(ptr unsafe.Pointer) bool {
type int64Codec struct { type int64Codec struct {
} }
func (codec *int64Codec) GetType() reflect.Kind {
return reflect.Int64
}
func (codec *int64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *int64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*int64)(ptr)) = extra.ReadInt64() *((*int64)(ptr)) = extra.ReadInt64()
@ -281,6 +297,9 @@ func (codec *int64Codec) IsEmpty(ptr unsafe.Pointer) bool {
type uint8Codec struct { type uint8Codec struct {
} }
func (codec *uint8Codec) GetType() reflect.Kind {
return reflect.Uint8
}
func (codec *uint8Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *uint8Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*uint8)(ptr)) = extra.ReadUint8() *((*uint8)(ptr)) = extra.ReadUint8()
@ -298,6 +317,9 @@ func (codec *uint8Codec) IsEmpty(ptr unsafe.Pointer) bool {
type uint16Codec struct { type uint16Codec struct {
} }
func (codec *uint16Codec) GetType() reflect.Kind {
return reflect.Uint16
}
func (codec *uint16Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *uint16Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*uint16)(ptr)) = extra.ReadUint16() *((*uint16)(ptr)) = extra.ReadUint16()
@ -315,6 +337,9 @@ func (codec *uint16Codec) IsEmpty(ptr unsafe.Pointer) bool {
type uint32Codec struct { type uint32Codec struct {
} }
func (codec *uint32Codec) GetType() reflect.Kind {
return reflect.Uint32
}
func (codec *uint32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *uint32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*uint32)(ptr)) = extra.ReadUint32() *((*uint32)(ptr)) = extra.ReadUint32()
@ -331,6 +356,9 @@ func (codec *uint32Codec) IsEmpty(ptr unsafe.Pointer) bool {
type uint64Codec struct { type uint64Codec struct {
} }
func (codec *uint64Codec) GetType() reflect.Kind {
return reflect.Uint64
}
func (codec *uint64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *uint64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*uint64)(ptr)) = extra.ReadUint64() *((*uint64)(ptr)) = extra.ReadUint64()
@ -348,6 +376,9 @@ func (codec *uint64Codec) IsEmpty(ptr unsafe.Pointer) bool {
type float32Codec struct { type float32Codec struct {
} }
func (codec *float32Codec) GetType() reflect.Kind {
return reflect.Float32
}
func (codec *float32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *float32Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*float32)(ptr)) = extra.ReadFloat32() *((*float32)(ptr)) = extra.ReadFloat32()
@ -365,6 +396,9 @@ func (codec *float32Codec) IsEmpty(ptr unsafe.Pointer) bool {
type float64Codec struct { type float64Codec struct {
} }
func (codec *float64Codec) GetType() reflect.Kind {
return reflect.Float64
}
func (codec *float64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *float64Codec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*float64)(ptr)) = extra.ReadFloat64() *((*float64)(ptr)) = extra.ReadFloat64()
@ -382,6 +416,9 @@ func (codec *float64Codec) IsEmpty(ptr unsafe.Pointer) bool {
type boolCodec struct { type boolCodec struct {
} }
func (codec *boolCodec) GetType() reflect.Kind {
return reflect.Bool
}
func (codec *boolCodec) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (codec *boolCodec) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadNil() { if !extra.ReadNil() {
*((*bool)(ptr)) = extra.ReadBool() *((*bool)(ptr)) = extra.ReadBool()

View File

@ -2,6 +2,7 @@ package factory
import ( import (
"fmt" "fmt"
"reflect"
"unsafe" "unsafe"
"go_dreamfactory/lego/sys/codec/core" "go_dreamfactory/lego/sys/codec/core"
@ -30,6 +31,9 @@ type OptionalDecoder struct {
ValueDecoder core.IDecoder ValueDecoder core.IDecoder
} }
func (this *OptionalDecoder) GetType() reflect.Kind {
return this.ValueDecoder.GetType()
}
func (this *OptionalDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *OptionalDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if extra.ReadNil() { if extra.ReadNil() {
*((*unsafe.Pointer)(ptr)) = nil *((*unsafe.Pointer)(ptr)) = nil
@ -48,6 +52,9 @@ type OptionalEncoder struct {
ValueEncoder core.IEncoder ValueEncoder core.IEncoder
} }
func (this *OptionalEncoder) GetType() reflect.Kind {
return this.ValueEncoder.GetType()
}
func (this *OptionalEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *OptionalEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
if *((*unsafe.Pointer)(ptr)) == nil { if *((*unsafe.Pointer)(ptr)) == nil {
stream.WriteNil() stream.WriteNil()
@ -96,6 +103,9 @@ type dereferenceDecoder struct {
valueDecoder core.IDecoder valueDecoder core.IDecoder
} }
func (this *dereferenceDecoder) GetType() reflect.Kind {
return this.valueDecoder.GetType()
}
func (this *dereferenceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *dereferenceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if *((*unsafe.Pointer)(ptr)) == nil { if *((*unsafe.Pointer)(ptr)) == nil {
newPtr := this.valueType.UnsafeNew() newPtr := this.valueType.UnsafeNew()
@ -110,6 +120,9 @@ type dereferenceEncoder struct {
ValueEncoder core.IEncoder ValueEncoder core.IEncoder
} }
func (this *dereferenceEncoder) GetType() reflect.Kind {
return this.ValueEncoder.GetType()
}
func (this *dereferenceEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *dereferenceEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
if *((*unsafe.Pointer)(ptr)) == nil { if *((*unsafe.Pointer)(ptr)) == nil {
stream.WriteNil() stream.WriteNil()

View File

@ -4,6 +4,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"reflect"
"unsafe" "unsafe"
"go_dreamfactory/lego/sys/codec/core" "go_dreamfactory/lego/sys/codec/core"
@ -28,6 +29,9 @@ type sliceEncoder struct {
elemEncoder core.IEncoder elemEncoder core.IEncoder
} }
func (codec *sliceEncoder) GetType() reflect.Kind {
return reflect.Slice
}
func (this *sliceEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *sliceEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
if this.sliceType.UnsafeIsNil(ptr) { if this.sliceType.UnsafeIsNil(ptr) {
stream.WriteNil() stream.WriteNil()
@ -86,6 +90,9 @@ type sliceDecoder struct {
elemDecoder core.IDecoder elemDecoder core.IDecoder
} }
func (codec *sliceDecoder) GetType() reflect.Kind {
return reflect.Slice
}
func (this *sliceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *sliceDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
sliceType := this.sliceType sliceType := this.sliceType
if extra.ReadNil() { if extra.ReadNil() {

View File

@ -250,6 +250,9 @@ type structEncoder struct {
fields []structFieldTo fields []structFieldTo
} }
func (codec *structEncoder) GetType() reflect.Kind {
return reflect.Struct
}
func (this *structEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (this *structEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteObjectStart() stream.WriteObjectStart()
isNotFirst := false isNotFirst := false
@ -305,6 +308,9 @@ type structDecoder struct {
disallowUnknownFields bool disallowUnknownFields bool
} }
func (codec *structDecoder) GetType() reflect.Kind {
return reflect.Struct
}
func (this *structDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (this *structDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
if !extra.ReadObjectStart() { if !extra.ReadObjectStart() {
return return
@ -384,6 +390,9 @@ type structFieldEncoder struct {
omitempty bool omitempty bool
} }
func (this *structFieldEncoder) GetType() reflect.Kind {
return this.fieldEncoder.GetType()
}
func (encoder *structFieldEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (encoder *structFieldEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
fieldPtr := encoder.field.UnsafeGet(ptr) fieldPtr := encoder.field.UnsafeGet(ptr)
encoder.fieldEncoder.Encode(fieldPtr, stream) encoder.fieldEncoder.Encode(fieldPtr, stream)
@ -411,6 +420,9 @@ type structFieldDecoder struct {
fieldDecoder core.IDecoder fieldDecoder core.IDecoder
} }
func (this *structFieldDecoder) GetType() reflect.Kind {
return this.fieldDecoder.GetType()
}
func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) { func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, extra core.IExtractor) {
fieldPtr := decoder.field.UnsafeGet(ptr) fieldPtr := decoder.field.UnsafeGet(ptr)
decoder.fieldDecoder.Decode(fieldPtr, extra) decoder.fieldDecoder.Decode(fieldPtr, extra)
@ -423,6 +435,9 @@ func (decoder *structFieldDecoder) Decode(ptr unsafe.Pointer, extra core.IExtrac
type emptyStructEncoder struct { type emptyStructEncoder struct {
} }
func (codec *emptyStructEncoder) GetType() reflect.Kind {
return reflect.Struct
}
func (encoder *emptyStructEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) { func (encoder *emptyStructEncoder) Encode(ptr unsafe.Pointer, stream core.IStream) {
stream.WriteEmptyObject() stream.WriteEmptyObject()
} }

View File

@ -2,10 +2,13 @@ package codec_test
import ( import (
"fmt" "fmt"
"reflect"
"testing" "testing"
"go_dreamfactory/lego/sys/codec" "go_dreamfactory/lego/sys/codec"
"go_dreamfactory/lego/sys/log" "go_dreamfactory/lego/sys/log"
"github.com/modern-go/reflect2"
) )
type TestData struct { type TestData struct {
@ -52,3 +55,31 @@ func Test_sys_mapjson(t *testing.T) {
fmt.Printf("codec UnmarshalJson data:%v err:%v", data, err) fmt.Printf("codec UnmarshalJson data:%v err:%v", data, err)
} }
} }
func Test_sys_reflect2(t *testing.T) {
data := []string{"123"}
ptr := reflect2.TypeOf(&data)
kind := ptr.Kind()
switch kind {
case reflect.Interface:
return
case reflect.Struct:
return
case reflect.Array:
return
case reflect.Slice:
return
case reflect.Map:
return
case reflect.Ptr:
ptrType := ptr.(*reflect2.UnsafePtrType)
elemType := ptrType.Elem()
kind = elemType.Kind()
if kind == reflect.Slice {
return
}
return
default:
return
}
}

View File

@ -11,6 +11,7 @@ import (
"go_dreamfactory/modules/equipment" "go_dreamfactory/modules/equipment"
"go_dreamfactory/modules/hero" "go_dreamfactory/modules/hero"
"go_dreamfactory/modules/items" "go_dreamfactory/modules/items"
"go_dreamfactory/modules/task"
"go_dreamfactory/modules/user" "go_dreamfactory/modules/user"
"go_dreamfactory/pb" "go_dreamfactory/pb"
"go_dreamfactory/services" "go_dreamfactory/services"
@ -74,6 +75,7 @@ func TestMain(m *testing.M) {
hero.NewModule(), hero.NewModule(),
user.NewModule(), user.NewModule(),
equipment.NewModule(), equipment.NewModule(),
task.NewModule(),
) )
}() }()
time.Sleep(time.Second * 3) time.Sleep(time.Second * 3)
@ -82,9 +84,9 @@ 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{})
s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{MainType: "pack", SubType: "getlist", Message: data}, &pb.RPCMessageReply{}) s_gateComp.ReceiveMsg(context.Background(), &pb.AgentMessage{UserId: "0_62c259916d8cf3e4e06311a8", MainType: "items", SubType: "getlist", Message: data}, &pb.RPCMessageReply{})
// items, err := module.db_comp.Pack_QueryUserPack("liwei1dao") // items, err := module.db_comp.Pack_QueryUserPack("liwei1dao")
// log.Debugf("item:%v err:%v", items, err) log.Debugf("data:%v", data)
} }
func Test_Modules_AddItems(t *testing.T) { func Test_Modules_AddItems(t *testing.T) {

View File

@ -122,7 +122,7 @@ type ItemsUseItemReq struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
GridId int32 `protobuf:"varint,1,opt,name=GridId,proto3" json:"GridId"` //格子Id GridId string `protobuf:"bytes,1,opt,name=GridId,proto3" json:"GridId"` //格子Id
ItemId int32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId"` //物品Id ItemId int32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId"` //物品Id
Amount uint32 `protobuf:"varint,3,opt,name=Amount,proto3" json:"Amount"` //使用数量 Amount uint32 `protobuf:"varint,3,opt,name=Amount,proto3" json:"Amount"` //使用数量
} }
@ -159,11 +159,11 @@ func (*ItemsUseItemReq) Descriptor() ([]byte, []int) {
return file_items_items_msg_proto_rawDescGZIP(), []int{2} return file_items_items_msg_proto_rawDescGZIP(), []int{2}
} }
func (x *ItemsUseItemReq) GetGridId() int32 { func (x *ItemsUseItemReq) GetGridId() string {
if x != nil { if x != nil {
return x.GridId return x.GridId
} }
return 0 return ""
} }
func (x *ItemsUseItemReq) GetItemId() int32 { func (x *ItemsUseItemReq) GetItemId() int32 {
@ -225,7 +225,7 @@ type ItemsSellItemReq struct {
sizeCache protoimpl.SizeCache sizeCache protoimpl.SizeCache
unknownFields protoimpl.UnknownFields unknownFields protoimpl.UnknownFields
GridId int32 `protobuf:"varint,1,opt,name=GridId,proto3" json:"GridId"` //格子Id GridId string `protobuf:"bytes,1,opt,name=GridId,proto3" json:"GridId"` //格子Id
ItemId int32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId"` //物品Id ItemId int32 `protobuf:"varint,2,opt,name=ItemId,proto3" json:"ItemId"` //物品Id
Amount uint32 `protobuf:"varint,3,opt,name=Amount,proto3" json:"Amount"` //使用数量 Amount uint32 `protobuf:"varint,3,opt,name=Amount,proto3" json:"Amount"` //使用数量
} }
@ -262,11 +262,11 @@ func (*ItemsSellItemReq) Descriptor() ([]byte, []int) {
return file_items_items_msg_proto_rawDescGZIP(), []int{4} return file_items_items_msg_proto_rawDescGZIP(), []int{4}
} }
func (x *ItemsSellItemReq) GetGridId() int32 { func (x *ItemsSellItemReq) GetGridId() string {
if x != nil { if x != nil {
return x.GridId return x.GridId
} }
return 0 return ""
} }
func (x *ItemsSellItemReq) GetItemId() int32 { func (x *ItemsSellItemReq) GetItemId() int32 {
@ -336,14 +336,14 @@ var file_items_items_msg_proto_rawDesc = []byte{
0x73, 0x65, 0x72, 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47, 0x72, 0x69, 0x73, 0x65, 0x72, 0x49, 0x74, 0x65, 0x6d, 0x44, 0x61, 0x74, 0x61, 0x52, 0x05, 0x47, 0x72, 0x69,
0x64, 0x73, 0x22, 0x59, 0x0a, 0x0f, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x55, 0x73, 0x65, 0x49, 0x74, 0x64, 0x73, 0x22, 0x59, 0x0a, 0x0f, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x55, 0x73, 0x65, 0x49, 0x74,
0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a,
0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49,
0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18,
0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x12, 0x0a, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x12, 0x0a,
0x10, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73, 0x10, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x55, 0x73, 0x65, 0x49, 0x74, 0x65, 0x6d, 0x52, 0x65, 0x73,
0x70, 0x22, 0x5a, 0x0a, 0x10, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74, 0x70, 0x22, 0x5a, 0x0a, 0x10, 0x49, 0x74, 0x65, 0x6d, 0x73, 0x53, 0x65, 0x6c, 0x6c, 0x49, 0x74,
0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18, 0x65, 0x6d, 0x52, 0x65, 0x71, 0x12, 0x16, 0x0a, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x18,
0x01, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x06, 0x47, 0x72, 0x69, 0x64, 0x49, 0x64, 0x12, 0x16, 0x0a,
0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49, 0x06, 0x49, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x05, 0x52, 0x06, 0x49,
0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x74, 0x65, 0x6d, 0x49, 0x64, 0x12, 0x16, 0x0a, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18,
0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x13, 0x0a, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x22, 0x13, 0x0a,

View File

@ -14,7 +14,7 @@ message ItemsGetlistResp {
//使 //使
message ItemsUseItemReq { message ItemsUseItemReq {
int32 GridId = 1; //Id string GridId = 1; //Id
int32 ItemId = 2; //Id int32 ItemId = 2; //Id
uint32 Amount = 3; //使 uint32 Amount = 3; //使
} }
@ -26,7 +26,7 @@ message ItemsUseItemResp {
//sailitem //sailitem
message ItemsSellItemReq { message ItemsSellItemReq {
int32 GridId = 1; //Id string GridId = 1; //Id
int32 ItemId = 2; //Id int32 ItemId = 2; //Id
uint32 Amount = 3; //使 uint32 Amount = 3; //使
} }