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] =?UTF-8?q?=E6=94=AF=E6=8C=81=E5=BA=8F=E5=88=97=E5=8C=96?= =?UTF-8?q?=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 +}