configstruct: allow parsing of []string encoded as JSON
This commit is contained in:
parent
1a77a2f92b
commit
3280b6b83c
1 changed files with 21 additions and 0 deletions
|
@ -2,6 +2,7 @@
|
||||||
package configstruct
|
package configstruct
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
@ -24,6 +25,16 @@ func camelToSnake(in string) string {
|
||||||
}
|
}
|
||||||
|
|
||||||
// StringToInterface turns in into an interface{} the same type as def
|
// StringToInterface turns in into an interface{} the same type as def
|
||||||
|
//
|
||||||
|
// This supports a subset of builtin types, string, integer types,
|
||||||
|
// bool, time.Duration and []string.
|
||||||
|
//
|
||||||
|
// Builtin types are expected to be encoding as their natural
|
||||||
|
// stringificatons as produced by fmt.Sprint except for []string which
|
||||||
|
// is expected to be encoded as JSON with empty array encoded as "".
|
||||||
|
//
|
||||||
|
// Any other types are expected to be encoded by their String()
|
||||||
|
// methods and decoded by their `Set(s string) error` methods.
|
||||||
func StringToInterface(def interface{}, in string) (newValue interface{}, err error) {
|
func StringToInterface(def interface{}, in string) (newValue interface{}, err error) {
|
||||||
typ := reflect.TypeOf(def)
|
typ := reflect.TypeOf(def)
|
||||||
o := reflect.New(typ)
|
o := reflect.New(typ)
|
||||||
|
@ -46,6 +57,16 @@ func StringToInterface(def interface{}, in string) (newValue interface{}, err er
|
||||||
newValue, err = strconv.ParseBool(in)
|
newValue, err = strconv.ParseBool(in)
|
||||||
case time.Duration:
|
case time.Duration:
|
||||||
newValue, err = time.ParseDuration(in)
|
newValue, err = time.ParseDuration(in)
|
||||||
|
case []string:
|
||||||
|
// JSON decode arrays of strings
|
||||||
|
if in != "" {
|
||||||
|
var out []string
|
||||||
|
err = json.Unmarshal([]byte(in), &out)
|
||||||
|
newValue = out
|
||||||
|
} else {
|
||||||
|
// Empty string we will treat as empty array
|
||||||
|
newValue = []string{}
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
// Try using a Set method
|
// Try using a Set method
|
||||||
if do, ok := o.Interface().(interface{ Set(s string) error }); ok {
|
if do, ok := o.Interface().(interface{ Set(s string) error }); ok {
|
||||||
|
|
Loading…
Add table
Reference in a new issue