Skip to content

Commit 2f6bb38

Browse files
committed
refactor: 更新BackupProviderConfig的Config字段类型为json.RawMessage,并重命名转换函数
1 parent a3aa0b2 commit 2f6bb38

File tree

3 files changed

+7
-27
lines changed

3 files changed

+7
-27
lines changed

config/config.go

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020

2121
type BackupProviderConfig struct {
2222
Type BackupProviderConfigType `json:"type"`
23-
Config any `json:"config"`
23+
Config json.RawMessage `json:"config"`
2424
}
2525

2626
type DefaultConfig struct {
@@ -95,15 +95,11 @@ type SyncConfig struct {
9595
Cron string `json:"cron"`
9696
}
9797

98-
func ConvertToBackupProviderConfig[T any](raw any) (*T, error) {
99-
b, err := json.Marshal(raw)
98+
func Convert[T any](raw json.RawMessage) (*T, error) {
99+
conf := new(T)
100+
err := json.Unmarshal(raw, &conf)
100101
if err != nil {
101102
return nil, err
102103
}
103-
var conf T
104-
err = json.Unmarshal(b, &conf)
105-
if err != nil {
106-
return nil, err
107-
}
108-
return &conf, nil
104+
return conf, nil
109105
}

config/config_test.go

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package config
33
import (
44
"encoding/json"
55
"fmt"
6-
"github.com/TBXark/github-backup/provider/file"
76
"github.com/TBXark/github-backup/provider/gitea"
87
"testing"
98
)
@@ -37,22 +36,7 @@ func TestSyncConfig(t *testing.T) {
3736
"[a-zA-Z0-9._-]+/[a-zA-Z0-9._-]+/0/[01]/[01]": "PUBLIC_GITHUB_TOKEN",
3837
},
3938
},
40-
Targets: []GithubConfig{
41-
{
42-
Owner: "GITHUB_OWNER",
43-
Token: "GITHUB_TOKEN",
44-
RepoOwner: "BACKUP_TARGET_REPO_OWNER",
45-
Backup: &BackupProviderConfig{
46-
Type: "file",
47-
Config: &file.FileBackupConfig{
48-
Dir: "SAVE_DIR",
49-
History: "FILE_HISTORY_JSON_PATH",
50-
},
51-
},
52-
Filter: &FilterConfig{
53-
UnmatchedRepoAction: UnmatchedRepoActionIgnore,
54-
},
55-
},
39+
Targets: []*GithubConfig{
5640
{
5741
Owner: "GITHUB_ORG",
5842
RepoOwner: "BACKUP_TARGET_REPO_ORG",

task.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func BuildBackupProvider(conf *config.BackupProviderConfig) (provider.Provider, error) {
1414
switch conf.Type {
1515
case config.BackupProviderConfigTypeGitea:
16-
c, err := config.ConvertToBackupProviderConfig[gitea.Config](conf.Config)
16+
c, err := config.Convert[gitea.Config](conf.Config)
1717
if err != nil {
1818
return nil, err
1919
}

0 commit comments

Comments
 (0)