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 +}