Skip to content

Commit

Permalink
refactor: 更新BackupProviderConfig的Config字段类型为json.RawMessage,并重命名转换函数
Browse files Browse the repository at this point in the history
  • Loading branch information
TBXark committed Jan 24, 2025
1 parent a3aa0b2 commit 2f6bb38
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 27 deletions.
14 changes: 5 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (

type BackupProviderConfig struct {
Type BackupProviderConfigType `json:"type"`
Config any `json:"config"`
Config json.RawMessage `json:"config"`
}

type DefaultConfig struct {
Expand Down Expand Up @@ -95,15 +95,11 @@ type SyncConfig struct {
Cron string `json:"cron"`
}

func ConvertToBackupProviderConfig[T any](raw any) (*T, error) {
b, err := json.Marshal(raw)
func Convert[T any](raw json.RawMessage) (*T, error) {
conf := new(T)
err := json.Unmarshal(raw, &conf)
if err != nil {
return nil, err
}
var conf T
err = json.Unmarshal(b, &conf)
if err != nil {
return nil, err
}
return &conf, nil
return conf, nil
}
18 changes: 1 addition & 17 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package config
import (
"encoding/json"
"fmt"
"github.com/TBXark/github-backup/provider/file"
"github.com/TBXark/github-backup/provider/gitea"
"testing"
)
Expand Down Expand Up @@ -37,22 +36,7 @@ func TestSyncConfig(t *testing.T) {
"[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+/0/[01]/[01]": "PUBLIC_GITHUB_TOKEN",
},
},
Targets: []GithubConfig{
{
Owner: "GITHUB_OWNER",
Token: "GITHUB_TOKEN",
RepoOwner: "BACKUP_TARGET_REPO_OWNER",
Backup: &BackupProviderConfig{
Type: "file",
Config: &file.FileBackupConfig{
Dir: "SAVE_DIR",
History: "FILE_HISTORY_JSON_PATH",
},
},
Filter: &FilterConfig{
UnmatchedRepoAction: UnmatchedRepoActionIgnore,
},
},
Targets: []*GithubConfig{
{
Owner: "GITHUB_ORG",
RepoOwner: "BACKUP_TARGET_REPO_ORG",
Expand Down
2 changes: 1 addition & 1 deletion task.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider, error) {
switch conf.Type {
case config.BackupProviderConfigTypeGitea:
c, err := config.ConvertToBackupProviderConfig[gitea.Config](conf.Config)
c, err := config.Convert[gitea.Config](conf.Config)
if err != nil {
return nil, err
}
Expand Down

0 comments on commit 2f6bb38

Please sign in to comment.