From 454b1e8ac67e5188a3041bef0af234a9825df119 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 17:04:10 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0=E6=8E=A5=E5=8F=A3=E7=BA=A6=E6=9D=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/serialize.go | 34 ++++++++++++++++++++++++++++++++++ file.go | 12 ++++++------ json.go | 32 ++++++++++++++++---------------- 3 files changed, 56 insertions(+), 22 deletions(-) create mode 100644 abstract/serialize.go diff --git a/abstract/serialize.go b/abstract/serialize.go new file mode 100644 index 0000000..af62ef0 --- /dev/null +++ b/abstract/serialize.go @@ -0,0 +1,34 @@ +// 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 { + // 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 + // MarshalForString 序列化为字符串 + MarshalForString(input any) string +} diff --git a/file.go b/file.go index 8b685ba..580d3e7 100644 --- a/file.go +++ b/file.go @@ -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") } @@ -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,7 +117,7 @@ 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") } @@ -132,9 +132,9 @@ func (f *file) ReadTomlContent(filePath string, result interface{}) error { // 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 := "" diff --git a/json.go b/json.go index 3cbe0c4..2746d72 100644 --- a/json.go +++ b/json.go @@ -38,7 +38,7 @@ type ownJSON struct { // 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 +49,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 +58,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 +69,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 +79,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 +88,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,7 +97,7 @@ 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 { buffer := bytes.NewBuffer([]byte{}) encoder := json.NewEncoder(buffer) encoder.SetEscapeHTML(false) @@ -110,7 +110,7 @@ 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 { +func (oj *ownJSON) MarshalForString(input any) string { return string(oj.MarshalForByte(input)) } @@ -119,7 +119,7 @@ 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 { +func (oj *ownJSON) Transition(input any, receiver any) error { return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) } @@ -128,7 +128,7 @@ 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{}) { +func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) { _ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) } @@ -137,15 +137,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 { @@ -167,7 +167,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,7 +177,7 @@ 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 { +func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error { res, err := oj.MergeDataForMap(false, dataList) if nil != err { return err @@ -193,7 +193,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: From 82ce67170af4e2365088009decbcb7e5e999baf7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 17:25:10 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=A2=9E=E5=8A=A0yaml=E6=96=87=E4=BB=B6?= =?UTF-8?q?=E7=9A=84=E5=BA=8F=E5=88=97=E5=8C=96=E4=B8=8E=E5=8F=8D=E5=BA=8F?= =?UTF-8?q?=E5=88=97=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- abstract/serialize.go | 6 ++-- go.mod | 2 +- go.sum | 2 ++ json.go | 41 +++++++++++++++++++++----- yml.go | 68 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 108 insertions(+), 11 deletions(-) create mode 100644 yml.go diff --git a/abstract/serialize.go b/abstract/serialize.go index af62ef0..d662899 100644 --- a/abstract/serialize.go +++ b/abstract/serialize.go @@ -28,7 +28,9 @@ type Serializable interface { // UnmarshalWithNumberForStringIgnoreError 序列化字符串反解析为结构体,并忽略异常 UnmarshalWithNumberForStringIgnoreError(input string, receiver any) // MarshalForByte 序列化为字节数组 - MarshalForByte(input any) []byte + MarshalForByte(input any) ([]byte, error) + MarshalForByteIgnoreError(input any) []byte // MarshalForString 序列化为字符串 - MarshalForString(input any) string + MarshalForString(input any) (string, error) + MarshalForStringIgnoreError(input any) string } diff --git a/go.mod b/go.mod index 0d97610..c7d970b 100644 --- a/go.mod +++ b/go.mod @@ -11,4 +11,4 @@ require ( gopkg.in/yaml.v3 v3.0.1 ) -require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241015035056-b6f4a6512763 +require git.zhangdeman.cn/zhangdeman/consts v0.0.0-20241023090605-10cff9173059 diff --git a/go.sum b/go.sum index f8a136d..8af8128 100644 --- a/go.sum +++ b/go.sum @@ -8,6 +8,8 @@ git.zhangdeman.cn/zhangdeman/consts v0.0.0-20240617073616-39e82fd033ed h1:BGv+y6 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= diff --git a/json.go b/json.go index 2746d72..d0ffc50 100644 --- a/json.go +++ b/json.go @@ -97,12 +97,24 @@ func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receive // Author : go_developer@163.com<白茶清欢> // // Date : 21:56 2023/7/22 -func (oj *ownJSON) MarshalForByte(input any) []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 +122,21 @@ func (oj *ownJSON) MarshalForByte(input any) []byte { // Author : go_developer@163.com<白茶清欢> // // Date : 21:56 2023/7/22 -func (oj *ownJSON) MarshalForString(input any) 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 @@ -120,7 +145,7 @@ func (oj *ownJSON) MarshalForString(input any) string { // // Date : 18:25 2023/12/29 func (oj *ownJSON) Transition(input any, receiver any) error { - return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) + return oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver) } // TransitionIgnoreError ... @@ -129,7 +154,7 @@ func (oj *ownJSON) Transition(input any, receiver any) error { // // Date : 16:57 2024/1/10 func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) { - _ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) + _ = oj.UnmarshalWithNumber(oj.MarshalForByteIgnoreError(input), receiver) } // MergeDataForMap 合并数据到map @@ -205,7 +230,7 @@ func (oj *ownJSON) ConsoleOutput(data any) { 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: diff --git a/yml.go b/yml.go new file mode 100644 index 0000000..c4e170f --- /dev/null +++ b/yml.go @@ -0,0 +1,68 @@ +// 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) 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 +} From 77f0930081569a81915788710ca7fa2104f67fbb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 17:27:26 +0800 Subject: [PATCH 3/5] fix --- file.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/file.go b/file.go index 580d3e7..4b4f2e8 100644 --- a/file.go +++ b/file.go @@ -121,7 +121,7 @@ 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 @@ -147,9 +147,7 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error { 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 From bfa244f97d5de4a378d6ba2cc91ecd077912e4ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 17:55:41 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=20xml=20=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- xml.go | 65 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 xml.go diff --git a/xml.go b/xml.go new file mode 100644 index 0000000..048ad52 --- /dev/null +++ b/xml.go @@ -0,0 +1,65 @@ +// Package serialize ... +// +// Description : serialize ... +// +// Author : go_developer@163.com<白茶清欢> +// +// Date : 2024-10-23 17:51 +package serialize + +import ( + "bytes" + "encoding/xml" + "io" +) + +type ownXml struct{} + +func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error { + return xml.NewDecoder(bytes.NewReader(byteData)).Decode(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 +} From b9826a24a06895a41a3c29b9ee8c9b04ad03205d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Wed, 23 Oct 2024 18:41:41 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=94=AF=E6=8C=81xml=E9=85=8D=E7=BD=AE?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E8=A7=A3=E6=9E=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- file.go | 25 ++++++++++++++++++++++--- xml.go | 4 ++++ 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/file.go b/file.go index 4b4f2e8..f9366fa 100644 --- a/file.go +++ b/file.go @@ -8,6 +8,7 @@ package serialize import ( + "encoding/xml" "errors" "git.zhangdeman.cn/zhangdeman/consts" "io" @@ -22,8 +23,6 @@ import ( "git.zhangdeman.cn/zhangdeman/util/define" "github.com/go-ini/ini" - - yml "gopkg.in/yaml.v3" ) var ( @@ -78,7 +77,7 @@ func (f *file) ReadYmlContent(filePath string, result any) error { if fileContent, err = f.ReadFileContent(filePath); nil != err { return err } - return yml.Unmarshal(fileContent, result) + return Yml.UnmarshalWithNumber(fileContent, result) } // ReadJSONContent 读取JSON内容,并解析到指定的结构体中 @@ -127,6 +126,24 @@ func (f *file) ReadTomlContent(filePath string, result any) error { 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<白茶清欢> @@ -153,6 +170,8 @@ func (f *file) ReadAnyFileContent(filePath string, receiver any) error { parseFunc = f.ReadIniContent case consts.FileTypeToml: parseFunc = f.ReadTomlContent + case consts.FileTypeXml: + parseFunc = f.ReadXmlContent default: return errors.New(fileExt + " 暂不支持当前格式的文件解析") } diff --git a/xml.go b/xml.go index 048ad52..f7d4250 100644 --- a/xml.go +++ b/xml.go @@ -13,6 +13,10 @@ import ( "io" ) +var ( + Xml = &ownXml{} +) + type ownXml struct{} func (o *ownXml) UnmarshalWithNumber(byteData []byte, receiver any) error {