支持toStruct

This commit is contained in:
白茶清欢 2023-08-10 15:48:46 +08:00
parent e437ba87af
commit 033f6861d5
8 changed files with 123 additions and 28 deletions

View File

@ -36,4 +36,6 @@ type EasyMap interface {
GetAllForMapKeyString() map[string]interface{}
// Iterator 对数据的迭代
Iterator(IteratorFunc)
// ToStruct 转换成结构体
ToStruct(receiver interface{}) error
}

View File

@ -7,7 +7,10 @@
// Date : 2021-02-23 10:15 下午
package easymap
import "github.com/pkg/errors"
import (
"errors"
"fmt"
)
// keyNotFound key 不存在
//
@ -15,7 +18,7 @@ import "github.com/pkg/errors"
//
// Date : 10:17 下午 2021/2/23
func keyNotFound(key interface{}) error {
return errors.Errorf("%v 未找到", key)
return fmt.Errorf("%v 未找到", key)
}
// convertFail 数据类型妆换失败
@ -24,7 +27,10 @@ func keyNotFound(key interface{}) error {
//
// Date : 10:28 下午 2021/2/23
func convertFail(err error) error {
return errors.Wrapf(err, "数据类型转换失败")
if nil == err {
return nil
}
return fmt.Errorf("数据类型转换失败 : %v", err.Error())
}
// segmentError ...

19
go.mod
View File

@ -1,21 +1,20 @@
module git.zhangdeman.cn/zhangdeman/easymap
go 1.17
require (
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151
github.com/pkg/errors v0.9.1
)
go 1.20
require git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562
require (
github.com/BurntSushi/toml v1.3.2 // indirect
github.com/Jeffail/gabs v1.4.0 // indirect
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/dgrijalva/jwt-go v3.2.0+incompatible // indirect
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 // indirect
github.com/go-ini/ini v1.67.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/mitchellh/go-homedir v1.1.0 // indirect
github.com/mozillazg/go-pinyin v0.20.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/spaolacci/murmur3 v1.1.0 // indirect
github.com/tidwall/gjson v1.14.4 // indirect
github.com/tidwall/gjson v1.16.0 // indirect
github.com/tidwall/match v1.1.1 // indirect
github.com/tidwall/pretty v1.2.1 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect

30
go.sum
View File

@ -1,36 +1,36 @@
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151 h1:j537bRLQL1FlkdXTIaT9Ecjx5eogkPsGiTOWIEFQlc8=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230211164227-256094968151/go.mod h1:SyRTkOz6gxUVn3S/Qtkf+rhKV0I1ym8lwsT8YjggYFs=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562 h1:wo0r4mexqkPzQ1SZOw5z8A7FJ3ne1G6A/qWR3iaqlhw=
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810063945-842592611562/go.mod h1:trYFOShINaQBvinQrH4A0G2kfL22Y2lygEcAiGDt/sc=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
github.com/Jeffail/gabs v1.4.0 h1://5fYRRTq1edjfIrQGvdkcd22pkYUrHZ5YC/H2GJVAo=
github.com/Jeffail/gabs v1.4.0/go.mod h1:6xMvQMK4k33lb7GUUpaAPh6nKMmemQeg5d4gn7/bOXc=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394 h1:OYA+5W64v3OgClL+IrOD63t4i/RW7RqrAVl9LTZ9UqQ=
github.com/axgle/mahonia v0.0.0-20180208002826-3358181d7394/go.mod h1:Q8n74mJTIgjX4RBBcHnJ05h//6/k6foqmgE45jTQtxg=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1 h1:CaO/zOnF8VvUfEbhRatPcwKVWamvbYd8tQGRWacE9kU=
github.com/dgrijalva/jwt-go/v4 v4.0.0-preview1/go.mod h1:+hnT3ywWDTAFrW5aE+u2Sa/wT555ZqwoCS+pk3p6ry4=
github.com/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
github.com/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mozillazg/go-pinyin v0.20.0 h1:BtR3DsxpApHfKReaPO1fCqF4pThRwH9uwvXzm+GnMFQ=
github.com/mozillazg/go-pinyin v0.20.0/go.mod h1:iR4EnMMRXkfpFVV5FMi4FNB6wGq9NV6uDWbUuPhP4Yc=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/spaolacci/murmur3 v1.1.0 h1:7c1g84S4BPRrfL5Xrdp6fOJ206sU9y293DDHaoy0bLI=
github.com/spaolacci/murmur3 v1.1.0/go.mod h1:JwIasOWyU6f++ZhiEuf87xNszmSA2myDM2Kzu9HwQUA=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
github.com/tidwall/gjson v1.16.0 h1:SyXa+dsSPpUlcwEDuKuEBJEz5vzTvOea+9rjyYodQFg=
github.com/tidwall/gjson v1.16.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

View File

@ -8,6 +8,9 @@
package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync"
@ -305,3 +308,22 @@ func (n *normal) Iterator(handleFunc IteratorFunc) {
}
}
}
// ToStruct map转为结构体
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:33 2023/8/10
func (n *normal) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := n.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}

View File

@ -8,6 +8,9 @@
package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
@ -178,3 +181,22 @@ func (s *segment) Iterator(handleFunc IteratorFunc) {
}
}
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:42 2023/8/10
func (s *segment) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := s.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}

View File

@ -8,6 +8,9 @@
package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"sync"
@ -247,3 +250,22 @@ func (s *syncMap) Iterator(handleFunc IteratorFunc) {
return handleFunc(key, value)
})
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:47 2023/8/10
func (s *syncMap) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := s.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}

View File

@ -8,6 +8,9 @@
package easymap
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"git.zhangdeman.cn/zhangdeman/util"
@ -154,3 +157,22 @@ func (s *segmentSync) Iterator(handleFunc IteratorFunc) {
}
}
}
// ToStruct ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:48 2023/8/10
func (s *segmentSync) ToStruct(receiver interface{}) error {
if nil == receiver {
return errors.New("receiver is nil")
}
mapData := s.GetAll()
byteData, err := json.Marshal(mapData)
if nil != err {
return err
}
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
}