From ac4c8d8dfcd949c11ab300569b4e99c7313d1293 Mon Sep 17 00:00:00 2001 From: Nick Craig-Wood Date: Mon, 3 Jun 2019 15:21:05 +0100 Subject: [PATCH] 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 --- fs/fs.go | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/fs/fs.go b/fs/fs.go index 1617cf9ee..ad412f1c7 100644 --- a/fs/fs.go +++ b/fs/fs.go @@ -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