cmd/providers: add DefaultStr, ValueStr and Type fields

These fields are auto generated:
- DefaultStr - a string rendering of Default
- ValueStr - a string rendering of Value
- Type - the type of the option
This commit is contained in:
Nick Craig-Wood 2019-06-03 15:21:05 +01:00
parent e2b6172f7d
commit ac4c8d8dfc

View file

@ -2,6 +2,7 @@
package fs
import (
"encoding/json"
"fmt"
"io"
"io/ioutil"
@ -132,6 +133,29 @@ type Option struct {
Advanced bool // set if this is an advanced config option
}
// BaseOption is an alias for Option used internally
type BaseOption Option
// MarshalJSON turns an Option into JSON
//
// It adds some generated fields for ease of use
// - DefaultStr - a string rendering of Default
// - ValueStr - a string rendering of Value
// - Type - the type of the option
func (o *Option) MarshalJSON() ([]byte, error) {
return json.Marshal(struct {
BaseOption
DefaultStr string
ValueStr string
Type string
}{
BaseOption: BaseOption(*o),
DefaultStr: fmt.Sprint(o.Default),
ValueStr: o.String(),
Type: o.Type(),
})
}
// GetValue gets the current current value which is the default if not set
func (o *Option) GetValue() interface{} {
val := o.Value