From ba648fa10c879aa1b54eabf4936bfc6a52444ba6 Mon Sep 17 00:00:00 2001 From: Noah Hsu Date: Wed, 8 Jun 2022 16:32:20 +0800 Subject: [PATCH] feat: get type from field's type --- drivers/local/meta.go | 2 +- internal/driver/addition.go | 9 +++++++++ internal/driver/manage.go | 9 +++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/drivers/local/meta.go b/drivers/local/meta.go index d3346417cec..a9a67606f35 100644 --- a/drivers/local/meta.go +++ b/drivers/local/meta.go @@ -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" help:"root folder path" default:"/"` + RootFolder string `json:"root_folder" help:"root folder path" default:"/"` } var config = driver.Config{ diff --git a/internal/driver/addition.go b/internal/driver/addition.go index 1356c8901e6..041dd0dd6dd 100644 --- a/internal/driver/addition.go +++ b/internal/driver/addition.go @@ -2,6 +2,15 @@ package driver type Additional interface{} +type Select string + +const ( + TypeString = "string" + TypeSelect = "select" + TypeBool = "bool" + TypeText = "text" +) + type Item struct { Name string `json:"name"` Type string `json:"type"` diff --git a/internal/driver/manage.go b/internal/driver/manage.go index ff7251ad9e0..2d971110385 100644 --- a/internal/driver/manage.go +++ b/internal/driver/manage.go @@ -3,6 +3,7 @@ package driver import ( log "github.com/sirupsen/logrus" "reflect" + "strings" ) type New func() Driver @@ -73,19 +74,23 @@ func getMainItems(config Config) []Item { func getAdditionalItems(t reflect.Type) []Item { var items []Item for i := 0; i < t.NumField(); i++ { - tag := t.Field(i).Tag + field := t.Field(i) + tag := field.Tag ignore, ok := tag.Lookup("ignore") if !ok || ignore == "false" { continue } item := Item{ Name: tag.Get("json"), - Type: tag.Get("type"), + Type: strings.ToLower(field.Name), Default: tag.Get("default"), Values: tag.Get("values"), Required: tag.Get("required") == "true", Help: tag.Get("help"), } + if tag.Get("type") != "" { + item.Type = tag.Get("type") + } // set default type to string if item.Type == "" { item.Type = "string"