Compare commits
15 Commits
2e0a298aa5
...
master
Author | SHA1 | Date | |
---|---|---|---|
8d68e6106e | |||
0c3d7b7e92 | |||
135850ee8a | |||
774d4bdc07 | |||
f617c9cda4 | |||
8a9502c122 | |||
73cf1be49c | |||
de2e49144f | |||
cb92be844e | |||
2d10d261da | |||
42ae8fe5eb | |||
2e4508468f | |||
ecb0211345 | |||
8b8d3c3384 | |||
bebbd57b58 |
@ -15,6 +15,8 @@ import "io"
|
|||||||
//
|
//
|
||||||
// Date : 16:55 2024/10/23
|
// Date : 16:55 2024/10/23
|
||||||
type Serializable interface {
|
type Serializable interface {
|
||||||
|
// Unmarshal 反序列化
|
||||||
|
Unmarshal(byteData []byte, receiver any) error
|
||||||
// UnmarshalWithNumber 反序列化,同时解析数字
|
// UnmarshalWithNumber 反序列化,同时解析数字
|
||||||
UnmarshalWithNumber(byteData []byte, receiver any) error
|
UnmarshalWithNumber(byteData []byte, receiver any) error
|
||||||
// UnmarshalWithNumberIgnoreError 反序列化,同时解析数字, 忽略结果的成功与失败
|
// UnmarshalWithNumberIgnoreError 反序列化,同时解析数字, 忽略结果的成功与失败
|
||||||
|
43
file.go
43
file.go
@ -13,6 +13,7 @@ import (
|
|||||||
"git.zhangdeman.cn/zhangdeman/consts"
|
"git.zhangdeman.cn/zhangdeman/consts"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"net/url"
|
||||||
"os"
|
"os"
|
||||||
"path"
|
"path"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
@ -153,14 +154,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error {
|
|||||||
var (
|
var (
|
||||||
parseFunc func(filePath string, receiver any) error
|
parseFunc func(filePath string, receiver any) error
|
||||||
)
|
)
|
||||||
filePathArr := strings.Split(filePath, "#@#")
|
|
||||||
fileExt := ""
|
fileExt := consts.FileType(strings.TrimLeft(strings.ToLower(path.Ext(filePath)), "."))
|
||||||
filePath = filePathArr[0]
|
|
||||||
if len(filePathArr) == 2 && len(filePathArr[1]) > 0 {
|
|
||||||
fileExt = strings.ToLower(filePathArr[1])
|
|
||||||
} else {
|
|
||||||
fileExt = strings.ToLower(path.Ext(filePath))
|
|
||||||
}
|
|
||||||
switch fileExt {
|
switch fileExt {
|
||||||
case consts.FileTypeJson:
|
case consts.FileTypeJson:
|
||||||
parseFunc = f.ReadJSONContent
|
parseFunc = f.ReadJSONContent
|
||||||
@ -173,7 +168,7 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error {
|
|||||||
case consts.FileTypeXml:
|
case consts.FileTypeXml:
|
||||||
parseFunc = f.ReadXmlContent
|
parseFunc = f.ReadXmlContent
|
||||||
default:
|
default:
|
||||||
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
|
return errors.New(fileExt.String() + " 暂不支持当前格式的文件解析")
|
||||||
}
|
}
|
||||||
return parseFunc(filePath, receiver)
|
return parseFunc(filePath, receiver)
|
||||||
}
|
}
|
||||||
@ -310,3 +305,33 @@ func (f *file) ReadFileListRecurve(rootDir string) ([]*define.FileInfo, error) {
|
|||||||
func (f *file) FileExt(filePath string) string {
|
func (f *file) FileExt(filePath string) string {
|
||||||
return strings.TrimLeft(filepath.Ext(filePath), ".")
|
return strings.TrimLeft(filepath.Ext(filePath), ".")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ReadFromRemote 从远端URL读取文件内容
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 16:09 2024/12/23
|
||||||
|
func (f *file) ReadFromRemote(remoteUrl string) ([]byte, error) {
|
||||||
|
if len(remoteUrl) == 0 {
|
||||||
|
return nil, errors.New("remoteUrl is empty")
|
||||||
|
}
|
||||||
|
var (
|
||||||
|
err error
|
||||||
|
resp *http.Response
|
||||||
|
responseData []byte
|
||||||
|
)
|
||||||
|
|
||||||
|
if _, err = url.Parse(remoteUrl); nil != err {
|
||||||
|
return nil, errors.New("remoteUrl is invalid : " + err.Error())
|
||||||
|
}
|
||||||
|
if resp, err = http.Get(remoteUrl); nil != err {
|
||||||
|
return nil, errors.New("request fail : " + err.Error())
|
||||||
|
}
|
||||||
|
if resp.StatusCode != http.StatusOK {
|
||||||
|
return nil, errors.New("http status fail : " + resp.Status)
|
||||||
|
}
|
||||||
|
if responseData, err = io.ReadAll(resp.Body); nil != err {
|
||||||
|
return nil, errors.New("read response fail : " + err.Error())
|
||||||
|
}
|
||||||
|
return responseData, nil
|
||||||
|
}
|
||||||
|
22
file_test.go
Normal file
22
file_test.go
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
// Package serialize ...
|
||||||
|
//
|
||||||
|
// Description : serialize ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2024-11-15 15:36
|
||||||
|
package serialize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func Test_file_ReadDirFileList(t *testing.T) {
|
||||||
|
f := &file{}
|
||||||
|
fileList, err := f.ReadDirFileList("/Users/zhangdeman/project/go-project/wrapper", true, true)
|
||||||
|
if nil != err {
|
||||||
|
panic(err.Error())
|
||||||
|
}
|
||||||
|
fmt.Println(JSON.MarshalForString(fileList))
|
||||||
|
}
|
10
go.mod
10
go.mod
@ -1,14 +1,16 @@
|
|||||||
module git.zhangdeman.cn/zhangdeman/serialize
|
module git.zhangdeman.cn/zhangdeman/serialize
|
||||||
|
|
||||||
go 1.21
|
go 1.23.0
|
||||||
|
|
||||||
toolchain go1.21.4
|
toolchain go1.24.2
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e
|
||||||
github.com/BurntSushi/toml v1.4.0
|
github.com/BurntSushi/toml v1.5.0
|
||||||
github.com/go-ini/ini v1.67.0
|
github.com/go-ini/ini v1.67.0
|
||||||
|
github.com/sbabiv/xml2map v1.2.1
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
)
|
)
|
||||||
|
|
||||||
require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059
|
require github.com/stretchr/testify v1.9.0 // indirect
|
||||||
|
43
go.sum
43
go.sum
@ -1,34 +1,27 @@
|
|||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c h1:Dan3iSVU6XTKt8r3/qixfPHPpfLZjkYlPmaJios7wtE=
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995 h1:LmPRAf0AsxRVFPibdpZR89ajlsz8hof2IvMMyTqiEq4=
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b h1:C7KftnLh7dOqzNRs5dn/9yqMDvuqMn5RCglvV6bY758=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230815040024-2b12dd51d19b/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240104123641-b3f23974e5d6 h1:ytpXTP3oxp480BAZQoOzqlBP4XP73NcpMplZ1/fA1lQ=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240104123641-b3f23974e5d6/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6ZdtkxI4HeSIHun0QRrIzjLnUht0bKdyO2t6n4=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763 h1:aG59AjZAn/Y+smWoa6l6HQB+yh/Rpml79EvmfjEb6Ks=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 h1:TPAYdTKKUjgxtCnK38d1Tb4teyQp1C7wYHPdR32yZtM=
|
|
||||||
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810093304-ccb4045065c4 h1:MgZ2f1BkUtfP8D6uDX9S8o+V4wYUw76HlAw3xCH9epA=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230810093304-ccb4045065c4/go.mod h1:tPl6isAsRQLZrX8G3/8fCdhN4OcZypdmdHR/PDkd2PY=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444 h1:JVp575weLUX4sfhgjjxotJPxfHio7Ua8KHH3LMRRs2E=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20230815042559-b34984be7444/go.mod h1:IqS3vAMyt1fVCWS7RqGeUw1EFnL/ruUMha45G2T+YNM=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231014142840-445c6407db92 h1:p1GVRYJc3NNoZeLs4CukitAbM3O/ALNq3l31cnbBQDM=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231014142840-445c6407db92/go.mod h1:6OBeuwKy2J1TjdAwStEyC6aYC3kStmJiCg1eFC7g0fk=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253 h1:GO3oZa5a2sqwAzGcLDJtQzmshSWRmoP7IDS8bwFqvC4=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20231227095334-7eb5cdbf9253/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e h1:Q973S6CcWr1ICZhFI1STFOJ+KUImCl2BaIXm6YppBqI=
|
||||||
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
git.zhangdeman.cn/zhangdeman/util v0.0.0-20240618042405-6ee2c904644e/go.mod h1:VpPjBlwz8U+OxZuxzHQBv1aEEZ3pStH6bZvT21ADEbI=
|
||||||
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
|
github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
|
||||||
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
|
github.com/BurntSushi/toml v1.5.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
||||||
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
|
|
||||||
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
|
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
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/go-ini/ini v1.67.0 h1:z6ZrTEZqSWOTyH2FlglNbNgARyHG8oLW9gMELqKr06A=
|
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/go-ini/ini v1.67.0/go.mod h1:ByCAeIL28uOIIG0E3PJtZPDL8WnHpFKFOtgjp+3Ies8=
|
||||||
|
github.com/gopherjs/gopherjs v1.17.2 h1:fQnZVsXk8uxXIStYb0N4bGk7jeyTalG/wsZjQ25dO0g=
|
||||||
|
github.com/gopherjs/gopherjs v1.17.2/go.mod h1:pRRIvn/QzFLrKfvEz3qUuEhtE/zLCWfreZ6J5gM2i+k=
|
||||||
|
github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7C0MuV77Wo=
|
||||||
|
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU=
|
||||||
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||||
github.com/stretchr/testify v1.8.2 h1:+h33VjcLVPDHtOdpUCuF+7gSuG3yGIftsP1YvFihtJ8=
|
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
|
||||||
|
github.com/sbabiv/xml2map v1.2.1 h1:1lT7t0hhUvXZCkdxqtq4n8/ZCnwLWGq4rDuDv5XOoFE=
|
||||||
|
github.com/sbabiv/xml2map v1.2.1/go.mod h1:2TPoAfcaM7+Sd4iriPvzyntb2mx7GY+kkQpB/GQa/eo=
|
||||||
|
github.com/smarty/assertions v1.15.0 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
|
||||||
|
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec=
|
||||||
|
github.com/smartystreets/goconvey v1.8.1 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=
|
||||||
|
github.com/smartystreets/goconvey v1.8.1/go.mod h1:+/u4qLyY6x1jReYOp7GOM2FSt8aP9CzCZL03bI28W60=
|
||||||
|
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
|
||||||
|
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
|
||||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
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/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
|
71
ini.go
Normal file
71
ini.go
Normal file
@ -0,0 +1,71 @@
|
|||||||
|
// Package serialize ...
|
||||||
|
//
|
||||||
|
// Description : serialize ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-04-28 11:32
|
||||||
|
package serialize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"github.com/go-ini/ini"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Ini = &ownIni{}
|
||||||
|
)
|
||||||
|
|
||||||
|
type ownIni struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) Unmarshal(byteData []byte, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
||||||
|
if nil == receiver {
|
||||||
|
return errors.New("receiver is nil")
|
||||||
|
}
|
||||||
|
return ini.MapTo(receiver, byteData)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
|
||||||
|
_ = o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
|
||||||
|
if nil == receiver {
|
||||||
|
return errors.New("receiver is nil")
|
||||||
|
}
|
||||||
|
return ini.MapTo(receiver, ioReader)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
|
||||||
|
_ = o.UnmarshalWithNumberForIOReader(ioReader, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumberForString(input string, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber([]byte(input), receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
|
||||||
|
_ = o.UnmarshalWithNumberForString(input, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) MarshalForByte(input any) ([]byte, error) {
|
||||||
|
return JSON.MarshalForByte(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) MarshalForByteIgnoreError(input any) []byte {
|
||||||
|
return JSON.MarshalForByteIgnoreError(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) MarshalForString(input any) (string, error) {
|
||||||
|
return JSON.MarshalForString(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownIni) MarshalForStringIgnoreError(input any) string {
|
||||||
|
return JSON.MarshalForStringIgnoreError(input)
|
||||||
|
}
|
12
json.go
12
json.go
@ -33,6 +33,10 @@ func init() {
|
|||||||
type ownJSON struct {
|
type ownJSON struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (oj *ownJSON) Unmarshal(byteData []byte, receiver any) error {
|
||||||
|
return oj.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
// UnmarshalWithNumber 解析json
|
// UnmarshalWithNumber 解析json
|
||||||
//
|
//
|
||||||
// Author : go_developer@163.com<白茶清欢>
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
@ -181,8 +185,14 @@ func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...any) (map[strin
|
|||||||
}
|
}
|
||||||
|
|
||||||
for k, v := range itemRes {
|
for k, v := range itemRes {
|
||||||
|
if v == nil {
|
||||||
|
if _, exist := res[k]; !exist {
|
||||||
res[k] = v
|
res[k] = v
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
res[k] = v
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return res, nil
|
return res, nil
|
||||||
}
|
}
|
||||||
@ -203,7 +213,7 @@ func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...any) map[string]any {
|
|||||||
//
|
//
|
||||||
// Date : 17:07 2024/1/10
|
// Date : 17:07 2024/1/10
|
||||||
func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error {
|
func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error {
|
||||||
res, err := oj.MergeDataForMap(false, dataList)
|
res, err := oj.MergeDataForMap(false, dataList...)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
97
toml.go
Normal file
97
toml.go
Normal file
@ -0,0 +1,97 @@
|
|||||||
|
// Package serialize ...
|
||||||
|
//
|
||||||
|
// Description : serialize ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-04-28 10:57
|
||||||
|
package serialize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"github.com/BurntSushi/toml"
|
||||||
|
"io"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Toml = &ownToml{}
|
||||||
|
)
|
||||||
|
|
||||||
|
type ownToml struct {
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) Unmarshal(byteData []byte, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) Parse(byteData []byte, receiver any) (*toml.MetaData, error) {
|
||||||
|
if nil == receiver {
|
||||||
|
return nil, errors.New("receiver is nil")
|
||||||
|
}
|
||||||
|
decoder := toml.NewDecoder(bytes.NewReader(byteData))
|
||||||
|
if res, err := decoder.Decode(receiver); nil != err {
|
||||||
|
return nil, err
|
||||||
|
} else {
|
||||||
|
return &res, nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
||||||
|
if _, err := o.Parse(byteData, receiver); nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
|
||||||
|
_ = o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
|
||||||
|
decoder := toml.NewDecoder(ioReader)
|
||||||
|
if _, err := decoder.Decode(receiver); nil != err {
|
||||||
|
return err
|
||||||
|
} else {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
|
||||||
|
_ = o.UnmarshalWithNumberForIOReader(ioReader, receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumberForString(input string, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber([]byte(input), receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
|
||||||
|
o.UnmarshalWithNumberIgnoreError([]byte(input), receiver)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) MarshalForByte(input any) ([]byte, error) {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
encoder := toml.NewEncoder(b)
|
||||||
|
if err := encoder.Encode(input); nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return b.Bytes(), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) MarshalForByteIgnoreError(input any) []byte {
|
||||||
|
res, _ := o.MarshalForByte(input)
|
||||||
|
return res
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) MarshalForString(input any) (string, error) {
|
||||||
|
res, err := o.MarshalForByte(input)
|
||||||
|
if nil != err {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(res), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *ownToml) MarshalForStringIgnoreError(input any) string {
|
||||||
|
res, _ := o.MarshalForString(input)
|
||||||
|
return res
|
||||||
|
}
|
76
wrapper.go
Normal file
76
wrapper.go
Normal file
@ -0,0 +1,76 @@
|
|||||||
|
// Package serialize ...
|
||||||
|
//
|
||||||
|
// Description : serialize ...
|
||||||
|
//
|
||||||
|
// Author : go_developer@163.com<白茶清欢>
|
||||||
|
//
|
||||||
|
// Date : 2025-04-28 11:58
|
||||||
|
package serialize
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
"git.zhangdeman.cn/zhangdeman/serialize/abstract"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
Wrapper = &wrapper{
|
||||||
|
serializableTable: map[string]abstract.Serializable{
|
||||||
|
"xml": Xml,
|
||||||
|
"yml": Yml,
|
||||||
|
"yaml": Yml,
|
||||||
|
"toml": Toml,
|
||||||
|
"ini": Ini,
|
||||||
|
"json": JSON,
|
||||||
|
},
|
||||||
|
l: &sync.RWMutex{},
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
type wrapper struct {
|
||||||
|
serializableTable map[string]abstract.Serializable
|
||||||
|
l *sync.RWMutex
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetSerializable ...
|
||||||
|
func (w *wrapper) GetSerializable(t string) (abstract.Serializable, error) {
|
||||||
|
w.l.RLock()
|
||||||
|
if _, exist := w.serializableTable[t]; !exist {
|
||||||
|
w.l.RUnlock()
|
||||||
|
return nil, errors.New(t + ": is not support")
|
||||||
|
}
|
||||||
|
serializable := w.serializableTable[t]
|
||||||
|
w.l.RUnlock()
|
||||||
|
if nil == serializable {
|
||||||
|
return nil, errors.New("serializable is nil")
|
||||||
|
}
|
||||||
|
return serializable, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// SetSerializable 设置序列化实现, 已存在会覆盖
|
||||||
|
func (w *wrapper) SetSerializable(t string, serializable abstract.Serializable) {
|
||||||
|
if nil == serializable {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
w.l.Lock()
|
||||||
|
defer w.l.Unlock()
|
||||||
|
w.serializableTable[t] = serializable
|
||||||
|
}
|
||||||
|
|
||||||
|
// Marshal 序列化
|
||||||
|
func (w *wrapper) Marshal(marshalType string, input any) ([]byte, error) {
|
||||||
|
serializable, err := w.GetSerializable(marshalType)
|
||||||
|
if nil != err {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
return serializable.MarshalForByte(input)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Unmarshal 反序列化
|
||||||
|
func (w *wrapper) Unmarshal(unmarshalType string, input []byte, receiver any) error {
|
||||||
|
serializable, err := w.GetSerializable(unmarshalType)
|
||||||
|
if nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return serializable.UnmarshalWithNumber(input, receiver)
|
||||||
|
}
|
54
xml.go
54
xml.go
@ -10,6 +10,7 @@ package serialize
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
|
"github.com/sbabiv/xml2map"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -19,8 +20,15 @@ var (
|
|||||||
|
|
||||||
type ownXml struct{}
|
type ownXml struct{}
|
||||||
|
|
||||||
|
func (o *ownXml) Unmarshal(byteData []byte, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
||||||
return xml.NewDecoder(bytes.NewReader(byteData)).Decode(receiver)
|
res, err := xml2map.NewDecoder(bytes.NewReader(byteData)).Decode()
|
||||||
|
if nil != err {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return JSON.Transition(res, receiver)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (o *ownXml) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
|
func (o *ownXml) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
|
||||||
@ -67,3 +75,47 @@ func (o *ownXml) MarshalForStringIgnoreError(input any) string {
|
|||||||
str, _ := o.MarshalForString(input)
|
str, _ := o.MarshalForString(input)
|
||||||
return str
|
return str
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type XmlStringMap map[string]string
|
||||||
|
|
||||||
|
type xmlMapEntry struct {
|
||||||
|
XMLName xml.Name
|
||||||
|
Value string `xml:",chardata"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *XmlStringMap) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
|
||||||
|
if nil == m {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
if len(*m) == 0 {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
err := e.EncodeToken(start)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range *m {
|
||||||
|
_ = e.Encode(xmlMapEntry{XMLName: xml.Name{Local: k}, Value: v})
|
||||||
|
}
|
||||||
|
|
||||||
|
return e.EncodeToken(start.End())
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m *XmlStringMap) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
|
||||||
|
*m = XmlStringMap{}
|
||||||
|
for {
|
||||||
|
var e xmlMapEntry
|
||||||
|
|
||||||
|
err := d.Decode(&e)
|
||||||
|
if err == io.EOF {
|
||||||
|
break
|
||||||
|
} else if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
(*m)[e.XMLName.Local] = e.Value
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
5
yml.go
5
yml.go
@ -14,7 +14,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
Yml = ownYml{}
|
Yml = &ownYml{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type ownYml struct{}
|
type ownYml struct{}
|
||||||
@ -40,6 +40,9 @@ func (o *ownYml) MarshalForStringIgnoreError(input any) string {
|
|||||||
return string(o.MarshalForByteIgnoreError(input))
|
return string(o.MarshalForByteIgnoreError(input))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (o *ownYml) Unmarshal(byteData []byte, receiver any) error {
|
||||||
|
return o.UnmarshalWithNumber(byteData, receiver)
|
||||||
|
}
|
||||||
func (o *ownYml) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
func (o *ownYml) UnmarshalWithNumber(byteData []byte, receiver any) error {
|
||||||
return yaml.NewDecoder(bytes.NewReader(byteData)).Decode(receiver)
|
return yaml.NewDecoder(bytes.NewReader(byteData)).Decode(receiver)
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user