From 8d68e6106ea91b4d563c65e8de317ab39252cc31 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sun, 4 May 2025 13:59:08 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8D=87=E7=BA=A7=E6=8E=A5=E5=8F=A3=E7=BA=A6?= =?UTF-8?q?=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/serialize.go | 2 ++ go.mod | 6 ++---- ini.go | 4 ++++ json.go | 4 ++++ toml.go | 4 ++++ xml.go | 3 +++ yml.go | 3 +++ 7 files changed, 22 insertions(+), 4 deletions(-) diff --git a/abstract/serialize.go b/abstract/serialize.go index d662899..1a02566 100644 --- a/abstract/serialize.go +++ b/abstract/serialize.go @@ -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 反序列化,同时解析数字, 忽略结果的成功与失败 diff --git a/go.mod b/go.mod index 986ef6e..76829e8 100644 --- a/go.mod +++ b/go.mod @@ -9,10 +9,8 @@ require ( 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 ) -require ( - github.com/sbabiv/xml2map v1.2.1 // indirect - github.com/stretchr/testify v1.9.0 // indirect -) +require github.com/stretchr/testify v1.9.0 // indirect diff --git a/ini.go b/ini.go index 1eea5fe..6541ea8 100644 --- a/ini.go +++ b/ini.go @@ -20,6 +20,10 @@ var ( 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") diff --git a/json.go b/json.go index 0fdb977..faf6fc5 100644 --- a/json.go +++ b/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<白茶清欢> diff --git a/toml.go b/toml.go index e22da4d..9d209d1 100644 --- a/toml.go +++ b/toml.go @@ -21,6 +21,10 @@ var ( 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") diff --git a/xml.go b/xml.go index daeaeba..7562f0e 100644 --- a/xml.go +++ b/xml.go @@ -20,6 +20,9 @@ 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 { res, err := xml2map.NewDecoder(bytes.NewReader(byteData)).Decode() if nil != err { diff --git a/yml.go b/yml.go index a1de86a..20361b9 100644 --- a/yml.go +++ b/yml.go @@ -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) }