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] =?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 +}