fs: allow []string to work in Options

This commit is contained in:
Nick Craig-Wood 2024-07-02 10:37:58 +01:00
parent c6ab37a59f
commit 8e10fe71f7

View File

@ -215,12 +215,40 @@ func (o *Option) GetValue() interface{} {
// String turns Option into a string
func (o *Option) String() string {
return fmt.Sprint(o.GetValue())
v := o.GetValue()
if stringArray, isStringArray := v.([]string); isStringArray {
// Treat empty string array as empty string
// This is to make the default value of the option help nice
if len(stringArray) == 0 {
return ""
}
// Encode string arrays as JSON
// The default Go encoding can't be decoded uniquely
buf, err := json.Marshal(stringArray)
if err != nil {
Errorf(nil, "Can't encode default value for %q key - ignoring: %v", o.Name, err)
return "[]"
}
return string(buf)
}
return fmt.Sprint(v)
}
// Set an Option from a string
func (o *Option) Set(s string) (err error) {
newValue, err := configstruct.StringToInterface(o.GetValue(), s)
v := o.GetValue()
if stringArray, isStringArray := v.([]string); isStringArray {
if stringArray == nil {
stringArray = []string{}
}
// If this is still the default value then overwrite the defaults
if reflect.ValueOf(o.Default).Pointer() == reflect.ValueOf(v).Pointer() {
stringArray = []string{}
}
o.Value = append(stringArray, s)
return nil
}
newValue, err := configstruct.StringToInterface(v, s)
if err != nil {
return err
}
@ -241,6 +269,11 @@ func (o *Option) Type() string {
return do.Type()
}
// Special case []string
if _, isStringArray := v.([]string); isStringArray {
return "stringArray"
}
return reflect.TypeOf(v).Name()
}