mirror of
https://github.com/spf13/viper
synced 2024-12-22 11:37:02 +00:00
feat: drop config type switch from marshaling
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
3c40652f77
commit
ebe913cc53
1 changed files with 27 additions and 25 deletions
52
viper.go
52
viper.go
|
@ -1643,17 +1643,20 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
|
||||||
buf := new(bytes.Buffer)
|
buf := new(bytes.Buffer)
|
||||||
buf.ReadFrom(in)
|
buf.ReadFrom(in)
|
||||||
|
|
||||||
switch format := strings.ToLower(v.getConfigType()); format {
|
format := strings.ToLower(v.getConfigType())
|
||||||
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "properties", "props", "prop", "dotenv", "env":
|
|
||||||
decoder, err := v.decoderRegistry.Decoder(format)
|
|
||||||
if err != nil {
|
|
||||||
return ConfigParseError{err}
|
|
||||||
}
|
|
||||||
|
|
||||||
err = decoder.Decode(buf.Bytes(), c)
|
if !stringInSlice(format, SupportedExts) {
|
||||||
if err != nil {
|
return UnsupportedConfigError(format)
|
||||||
return ConfigParseError{err}
|
}
|
||||||
}
|
|
||||||
|
decoder, err := v.decoderRegistry.Decoder(format)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
err = decoder.Decode(buf.Bytes(), c)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigParseError{err}
|
||||||
}
|
}
|
||||||
|
|
||||||
insensitiviseMap(c)
|
insensitiviseMap(c)
|
||||||
|
@ -1663,23 +1666,22 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
|
||||||
// Marshal a map into Writer.
|
// Marshal a map into Writer.
|
||||||
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 {
|
|
||||||
case "yaml", "yml", "json", "toml", "hcl", "tfvars", "ini", "prop", "props", "properties", "dotenv", "env":
|
|
||||||
encoder, err := v.encoderRegistry.Encoder(configType)
|
|
||||||
if err != nil {
|
|
||||||
return ConfigMarshalError{err}
|
|
||||||
}
|
|
||||||
|
|
||||||
b, err := encoder.Encode(c)
|
encoder, err := v.encoderRegistry.Encoder(configType)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return ConfigMarshalError{err}
|
return ConfigMarshalError{err}
|
||||||
}
|
|
||||||
|
|
||||||
_, err = f.WriteString(string(b))
|
|
||||||
if err != nil {
|
|
||||||
return ConfigMarshalError{err}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
b, err := encoder.Encode(c)
|
||||||
|
if err != nil {
|
||||||
|
return ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = f.WriteString(string(b))
|
||||||
|
if err != nil {
|
||||||
|
return ConfigMarshalError{err}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue