序列化增加接口约束

This commit is contained in:
白茶清欢 2024-10-23 17:04:10 +08:00
parent d02ff0d1e0
commit 454b1e8ac6
3 changed files with 56 additions and 22 deletions

34
abstract/serialize.go Normal file
View File

@ -0,0 +1,34 @@
// Package abstract ...
//
// Description : abstract ...
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 2024-10-23 16:54
package abstract
import "io"
// Serializable 序列化的接口约束
//
// Author : go_developer@163.com<白茶清欢>
//
// Date : 16:55 2024/10/23
type Serializable interface {
// UnmarshalWithNumber 反序列化,同时解析数字
UnmarshalWithNumber(byteData []byte, receiver any) error
// UnmarshalWithNumberIgnoreError 反序列化,同时解析数字, 忽略结果的成功与失败
UnmarshalWithNumberIgnoreError(byteData []byte, receiver any)
// UnmarshalWithNumberForIOReader 从文件流读取数据并反序列化
UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error
// UnmarshalWithNumberForIOReaderIgnoreError 从文件流读取数据并反序列化, 忽略异常
UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any)
// UnmarshalWithNumberForString 序列化字符串反解析为结构体
UnmarshalWithNumberForString(input string, receiver any) error
// UnmarshalWithNumberForStringIgnoreError 序列化字符串反解析为结构体,并忽略异常
UnmarshalWithNumberForStringIgnoreError(input string, receiver any)
// MarshalForByte 序列化为字节数组
MarshalForByte(input any) []byte
// MarshalForString 序列化为字符串
MarshalForString(input any) string
}

12
file.go
View File

