From 430936044e0e1b5f093b2c5681c6eb53a6caff86 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Fri, 16 Jul 2021 03:14:41 +0200 Subject: [PATCH] feat(encoding): integrate ini codec into Viper Signed-off-by: Mark Sagi-Kazar --- viper.go | 49 +++++++++++++------------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) diff --git a/viper.go b/viper.go index 5a243bd..eb848f3 100644 --- a/viper.go +++ b/viper.go @@ -41,10 +41,10 @@ import ( "github.com/spf13/cast" "github.com/spf13/pflag" "github.com/subosito/gotenv" - "gopkg.in/ini.v1" "github.com/spf13/viper/internal/encoding" "github.com/spf13/viper/internal/encoding/hcl" + "github.com/spf13/viper/internal/encoding/ini" "github.com/spf13/viper/internal/encoding/json" "github.com/spf13/viper/internal/encoding/toml" "github.com/spf13/viper/internal/encoding/yaml" @@ -346,6 +346,16 @@ func (v *Viper) resetEncoding() { decoderRegistry.RegisterDecoder("tfvars", codec) } + { + codec := ini.Codec{ + KeyDelimiter: v.keyDelim, + LoadOptions: v.iniLoadOptions, + } + + encoderRegistry.RegisterEncoder("ini", codec) + decoderRegistry.RegisterDecoder("ini", codec) + } + v.encoderRegistry = encoderRegistry v.decoderRegistry = decoderRegistry } @@ -1646,7 +1656,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { buf.ReadFrom(in) switch format := strings.ToLower(v.getConfigType()); format { - case "yaml", "yml", "json", "toml", "hcl", "tfvars": + case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini": err := v.decoderRegistry.Decode(format, buf.Bytes(), c) if err != nil { return ConfigParseError{err} @@ -1676,23 +1686,6 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { // set innermost value deepestMap[lastKey] = value } - - case "ini": - cfg := ini.Empty(v.iniLoadOptions) - err := cfg.Append(buf.Bytes()) - if err != nil { - return ConfigParseError{err} - } - sections := cfg.Sections() - for i := 0; i < len(sections); i++ { - section := sections[i] - keys := section.Keys() - for j := 0; j < len(keys); j++ { - key := keys[j] - value := cfg.Section(section.Name()).Key(key.Name()).String() - c[section.Name()+"."+key.Name()] = value - } - } } insensitiviseMap(c) @@ -1703,7 +1696,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error { func (v *Viper) marshalWriter(f afero.File, configType string) error { c := v.AllSettings() switch configType { - case "yaml", "yml", "json", "toml", "hcl", "tfvars": + case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini": b, err := v.encoderRegistry.Encode(configType, c) if err != nil { return ConfigMarshalError{err} @@ -1741,22 +1734,6 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error { if _, err := f.WriteString(s); err != nil { return ConfigMarshalError{err} } - - case "ini": - keys := v.AllKeys() - cfg := ini.Empty() - ini.PrettyFormat = false - for i := 0; i < len(keys); i++ { - key := keys[i] - lastSep := strings.LastIndex(key, ".") - sectionName := key[:(lastSep)] - keyName := key[(lastSep + 1):] - if sectionName == "default" { - sectionName = "" - } - cfg.Section(sectionName).Key(keyName).SetValue(v.GetString(key)) - } - cfg.WriteTo(f) } return nil }