From 94e8f72a4cccc84726ace882ea71e1c7a0b2b34c Mon Sep 17 00:00:00 2001 From: Cameron Moore Date: Mon, 26 Sep 2016 21:16:45 -0500 Subject: [PATCH] Properly handle string slice values This commit attempts to properly retrieve string slice values. To support that effort, I've copied over the readAsCSV() func from pflag, which adds a dependency on encoding/csv. --- viper.go | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/viper.go b/viper.go index 4ed2d40..01d25cf 100644 --- a/viper.go +++ b/viper.go @@ -21,6 +21,7 @@ package viper import ( "bytes" + "encoding/csv" "fmt" "io" "log" @@ -879,8 +880,13 @@ func (v *Viper) find(lcaseKey string) interface{} { case "bool": return cast.ToBool(flag.ValueString()) case "stringSlice": - s := strings.TrimPrefix(flag.ValueString(), "[") - return strings.TrimSuffix(s, "]") + s := flag.ValueString() + s = s[1 : len(s)-1] // trim [] + if len(s) == 0 { + return []string{} + } + res, _ := readAsCSV(s) + return res default: return flag.ValueString() } @@ -1503,6 +1509,15 @@ func (v *Viper) findConfigFile() (string, error) { return "", ConfigFileNotFoundError{v.configName, fmt.Sprintf("%s", v.configPaths)} } +func readAsCSV(val string) ([]string, error) { + if val == "" { + return []string{}, nil + } + stringReader := strings.NewReader(val) + csvReader := csv.NewReader(stringReader) + return csvReader.Read() +} + // Debug prints all configuration registries for debugging // purposes. func Debug() { v.Debug() }