feat: driver additional items parse

refactor/fs
Noah Hsu 2022-06-08 16:20:58 +08:00
parent 677047c80b
commit ae755db2d2
6 changed files with 97 additions and 6 deletions

View File

@ -37,8 +37,8 @@ func (d *Driver) Drop(ctx context.Context) error {
return nil
}
func (d *Driver) GetAccount() model.Account {
return d.Account
func (d *Driver) GetAddition() driver.Additional {
return d.Addition
}
func (d *Driver) File(ctx context.Context, path string) (driver.FileInfo, error) {

View File

@ -3,7 +3,7 @@ package local
import "github.com/alist-org/alist/v3/internal/driver"
type Addition struct {
RootFolder string `json:"root_folder" type:"string" desc:"root folder path" default:"/"`
RootFolder string `json:"root_folder" type:"string" help:"root folder path" default:"/"`
}
var config = driver.Config{

View File

@ -1,7 +1,6 @@
package driver
type Additional interface {
}
type Additional interface{}
type Item struct {
Name string `json:"name"`
@ -9,5 +8,10 @@ type Item struct {
Default string `json:"default"`
Values string `json:"values"`
Required bool `json:"required"`
Desc string `json:"desc"`
Help string `json:"help"`
}
type Items struct {
Main []Item `json:"main"`
Additional []Item `json:"additional"`
}

View File

@ -19,6 +19,7 @@ type Meta interface {
Drop(ctx context.Context) error
// GetAccount transform additional field to string and assign to account's addition
GetAccount() model.Account
GetAddition() Additional
}
type Other interface {

View File

@ -2,13 +2,95 @@ package driver
import (
log "github.com/sirupsen/logrus"
"reflect"
)
type New func() Driver
var driversMap = map[string]New{}
var driverItemsMap = map[string]Items{}
func RegisterDriver(config Config, driver New) {
log.Infof("register driver: [%s]", config.Name)
registerDriverItems(config, driver().GetAddition())
driversMap[config.Name] = driver
}
func registerDriverItems(config Config, addition Additional) {
tAddition := reflect.TypeOf(addition)
mainItems := getMainItems(config)
additionalItems := getAdditionalItems(tAddition)
driverItemsMap[config.Name] = Items{mainItems, additionalItems}
}
func getMainItems(config Config) []Item {
items := []Item{{
Name: "virtual_path",
Type: "string",
Required: true,
Help: "",
}, {
Name: "index",
Type: "int",
Help: "use to sort",
}, {
Name: "down_proxy_url",
Type: "text",
}, {
Name: "webdav_direct",
Type: "bool",
Help: "Transfer the WebDAV of this account through the native without redirect",
}}
if !config.OnlyProxy && !config.OnlyLocal {
items = append(items, []Item{{
Name: "web_proxy",
Type: "bool",
}, {
Name: "webdav_proxy",
Type: "bool",
},
}...)
}
if config.LocalSort {
items = append(items, []Item{{
Name: "order_by",
Type: "select",
Values: "name,size,updated_at",
}, {
Name: "order_direction",
Type: "select",
Values: "ASC,DESC",
}}...)
}
items = append(items, Item{
Name: "extract_folder",
Values: "front,back",
Type: "select",
})
return items
}
func getAdditionalItems(t reflect.Type) []Item {
var items []Item
for i := 0; i < t.NumField(); i++ {
tag := t.Field(i).Tag
ignore, ok := tag.Lookup("ignore")
if !ok || ignore == "false" {
continue
}
item := Item{
Name: tag.Get("json"),
Type: tag.Get("type"),
Default: tag.Get("default"),
Values: tag.Get("values"),
Required: tag.Get("required") == "true",
Help: tag.Get("help"),
}
// set default type to string
if item.Type == "" {
item.Type = "string"
}
items = append(items, item)
}
return items
}

View File

@ -24,3 +24,7 @@ type Proxy struct {
WebdavDirect bool `json:"webdav_direct"`
DownProxyUrl string `json:"down_proxy_url"`
}
func (a Account) GetAccount() Account {
return a
}