feat(encoding): integrate ini codec into Viper

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2021-07-16 03:14:41 +02:00 committed by Márk Sági-Kazár
parent dd62da434f
commit 430936044e
1 changed files with 13 additions and 36 deletions

View File

@ -41,10 +41,10 @@ import (
"github.com/spf13/cast" "github.com/spf13/cast"
"github.com/spf13/pflag" "github.com/spf13/pflag"
"github.com/subosito/gotenv" "github.com/subosito/gotenv"
"gopkg.in/ini.v1"
"github.com/spf13/viper/internal/encoding" "github.com/spf13/viper/internal/encoding"
"github.com/spf13/viper/internal/encoding/hcl" "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/json"
"github.com/spf13/viper/internal/encoding/toml" "github.com/spf13/viper/internal/encoding/toml"
"github.com/spf13/viper/internal/encoding/yaml" "github.com/spf13/viper/internal/encoding/yaml"
@ -346,6 +346,16 @@ func (v *Viper) resetEncoding() {
decoderRegistry.RegisterDecoder("tfvars", codec) 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.encoderRegistry = encoderRegistry
v.decoderRegistry = decoderRegistry v.decoderRegistry = decoderRegistry
} }
@ -1646,7 +1656,7 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
buf.ReadFrom(in) buf.ReadFrom(in)
switch format := strings.ToLower(v.getConfigType()); format { 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) err := v.decoderRegistry.Decode(format, buf.Bytes(), c)
if err != nil { if err != nil {
return ConfigParseError{err} return ConfigParseError{err}
@ -1676,23 +1686,6 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]interface{}) error {
// set innermost value // set innermost value
deepestMap[lastKey] = 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) 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 { func (v *Viper) marshalWriter(f afero.File, configType string) error {
c := v.AllSettings() c := v.AllSettings()
switch configType { switch configType {
case "yaml", "yml", "json", "toml", "hcl", "tfvars": case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini":
b, err := v.encoderRegistry.Encode(configType, c) b, err := v.encoderRegistry.Encode(configType, c)
if err != nil { if err != nil {
return ConfigMarshalError{err} return ConfigMarshalError{err}
@ -1741,22 +1734,6 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
if _, err := f.WriteString(s); err != nil { if _, err := f.WriteString(s); err != nil {
return ConfigMarshalError{err} 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 return nil
} }