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