Compare commits

21 Commits

Author SHA1 Message Date
8d68e6106e 升级接口约束 2025-05-04 13:59:08 +08:00
0c3d7b7e92 升级xml解析 2025-05-03 19:45:48 +08:00
135850ee8a Merge pull request '新增序列化能力' (#2) from feature/support_toml_ini into master
Reviewed-on: #2
2025-04-28 12:11:57 +08:00
774d4bdc07 wrapper高级封装序列化能力 2025-04-28 12:10:59 +08:00
f617c9cda4 增加ini序列化能力 2025-04-28 11:52:29 +08:00
8a9502c122 增加toml内容序列化能力 2025-04-28 11:23:27 +08:00
73cf1be49c update go mod 2025-04-26 21:22:59 +08:00
de2e49144f 支持从远程URL读取文件内容 2024-12-23 16:49:48 +08:00
cb92be844e 优化json合并 2024-11-25 18:54:03 +08:00
2d10d261da 升级FileType枚举类型定义 2024-11-25 16:18:53 +08:00
42ae8fe5eb 修复MergeDataForReceiver方法BUG 2024-11-08 16:20:10 +08:00
2e4508468f 修复文件格式解析的BUG 2024-11-07 18:16:41 +08:00
ecb0211345 AnyMap -> XmlStringMap 2024-11-04 17:23:08 +08:00
8b8d3c3384 update xml serialize 2024-11-04 17:13:43 +08:00
bebbd57b58 update xml serialize 2024-11-04 17:13:12 +08:00
2e0a298aa5 Merge pull request '序列化约束升级' (#1) from feature/xml into master
Reviewed-on: #1
2024-10-23 18:42:58 +08:00
b9826a24a0 支持xml配置文件解析 2024-10-23 18:41:41 +08:00
bfa244f97d 支持序列化 xml 文件 2024-10-23 17:55:41 +08:00
77f0930081 fix 2024-10-23 17:27:26 +08:00
82ce67170a 增加yaml文件的序列化与反序列化 2024-10-23 17:25:10 +08:00
454b1e8ac6 序列化增加接口约束 2024-10-23 17:04:10 +08:00
11 changed files with 643 additions and 73 deletions

38
abstract/serialize.go Normal file
View File

@ -0,0 +1,38 @@
// Package abstract ...
//
// Description : abstract ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-23 16:54
package abstract
import "io"
// Serializable 序列化的接口约束
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:55 2024/10/23
type Serializable interface {
// Unmarshal 反序列化
Unmarshal(byteData []byte, receiver any) error
// UnmarshalWithNumber 反序列化,同时解析数字
UnmarshalWithNumber(byteData []byte, receiver any) error
// UnmarshalWithNumberIgnoreError 反序列化,同时解析数字, 忽略结果的成功与失败
UnmarshalWithNumberIgnoreError(byteData []byte, receiver any)
// UnmarshalWithNumberForIOReader 从文件流读取数据并反序列化
UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error
// UnmarshalWithNumberForIOReaderIgnoreError 从文件流读取数据并反序列化, 忽略异常
UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any)
// UnmarshalWithNumberForString 序列化字符串反解析为结构体
UnmarshalWithNumberForString(input string, receiver any) error
// UnmarshalWithNumberForStringIgnoreError 序列化字符串反解析为结构体,并忽略异常
UnmarshalWithNumberForStringIgnoreError(input string, receiver any)
// MarshalForByte 序列化为字节数组
MarshalForByte(input any) ([]byte, error)
MarshalForByteIgnoreError(input any) []byte
// MarshalForString 序列化为字符串
MarshalForString(input any) (string, error)
MarshalForStringIgnoreError(input any) string
}

86
file.go
View File

@ -8,10 +8,12 @@
package serialize
import (
"encoding/xml"
"errors"
"git.zhangdeman.cn/zhangdeman/consts"
"io"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
@ -22,8 +24,6 @@ import (
"git.zhangdeman.cn/zhangdeman/util/define"
"github.com/go-ini/ini"
yml "gopkg.in/yaml.v3"
)
var (
@ -67,7 +67,7 @@ func (f *file) GetProjectPath() (string, error) {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 10:35 下午 2021/4/26
func (f *file) ReadYmlContent(filePath string, result interface{}) error {
func (f *file) ReadYmlContent(filePath string, result any) error {
if nil == result {
return errors.New("接收读取结果的数据指针为NIL")
}
@ -78,7 +78,7 @@ func (f *file) ReadYmlContent(filePath string, result interface{}) error {
if fileContent, err = f.ReadFileContent(filePath); nil != err {
return err
}
return yml.Unmarshal(fileContent, result)
return Yml.UnmarshalWithNumber(fileContent, result)
}
// ReadJSONContent 读取JSON内容,并解析到指定的结构体中
@ -86,7 +86,7 @@ func (f *file) ReadYmlContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 15:23 2022/6/9
func (f *file) ReadJSONContent(filePath string, result interface{}) error {
func (f *file) ReadJSONContent(filePath string, result any) error {
if nil == result {
return errors.New("接收读取结果的数据指针为NIL")
}
@ -105,7 +105,7 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:21 2022/7/2
func (f *file) ReadIniContent(filePath string, result interface{}) error {
func (f *file) ReadIniContent(filePath string, result any) error {
if nil == result {
return errors.New("接收读取结果的数据指针为NIL")
}
@ -117,46 +117,58 @@ func (f *file) ReadIniContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:08 2023/7/31
func (f *file) ReadTomlContent(filePath string, result interface{}) error {
func (f *file) ReadTomlContent(filePath string, result any) error {
if nil == result {
return errors.New("接收读取结果的数据指针为NIL")
}
if _, err := toml.DecodeFile("example.toml", result); err != nil {
if _, err := toml.DecodeFile(filePath, result); err != nil {
return err
}
return nil
}
// ReadXmlContent ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:39 2024/10/23
func (f *file) ReadXmlContent(filePath string, result any) error {
var (
fileContent []byte
err error
)
if fileContent, err = f.ReadFileContent(filePath); nil != err {
return err
}
return xml.Unmarshal(fileContent, result)
}
// ReadAnyFileContent 读取任意类型的文件并解析
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 13:11 2022/7/2
func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error {
func (f *file) ReadAnyFileContent(filePath string, receiver any) error {
var (
parseFunc func(filePath string, receiver interface{}) error
parseFunc func(filePath string, receiver any) error
)
filePathArr := strings.Split(filePath, "#@#")
fileExt := ""
filePath = filePathArr[0]
if len(filePathArr) == 2 && len(filePathArr[1]) > 0 {
fileExt = strings.ToLower(filePathArr[1])
} else {
fileExt = strings.ToLower(path.Ext(filePath))
}
fileExt := consts.FileType(strings.TrimLeft(strings.ToLower(path.Ext(filePath)), "."))
switch fileExt {
case consts.FileTypeJson:
parseFunc = f.ReadJSONContent
case consts.FileTypeYml:
fallthrough
case consts.FileTypeYaml:
case consts.FileTypeYml, consts.FileTypeYaml:
parseFunc = f.ReadYmlContent
case consts.FileTypeIni:
parseFunc = f.ReadIniContent
case consts.FileTypeToml:
parseFunc = f.ReadTomlContent
case consts.FileTypeXml:
parseFunc = f.ReadXmlContent
default:
return errors.New(fileExt + " 暂不支持当前格式的文件解析")
return errors.New(fileExt.String() + " 暂不支持当前格式的文件解析")
}
return parseFunc(filePath, receiver)
}
@ -293,3 +305,33 @@ func (f *file) ReadFileListRecurve(rootDir string) ([]*define.FileInfo, error) {
func (f *file) FileExt(filePath string) string {
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
View 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
View File

@ -1,14 +1,16 @@
module git.zhangdeman.cn/zhangdeman/serialize
go 1.21
go 1.23.0
toolchain go1.21.4
toolchain go1.24.2
require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995
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/sbabiv/xml2map v1.2.1
gopkg.in/yaml.v3 v3.0.1
)
require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763
require github.com/stretchr/testify v1.9.0 // indirect

41
go.sum
View File

@ -1,32 +1,27 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c h1:Dan3iSVU6XTKt8r3/qixfPHPpfLZjkYlPmaJios7wtE=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20230811030300-6f850372c88c/go.mod h1:IXXaZkb7vGzGnGM5RRWrASAuwrVSNxuoe0DmeXx5g6k=
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/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/consts v0.0.0-20250425024726-cc17224cb995 h1:LmPRAf0AsxRVFPibdpZR89ajlsz8hof2IvMMyTqiEq4=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
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=
github.com/BurntSushi/toml v1.3.2 h1:o7IhLm0Msx3BaB+n3Ag7L8EVlByGnpq14C4YWiu/gL8=
github.com/BurntSushi/toml v1.3.2/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
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/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
github.com/BurntSushi/toml v1.5.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/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/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/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/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=

71
ini.go Normal file
View 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)
}

83
json.go
View File

@ -33,12 +33,16 @@ func init() {
type ownJSON struct {
}
func (oj *ownJSON) Unmarshal(byteData []byte, receiver any) error {
return oj.UnmarshalWithNumber(byteData, receiver)
}
// UnmarshalWithNumber 解析json
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:39 下午 2021/9/14
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) error {
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver any) error {
decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber()
return decoder.Decode(receiver)
@ -49,7 +53,7 @@ func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) er
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:46 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver interface{}) {
func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
_ = oj.UnmarshalWithNumber(byteData, receiver)
}
@ -58,7 +62,7 @@ func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver inte
// Author : go_developer@163.com<白茶清欢>
//
// Date : 8:43 下午 2021/9/14
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver interface{}) error {
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
decoder := json.NewDecoder(ioReader)
decoder.UseNumber()
return decoder.Decode(receiver)
@ -69,7 +73,7 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiv
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:47 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver interface{}) {
func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
_ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver)
return
}
@ -79,7 +83,7 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadClo
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:50 2023/7/22
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error {
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver any) error {
return oj.UnmarshalWithNumber([]byte(input), receiver)
}
@ -88,7 +92,7 @@ func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface
// Author : go_developer@163.com<白茶清欢>
//
// Date : 20:48 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver interface{}) {
func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver)
}
@ -97,12 +101,24 @@ func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receive
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
func (oj *ownJSON) MarshalForByte(input any) ([]byte, error) {
buffer := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false)
_ = encoder.Encode(input)
return buffer.Bytes()
if err := encoder.Encode(input); nil != err {
return nil, err
}
return buffer.Bytes(), nil
}
// MarshalForByteIgnoreError 序列化并忽略异常
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:16 2024/10/23
func (oj *ownJSON) MarshalForByteIgnoreError(input any) []byte {
byteData, _ := oj.MarshalForByte(input)
return byteData
}
// MarshalForString 序列化并返回字符串
@ -110,8 +126,21 @@ func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForString(input interface{}) string {
return string(oj.MarshalForByte(input))
func (oj *ownJSON) MarshalForString(input any) (string, error) {
byteData, err := oj.MarshalForByte(input)
if nil != err {
return "", err
}
return string(byteData), nil
}
// MarshalForStringIgnoreError ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:19 2024/10/23
func (oj *ownJSON) MarshalForStringIgnoreError(input any) string {
return string(oj.MarshalForByteIgnoreError(input))
}
// Transition 数据结构转换 map/slice/struct => struct | struct => map/slice/struct
@ -119,8 +148,8 @@ func (oj *ownJSON) MarshalForString(input interface{}) string {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 18:25 2023/12/29
func (oj *ownJSON) Transition(input interface{}, receiver interface{}) error {
return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
func (oj *ownJSON) Transition(input any, receiver any) error {
return oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver)
}
// TransitionIgnoreError ...
@ -128,8 +157,8 @@ func (oj *ownJSON) Transition(input interface{}, receiver interface{}) error {
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:57 2024/1/10
func (oj *ownJSON) TransitionIgnoreError(input interface{}, receiver interface{}) {
_ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) {
_ = oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver)
}
// MergeDataForMap 合并数据到map
@ -137,15 +166,15 @@ func (oj *ownJSON) TransitionIgnoreError(input interface{}, receiver interface{}
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:48 2024/1/10
func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (map[string]interface{}, error) {
res := make(map[string]interface{})
func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...any) (map[string]any, error) {
res := make(map[string]any)
for _, data := range dataList {
if nil == data {
continue
}
var (
err error
itemRes map[string]interface{}
itemRes map[string]any
)
if err = oj.Transition(data, &itemRes); nil != err {
@ -156,7 +185,13 @@ func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (m
}
for k, v := range itemRes {
res[k] = v
if v == nil {
if _, exist := res[k]; !exist {
res[k] = v
}
} else {
res[k] = v
}
}
}
return res, nil
@ -167,7 +202,7 @@ func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (m
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:00 2024/1/10
func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...interface{}) map[string]interface{} {
func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...any) map[string]any {
res, _ := oj.MergeDataForMap(true, dataList)
return res
}
@ -177,8 +212,8 @@ func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...interface{}) map[strin
// Author : go_developer@163.com<白茶清欢>
//
// Date : 17:07 2024/1/10
func (oj *ownJSON) MergeDataForReceiver(receiver interface{}, dataList ...interface{}) error {
res, err := oj.MergeDataForMap(false, dataList)
func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error {
res, err := oj.MergeDataForMap(false, dataList...)
if nil != err {
return err
}
@ -193,7 +228,7 @@ func (oj *ownJSON) MergeDataForReceiver(receiver interface{}, dataList ...interf
// Author : go_developer@163.com<白茶清欢>
//
// Date : 5:45 下午 2021/11/5
func (oj *ownJSON) ConsoleOutput(data interface{}) {
func (oj *ownJSON) ConsoleOutput(data any) {
var out bytes.Buffer
switch reflect.TypeOf(data).Kind() {
case reflect.Slice:
@ -205,7 +240,7 @@ func (oj *ownJSON) ConsoleOutput(data interface{}) {
case reflect.Struct:
fallthrough
case reflect.Ptr:
_ = json.Indent(&out, []byte(oj.MarshalForString(data)+"\n"), "", "\t")
_ = json.Indent(&out, []byte(oj.MarshalForStringIgnoreError(data)+"\n"), "", "\t")
_, _ = out.WriteTo(os.Stdout)
return
case reflect.Int:

97
toml.go Normal file
View 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
View 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)
}

121
xml.go Normal file
View File

@ -0,0 +1,121 @@
// Package serialize ...
//
// Description : serialize ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-23 17:51
package serialize
import (
"bytes"
"encoding/xml"
"github.com/sbabiv/xml2map"
"io"
)
var (
Xml = &ownXml{}
)
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 {
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) {
_ = o.UnmarshalWithNumber(byteData, receiver)
return
}
func (o *ownXml) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
return xml.NewDecoder(ioReader).Decode(receiver)
}
func (o *ownXml) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
_ = o.UnmarshalWithNumberForIOReader(ioReader, receiver)
return
}
func (o *ownXml) UnmarshalWithNumberForString(input string, receiver any) error {
return o.UnmarshalWithNumber([]byte(input), receiver)
}
func (o *ownXml) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
_ = o.UnmarshalWithNumberForString(input, receiver)
return
}
func (o *ownXml) MarshalForByte(input any) ([]byte, error) {
return xml.Marshal(input)
}
func (o *ownXml) MarshalForByteIgnoreError(input any) []byte {
byteData, _ := o.MarshalForByte(input)
return byteData
}
func (o *ownXml) MarshalForString(input any) (string, error) {
byteData, err := o.MarshalForByte(input)
if nil != err {
return "", err
}
return string(byteData), nil
}
func (o *ownXml) MarshalForStringIgnoreError(input any) string {
str, _ := o.MarshalForString(input)
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
}

71
yml.go Normal file
View File

@ -0,0 +1,71 @@
// Package serialize ...
//
// Description : serialize ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-23 17:06
package serialize
import (
"bytes"
"gopkg.in/yaml.v3"
"io"
)
var (
Yml = &ownYml{}
)
type ownYml struct{}
func (o *ownYml) MarshalForByte(input any) ([]byte, error) {
return yaml.Marshal(input)
}
func (o *ownYml) MarshalForByteIgnoreError(input any) []byte {
byteData, _ := o.MarshalForByte(input)
return byteData
}
func (o *ownYml) MarshalForString(input any) (string, error) {
byteData, err := o.MarshalForByte(input)
if nil != err {
return "", err
}
return string(byteData), nil
}
func (o *ownYml) MarshalForStringIgnoreError(input any) string {
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 {
return yaml.NewDecoder(bytes.NewReader(byteData)).Decode(receiver)
}
func (o *ownYml) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
_ = o.UnmarshalWithNumber(byteData, receiver)
return
}
func (o *ownYml) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
return yaml.NewDecoder(ioReader).Decode(receiver)
}
func (o *ownYml) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
_ = o.UnmarshalWithNumberForIOReader(ioReader, receiver)
return
}
func (o *ownYml) UnmarshalWithNumberForString(input string, receiver any) error {
return o.UnmarshalWithNumber([]byte(input), receiver)
}
func (o *ownYml) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
_ = o.UnmarshalWithNumberForString(input, receiver)
return
}