Compare commits
6 Commits
73cf1be49c
...
master
Author | SHA1 | Date | |
---|---|---|---|
8d68e6106e | |||
0c3d7b7e92 | |||
135850ee8a | |||
774d4bdc07 | |||
f617c9cda4 | |||
8a9502c122 |
@ -15,6 +15,8 @@ import "io"
|
||||
//
|
||||
// Date : 16:55 2024/10/23
|
||||
type Serializable interface {
|
||||
// Unmarshal 反序列化
|
||||
Unmarshal(byteData []byte, receiver any) error
|
||||
// UnmarshalWithNumber 反序列化,同时解析数字
|
||||
UnmarshalWithNumber(byteData []byte, receiver any) error
|
||||
// UnmarshalWithNumberIgnoreError 反序列化,同时解析数字, 忽略结果的成功与失败
|
||||
|
8
go.mod
8
go.mod
@ -5,10 +5,12 @@ go 1.23.0
|
||||
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.5.0
|
||||
github.com/go-ini/ini v1.67.0
|
||||
github.com/sbabiv/xml2map v1.2.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
|
||||
|
2
go.sum
2
go.sum
@ -14,6 +14,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/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
|
||||
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=
|
||||
|
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)
|
||||
}
|
4
json.go
4
json.go
@ -33,6 +33,10 @@ 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<白茶清欢>
|
||||
|
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)
|
||||
}
|
10
xml.go
10
xml.go
@ -10,6 +10,7 @@ package serialize
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/xml"
|
||||
"github.com/sbabiv/xml2map"
|
||||
"io"
|
||||
)
|
||||
|
||||
@ -19,8 +20,15 @@ var (
|
||||
|
||||
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 {
|
||||
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) {
|
||||
|
5
yml.go
5
yml.go
@ -14,7 +14,7 @@ import (
|
||||
)
|
||||
|
||||
var (
|
||||
Yml = ownYml{}
|
||||
Yml = &ownYml{}
|
||||
)
|
||||
|
||||
type ownYml struct{}
|
||||
@ -40,6 +40,9 @@ 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)
|
||||
}
|
||||
|
Reference in New Issue
Block a user