feat: refactor storage retrieval functions and improve documentation
This commit is contained in:
@@ -11,7 +11,8 @@ import (
|
||||
var UserStorages = make(map[int64][]Storage)
|
||||
|
||||
// GetStorageByName returns storage by name from cache or creates new one
|
||||
func getStorageByName(ctx context.Context, name string) (Storage, error) {
|
||||
// It should NOT be used to get storage for user, use GetStorageByUserIDAndName instead
|
||||
func GetStorageByName(ctx context.Context, name string) (Storage, error) {
|
||||
if name == "" {
|
||||
return nil, ErrStorageNameEmpty
|
||||
}
|
||||
@@ -43,7 +44,7 @@ func GetStorageByUserIDAndName(ctx context.Context, chatID int64, name string) (
|
||||
return nil, fmt.Errorf("no storage %s for user %d", name, chatID)
|
||||
}
|
||||
|
||||
return getStorageByName(ctx, name)
|
||||
return GetStorageByName(ctx, name)
|
||||
}
|
||||
|
||||
func GetUserStorages(ctx context.Context, chatID int64) []Storage {
|
||||
@@ -55,7 +56,7 @@ func GetUserStorages(ctx context.Context, chatID int64) []Storage {
|
||||
}
|
||||
var storages []Storage
|
||||
for _, name := range config.C().GetStorageNamesByUserID(chatID) {
|
||||
storage, err := getStorageByName(ctx, name)
|
||||
storage, err := GetStorageByName(ctx, name)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
@@ -68,7 +69,7 @@ func LoadStorages(ctx context.Context) {
|
||||
logger := log.FromContext(ctx)
|
||||
logger.Info("加载存储...")
|
||||
for _, storage := range config.C().Storages {
|
||||
_, err := getStorageByName(ctx, storage.GetName())
|
||||
_, err := GetStorageByName(ctx, storage.GetName())
|
||||
if err != nil {
|
||||
logger.Errorf("加载存储 %s 失败: %v", storage.GetName(), err)
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import (
|
||||
)
|
||||
|
||||
type Storage interface {
|
||||
// Init 只应该在创建存储时调用一次
|
||||
Init(ctx context.Context, cfg storcfg.StorageConfig) error
|
||||
Type() storenum.StorageType
|
||||
Name() string
|
||||
@@ -42,6 +43,7 @@ var storageConstructors = map[storenum.StorageType]StorageConstructor{
|
||||
storenum.Telegram: func() Storage { return new(telegram.Telegram) },
|
||||
}
|
||||
|
||||
// NewStorage creates a new storage instance based on the provided config and initializes it
|
||||
func NewStorage(ctx context.Context, cfg storcfg.StorageConfig) (Storage, error) {
|
||||
constructor, ok := storageConstructors[cfg.GetType()]
|
||||
if !ok {
|
||||
|
||||
Reference in New Issue
Block a user