上传编解码库代码

This commit is contained in:
liwei1dao 2022-07-11 17:10:49 +08:00
parent 906afe87d0
commit c79db39d2b
5 changed files with 51 additions and 51 deletions

View File

@ -4,9 +4,8 @@ import (
"sync"
"go_dreamfactory/lego/sys/codec/core"
"go_dreamfactory/lego/sys/codec/extractor"
"go_dreamfactory/lego/sys/codec/factory"
"go_dreamfactory/lego/sys/codec/stream"
"go_dreamfactory/lego/sys/codec/render"
"github.com/modern-go/reflect2"
)
@ -18,12 +17,12 @@ func newSys(options core.Options) (sys *codec, err error) {
encoderCache: new(sync.Map),
streamPool: &sync.Pool{
New: func() interface{} {
return stream.NewStream(sys, 512)
return render.NewStream(sys, 512)
},
},
extraPool: &sync.Pool{
New: func() interface{} {
return extractor.NewExtractor(sys)
return render.NewExtractor(sys)
},
},
}

View File

@ -0,0 +1,45 @@
package render
import "go_dreamfactory/lego/sys/codec/core"
const maxDepth = 10000
var hexDigits []byte
var valueTypes []core.ValueType
func init() {
hexDigits = make([]byte, 256)
for i := 0; i < len(hexDigits); i++ {
hexDigits[i] = 255
}
for i := '0'; i <= '9'; i++ {
hexDigits[i] = byte(i - '0')
}
for i := 'a'; i <= 'f'; i++ {
hexDigits[i] = byte((i - 'a') + 10)
}
for i := 'A'; i <= 'F'; i++ {
hexDigits[i] = byte((i - 'A') + 10)
}
valueTypes = make([]core.ValueType, 256)
for i := 0; i < len(valueTypes); i++ {
valueTypes[i] = core.InvalidValue
}
valueTypes['"'] = core.StringValue
valueTypes['-'] = core.NumberValue
valueTypes['0'] = core.NumberValue
valueTypes['1'] = core.NumberValue
valueTypes['2'] = core.NumberValue
valueTypes['3'] = core.NumberValue
valueTypes['4'] = core.NumberValue
valueTypes['5'] = core.NumberValue
valueTypes['6'] = core.NumberValue
valueTypes['7'] = core.NumberValue
valueTypes['8'] = core.NumberValue
valueTypes['9'] = core.NumberValue
valueTypes['t'] = core.BoolValue
valueTypes['f'] = core.BoolValue
valueTypes['n'] = core.NilValue
valueTypes['['] = core.ArrayValue
valueTypes['{'] = core.ObjectValue
}

View File

@ -1,4 +1,4 @@
package extractor
package render
import (
"fmt"
@ -12,47 +12,6 @@ import (
"github.com/modern-go/reflect2"
)
const maxDepth = 10000
var hexDigits []byte
var valueTypes []core.ValueType
func init() {
hexDigits = make([]byte, 256)
for i := 0; i < len(hexDigits); i++ {
hexDigits[i] = 255
}
for i := '0'; i <= '9'; i++ {
hexDigits[i] = byte(i - '0')
}
for i := 'a'; i <= 'f'; i++ {
hexDigits[i] = byte((i - 'a') + 10)
}
for i := 'A'; i <= 'F'; i++ {
hexDigits[i] = byte((i - 'A') + 10)
}
valueTypes = make([]core.ValueType, 256)
for i := 0; i < len(valueTypes); i++ {
valueTypes[i] = core.InvalidValue
}
valueTypes['"'] = core.StringValue
valueTypes['-'] = core.NumberValue
valueTypes['0'] = core.NumberValue
valueTypes['1'] = core.NumberValue
valueTypes['2'] = core.NumberValue
valueTypes['3'] = core.NumberValue
valueTypes['4'] = core.NumberValue
valueTypes['5'] = core.NumberValue
valueTypes['6'] = core.NumberValue
valueTypes['7'] = core.NumberValue
valueTypes['8'] = core.NumberValue
valueTypes['9'] = core.NumberValue
valueTypes['t'] = core.BoolValue
valueTypes['f'] = core.BoolValue
valueTypes['n'] = core.NilValue
valueTypes['['] = core.ArrayValue
valueTypes['{'] = core.ObjectValue
}
func NewExtractor(codec core.ICodec) *JsonExtractor {
return &JsonExtractor{
codec: codec,
@ -145,7 +104,6 @@ func (this *JsonExtractor) ReadNil() (ret bool) {
this.unreadByte()
return false
}
func (this *JsonExtractor) ReadArrayStart() (ret bool) {
c := this.nextToken()
if c == '[' {
@ -226,7 +184,6 @@ func (this *JsonExtractor) ReadKeyEnd() (ret bool) {
this.reportError("ReadKeyEnd", `expect " but found `+string([]byte{c}))
return
}
func (this *JsonExtractor) Skip() {
c := this.nextToken()
switch c {

View File

@ -1,4 +1,4 @@
package stream
package render
import (
"go_dreamfactory/lego/sys/codec/core"
@ -143,7 +143,6 @@ func (this *JsonStream) WriteString(val string) {
}
utils.WriteStringSlowPath(&this.buf, i, val, valLen)
}
func (this *JsonStream) WriteBytes(val []byte) {
this.buf = append(this.buf, val...)
}

View File

@ -27,7 +27,7 @@ func Test_sys(t *testing.T) {
if sys, err := codec.NewSys(); err != nil {
fmt.Printf("gin init err:%v", err)
} else {
d, err := sys.MarshalJson(&TestData{Name: "liwe1idao", Value: 10, Array: []interface{}{1, "dajiahao", &Test1Data{Name: "liwe1dao", Value: 123}}, Data: map[string]interface{}{"hah": 1, "asd": 999}})
d, err := sys.MarshalJson(&TestData{Name: "http://liwei1dao.com?asd=1&dd=1", Value: 10, Array: []interface{}{1, "dajiahao", &Test1Data{Name: "liwe1dao", Value: 123}}, Data: map[string]interface{}{"hah": 1, "asd": 999}})
fmt.Printf("codec Marshal d:%s err:%v", d, err)
data := &TestData{}
err = sys.UnmarshalJson(d, data)