@ -67,7 +67,7 @@ func (f *file) GetProjectPath() (string, error) {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 10:35 下午 2021/4/26 // Date : 10:35 下午 2021/4/26
func (f *file) ReadYmlContent(filePath string, result interface{}) error { func (f *file) ReadYmlContent(filePath string, result any) error {
if nil == result { if nil == result {
return errors.New("接收读取结果的数据指针为NIL") return errors.New("接收读取结果的数据指针为NIL")
} }
@ -86,7 +86,7 @@ func (f *file) ReadYmlContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 15:23 2022/6/9 // Date : 15:23 2022/6/9
func (f *file) ReadJSONContent(filePath string, result interface{}) error { func (f *file) ReadJSONContent(filePath string, result any) error {
if nil == result { if nil == result {
return errors.New("接收读取结果的数据指针为NIL") return errors.New("接收读取结果的数据指针为NIL")
} }
@ -105,7 +105,7 @@ func (f *file) ReadJSONContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 13:21 2022/7/2 // Date : 13:21 2022/7/2
func (f *file) ReadIniContent(filePath string, result interface{}) error { func (f *file) ReadIniContent(filePath string, result any) error {
if nil == result { if nil == result {
return errors.New("接收读取结果的数据指针为NIL") return errors.New("接收读取结果的数据指针为NIL")
} }
@ -117,7 +117,7 @@ func (f *file) ReadIniContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:08 2023/7/31 // Date : 21:08 2023/7/31
func (f *file) ReadTomlContent(filePath string, result interface{}) error { func (f *file) ReadTomlContent(filePath string, result any) error {
if nil == result { if nil == result {
return errors.New("接收读取结果的数据指针为NIL") return errors.New("接收读取结果的数据指针为NIL")
} }
@ -132,9 +132,9 @@ func (f *file) ReadTomlContent(filePath string, result interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 13:11 2022/7/2 // Date : 13:11 2022/7/2
func (f *file) ReadAnyFileContent(filePath string, receiver interface{}) error { func (f *file) ReadAnyFileContent(filePath string, receiver any) error {
var ( var (
parseFunc func(filePath string, receiver interface{}) error parseFunc func(filePath string, receiver any) error
) )
filePathArr := strings.Split(filePath, "#@#") filePathArr := strings.Split(filePath, "#@#")
fileExt := "" fileExt := ""

32
json.go
View File

@ -38,7 +38,7 @@ type ownJSON struct {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:39 下午 2021/9/14 // Date : 8:39 下午 2021/9/14
func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) error { func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver any) error {
decoder := json.NewDecoder(bytes.NewReader(byteData)) decoder := json.NewDecoder(bytes.NewReader(byteData))
decoder.UseNumber() decoder.UseNumber()
return decoder.Decode(receiver) return decoder.Decode(receiver)
@ -49,7 +49,7 @@ func (oj *ownJSON) UnmarshalWithNumber(byteData []byte, receiver interface{}) er
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:46 2023/12/24 // Date : 20:46 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver interface{}) { func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver any) {
_ = oj.UnmarshalWithNumber(byteData, receiver) _ = oj.UnmarshalWithNumber(byteData, receiver)
} }
@ -58,7 +58,7 @@ func (oj *ownJSON) UnmarshalWithNumberIgnoreError(byteData []byte, receiver inte
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 8:43 下午 2021/9/14 // Date : 8:43 下午 2021/9/14
func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver interface{}) error { func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiver any) error {
decoder := json.NewDecoder(ioReader) decoder := json.NewDecoder(ioReader)
decoder.UseNumber() decoder.UseNumber()
return decoder.Decode(receiver) return decoder.Decode(receiver)
@ -69,7 +69,7 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReader(ioReader io.ReadCloser, receiv
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:47 2023/12/24 // Date : 20:47 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver interface{}) { func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadCloser, receiver any) {
_ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver) _ = oj.UnmarshalWithNumberForIOReader(ioReader, receiver)
return return
} }
@ -79,7 +79,7 @@ func (oj *ownJSON) UnmarshalWithNumberForIOReaderIgnoreError(ioReader io.ReadClo
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:50 2023/7/22 // Date : 21:50 2023/7/22
func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface{}) error { func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver any) error {
return oj.UnmarshalWithNumber([]byte(input), receiver) return oj.UnmarshalWithNumber([]byte(input), receiver)
} }
@ -88,7 +88,7 @@ func (oj *ownJSON) UnmarshalWithNumberForString(input string, receiver interface
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 20:48 2023/12/24 // Date : 20:48 2023/12/24
func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver interface{}) { func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receiver any) {
oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver) oj.UnmarshalWithNumberIgnoreError([]byte(input), receiver)
} }
@ -97,7 +97,7 @@ func (oj *ownJSON) UnmarshalWithNumberForStringIgnoreError(input string, receive
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:56 2023/7/22 // Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForByte(input interface{}) []byte { func (oj *ownJSON) MarshalForByte(input any) []byte {
buffer := bytes.NewBuffer([]byte{}) buffer := bytes.NewBuffer([]byte{})
encoder := json.NewEncoder(buffer) encoder := json.NewEncoder(buffer)
encoder.SetEscapeHTML(false) encoder.SetEscapeHTML(false)
@ -110,7 +110,7 @@ func (oj *ownJSON) MarshalForByte(input interface{}) []byte {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 21:56 2023/7/22 // Date : 21:56 2023/7/22
func (oj *ownJSON) MarshalForString(input interface{}) string { func (oj *ownJSON) MarshalForString(input any) string {
return string(oj.MarshalForByte(input)) return string(oj.MarshalForByte(input))
} }
@ -119,7 +119,7 @@ func (oj *ownJSON) MarshalForString(input interface{}) string {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 18:25 2023/12/29 // Date : 18:25 2023/12/29
func (oj *ownJSON) Transition(input interface{}, receiver interface{}) error { func (oj *ownJSON) Transition(input any, receiver any) error {
return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) return oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
} }
@ -128,7 +128,7 @@ func (oj *ownJSON) Transition(input interface{}, receiver interface{}) error {
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:57 2024/1/10 // Date : 16:57 2024/1/10
func (oj *ownJSON) TransitionIgnoreError(input interface{}, receiver interface{}) { func (oj *ownJSON) TransitionIgnoreError(input any, receiver any) {
_ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver) _ = oj.UnmarshalWithNumber(oj.MarshalForByte(input), receiver)
} }
@ -137,15 +137,15 @@ func (oj *ownJSON) TransitionIgnoreError(input interface{}, receiver interface{}
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 16:48 2024/1/10 // Date : 16:48 2024/1/10
func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (map[string]interface{}, error) { func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...any) (map[string]any, error) {
res := make(map[string]interface{}) res := make(map[string]any)
for _, data := range dataList { for _, data := range dataList {
if nil == data { if nil == data {
continue continue
} }
var ( var (
err error err error
itemRes map[string]interface{} itemRes map[string]any
) )
if err = oj.Transition(data, &itemRes); nil != err { if err = oj.Transition(data, &itemRes); nil != err {
@ -167,7 +167,7 @@ func (oj *ownJSON) MergeDataForMap(ignoreError bool, dataList ...interface{}) (m
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:00 2024/1/10 // Date : 17:00 2024/1/10
func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...interface{}) map[string]interface{} { func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...any) map[string]any {
res, _ := oj.MergeDataForMap(true, dataList) res, _ := oj.MergeDataForMap(true, dataList)
return res return res
} }
@ -177,7 +177,7 @@ func (oj *ownJSON) MergeDataForMapIgnoreError(dataList ...interface{}) map[strin
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 17:07 2024/1/10 // Date : 17:07 2024/1/10
func (oj *ownJSON) MergeDataForReceiver(receiver interface{}, dataList ...interface{}) error { func (oj *ownJSON) MergeDataForReceiver(receiver any, dataList ...any) error {
res, err := oj.MergeDataForMap(false, dataList) res, err := oj.MergeDataForMap(false, dataList)
if nil != err { if nil != err {
return err return err
@ -193,7 +193,7 @@ func (oj *ownJSON) MergeDataForReceiver(receiver interface{}, dataList ...interf
// Author : go_developer@163.com<白茶清欢> // Author : go_developer@163.com<白茶清欢>
// //
// Date : 5:45 下午 2021/11/5 // Date : 5:45 下午 2021/11/5
func (oj *ownJSON) ConsoleOutput(data interface{}) { func (oj *ownJSON) ConsoleOutput(data any) {
var out bytes.Buffer var out bytes.Buffer
switch reflect.TypeOf(data).Kind() { switch reflect.TypeOf(data).Kind() {
case reflect.Slice: case reflect.Slice: