feat: storage 添加内置实现的文件存储器,可以通过 storages 包进行使用

This commit is contained in:
kercylan98
2023-07-19 14:56:52 +08:00
parent f59354db3f
commit c447c8afb3
11 changed files with 360 additions and 76 deletions
+23
View File
@@ -0,0 +1,23 @@
package storages
import "encoding/json"
// FileStorageEncoder 全局数据文件存储编码器
type FileStorageEncoder[T any] func(data T) ([]byte, error)
// FileStorageDecoder 全局数据文件存储解码器
type FileStorageDecoder[T any] func(bytes []byte, data T) error
// FileStorageJSONEncoder JSON 编码器
func FileStorageJSONEncoder[T any]() FileStorageEncoder[T] {
return func(data T) ([]byte, error) {
return json.Marshal(data)
}
}
// FileStorageJSONDecoder JSON 解码器
func FileStorageJSONDecoder[T any]() FileStorageDecoder[T] {
return func(bytes []byte, data T) error {
return json.Unmarshal(bytes, data)
}
}