Compare commits

3 Commits

Author SHA1 Message Date
86c1a4a3a9 feat: add func 2025-10-13 12:45:11 +08:00
8d68e6106e 升级接口约束 2025-05-04 13:59:08 +08:00
0c3d7b7e92 升级xml解析 2025-05-03 19:45:48 +08:00
8 changed files with 40 additions and 4 deletions

View File

@ -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 反序列化,同时解析数字, 忽略结果的成功与失败

8
go.mod
View File

@ -5,10 +5,12 @@ go 1.23.0
toolchain go1.24.2 toolchain go1.24.2
require ( require (
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772
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.5.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
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995 )
github.com/stretchr/testify v1.9.0 // indirect
) require github.com/stretchr/testify v1.9.0 // indirect

4
go.sum
View File

@ -1,5 +1,7 @@
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250425024726-cc17224cb995 h1:LmPRAf0AsxRVFPibdpZR89ajlsz8hof2IvMMyTqiEq4= 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/consts v0.0.0-20250425024726-cc17224cb995/go.mod h1:5p8CEKGBxi7qPtTXDI3HDmqKAfIm5i/aBWdrbkbdNjc=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772 h1:Yo1ur3LnDF5s7F7tpJsNrdUSF8LwYKnN9TdQU32F3eU=
git.zhangdeman.cn/zhangdeman/consts v0.0.0-20250916024308-d378e6c57772/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 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.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg= github.com/BurntSushi/toml v1.5.0 h1:W5quZX/G/csjUnuI8SUYlsHs9M38FC7znL0lIO+DvMg=
@ -14,6 +16,8 @@ github.com/jtolds/gls v4.20.0+incompatible h1:xdiiI2gbIgH/gLH7ADydsJ1uDOEzR8yvV7
github.com/jtolds/gls v4.20.0+incompatible/go.mod h1:QJZ7F/aHp+rZTRtaJ1ow/lLfFfVYBRgL+9YlvaHOwJU= 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/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= 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 h1:cR//PqUBUiQRakZWqBiFFQ9wb8emQGDb0HeGdqGByCY=
github.com/smarty/assertions v1.15.0/go.mod h1:yABtdzeQs6l1brC900WlRNwj6ZR55d7B+E8C6HtKdec= 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 h1:qGjIddxOk4grTu9JPOU31tVfq3cNdBlNa5sSznIX1xY=

4
ini.go
View File

@ -20,6 +20,10 @@ var (
type ownIni struct { 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 { func (o *ownIni) UnmarshalWithNumber(byteData []byte, receiver any) error {
if nil == receiver { if nil == receiver {
return errors.New("receiver is nil") return errors.New("receiver is nil")

View File

@ -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<白茶清欢>
@ -74,6 +78,11 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadClo
return return
} }
// UnmarshalForString ...
func (oj *ownJSON) UnmarshalForString(input string, receiver any) error {
return oj.Unmarshal([]byte(input), receiver)
}
// UnmarshalWithNumberForString 字符串转结构体 // UnmarshalWithNumberForString 字符串转结构体
// //
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>

View File

@ -21,6 +21,10 @@ var (
type ownToml struct { 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) { func (o *ownToml) Parse(byteData []byte, receiver any) (*toml.MetaData, error) {
if nil == receiver { if nil == receiver {
return nil, errors.New("receiver is nil") return nil, errors.New("receiver is nil")

10
xml.go
View File

@ -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) {

3
yml.go
View File

@ -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)
} }