diff --git a/framework/database/marshaler.go b/framework/database/marshaler.go index 00fffe19..3bb8b323 100644 --- a/framework/database/marshaler.go +++ b/framework/database/marshaler.go @@ -1,8 +1,11 @@ package database import ( + "bytes" "context" "fmt" + "github.com/bytedance/sonic/decoder" + "github.com/pkg/errors" "reflect" "github.com/bytedance/sonic" @@ -29,23 +32,30 @@ func NewMarshaler(cache cache.CacheInterface[any]) *Marshaler { } } +type idecoder interface { + UseInt64() + Decode(val interface{}) (err error) +} + // Get obtains a value from cache and unmarshal value with given object func (c *Marshaler) Get(ctx context.Context, key any, returnObj any) (any, error) { key = c.shortenKey(key) result, err := c.cache.Get(ctx, key) if err != nil { - return nil, err + return nil, errors.WithStack(err) } + var dec idecoder switch v := result.(type) { case []byte: - err = json.Unmarshal(v, returnObj) + dec = decoder.NewStreamDecoder(bytes.NewBuffer(v)) case string: - err = json.Unmarshal([]byte(v), returnObj) + dec = decoder.NewDecoder(v) } - if err != nil { - return nil, err + dec.UseInt64() + if err = dec.Decode(returnObj); err != nil { + return nil, errors.WithStack(err) } return returnObj, nil diff --git a/go.mod b/go.mod index bb6de277..7fdb8449 100644 --- a/go.mod +++ b/go.mod @@ -46,7 +46,6 @@ require ( github.com/coocood/freecache v1.2.4 github.com/deckarep/golang-set/v2 v2.6.0 github.com/dgraph-io/ristretto v0.1.1 - github.com/duke-git/lancet/v2 v2.3.2 github.com/elliotchance/orderedmap/v2 v2.2.0 github.com/go-playground/assert/v2 v2.2.0 github.com/go-redis/redis/v8 v8.11.5 diff --git a/go.sum b/go.sum index 71f4e5f1..da2a07af 100644 --- a/go.sum +++ b/go.sum @@ -843,8 +843,6 @@ github.com/docopt/docopt-go v0.0.0-20180111231733-ee0de3bc6815/go.mod h1:WwZ+bS3 github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5 h1:iFaUwBSo5Svw6L7HYpRu/0lE3e0BaElwnNO1qkNQxBY= github.com/dsnet/compress v0.0.2-0.20210315054119-f66993602bf5/go.mod h1:qssHWj60/X5sZFNxpG4HBPDHVqxNm4DfnCKgrbZOT+s= github.com/dsnet/golib v0.0.0-20171103203638-1ea166775780/go.mod h1:Lj+Z9rebOhdfkVLjJ8T6VcRQv3SXugXy999NBtR9aFY= -github.com/duke-git/lancet/v2 v2.3.2 h1:Cv+uNkx5yGqDSvGc5Vu9eiiZobsPIf0Ng7NGy5hEdow= -github.com/duke-git/lancet/v2 v2.3.2/go.mod h1:zGa2R4xswg6EG9I6WnyubDbFO/+A/RROxIbXcwryTsc= github.com/dustin/go-humanize v1.0.0/go.mod h1:HtrtbFcZ19U5GC7JDqmcUSB87Iq5E25KnS6fMYU6eOk= github.com/dustin/go-humanize v1.0.1 h1:GzkhY7T5VNhEkwH0PVJgjz+fX1rhBrR7pRT3mDkpeCY= github.com/dustin/go-humanize v1.0.1/go.mod h1:Mu1zIs6XwVuF/gI1OepvI0qD18qycQx+mFykh5fBlto= diff --git a/toolkit/copier/copier.go b/toolkit/copier/copier.go index c469f3be..664f1dbb 100644 --- a/toolkit/copier/copier.go +++ b/toolkit/copier/copier.go @@ -2,6 +2,7 @@ package copier import ( "github.com/bytedance/sonic" + "github.com/bytedance/sonic/decoder" "github.com/pkg/errors" "reflect" ) @@ -13,12 +14,17 @@ func DeepCopy(src, target interface{}) error { if src == nil || target == nil { return nil } - b, err := json.Marshal(src) + if reflect.ValueOf(target).Kind() != reflect.Ptr { + return errors.New("Target should be a pointer") + } + b, err := json.MarshalToString(src) if err != nil { return errors.WithStack(err) } - if reflect.ValueOf(target).Kind() != reflect.Ptr { - return errors.New("Target should be a pointer") + dec := decoder.NewDecoder(b) + dec.UseInt64() + if err = dec.Decode(target); err != nil { + return errors.WithStack(err) } - return json.Unmarshal(b, target) + return nil }