From 8a9502c1221710ff152c91d5895a3183a6846073 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 28 Apr 2025 11:23:27 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0toml=E5=86=85=E5=AE=B9?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- toml.go | 93 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 toml.go diff --git a/toml.go b/toml.go new file mode 100644 index 0000000..72c8b0b --- /dev/null +++ b/toml.go @@ -0,0 +1,93 @@ +// 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) 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 +} From f617c9cda4c1604bc9b494747dc757a01a435ce7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 28 Apr 2025 11:52:29 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E5=A2=9E=E5=8A=A0ini=E5=BA=8F=E5=88=97?= =?UTF-8?q?=E5=8C=96=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ini.go | 67 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++ toml.go | 2 +- 2 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 ini.go diff --git a/ini.go b/ini.go new file mode 100644 index 0000000..1eea5fe --- /dev/null +++ b/ini.go @@ -0,0 +1,67 @@ +// 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) 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) +} diff --git a/toml.go b/toml.go index 72c8b0b..e22da4d 100644 --- a/toml.go +++ b/toml.go @@ -15,7 +15,7 @@ import ( ) var ( - Toml *ownToml + Toml = &ownToml{} ) type ownToml struct { From 774d4bdc07868462f4d5ae4d7dc888f62bd7692d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Mon, 28 Apr 2025 12:10:59 +0800 Subject: [PATCH 3/3] =?UTF-8?q?wrapper=E9=AB=98=E7=BA=A7=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E5=BA=8F=E5=88=97=E5=8C=96=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- wrapper.go | 76 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ yml.go | 2 +- 2 files changed, 77 insertions(+), 1 deletion(-) create mode 100644 wrapper.go diff --git a/wrapper.go b/wrapper.go new file mode 100644 index 0000000..8663b71 --- /dev/null +++ b/wrapper.go @@ -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) +} diff --git a/yml.go b/yml.go index c4e170f..a1de86a 100644 --- a/yml.go +++ b/yml.go @@ -14,7 +14,7 @@ import ( ) var ( - Yml = ownYml{} + Yml = &ownYml{} ) type ownYml struct{}