feat: drop config type switch from marshaling

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-07-20 15:31:27 +02:00 committed by Márk Sági-Kazár
parent 3c40652f77
commit ebe913cc53

View file

@ -1643,8 +1643,12 @@ 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":
if !stringInSlice(format, SupportedExts) {
return UnsupportedConfigError(format)
}
decoder, err := v.decoderRegistry.Decoder(format) decoder, err := v.decoderRegistry.Decoder(format)
if err != nil { if err != nil {
return ConfigParseError{err} return ConfigParseError{err}
@ -1654,7 +1658,6 @@ func (v *Viper) unmarshalReader(in io.Reader, c map[string]any) error {
if err != nil { if err != nil {
return ConfigParseError{err} return ConfigParseError{err}
} }
}
insensitiviseMap(c) insensitiviseMap(c)
return nil return nil
@ -1663,8 +1666,7 @@ 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) encoder, err := v.encoderRegistry.Encoder(configType)
if err != nil { if err != nil {
return ConfigMarshalError{err} return ConfigMarshalError{err}
@ -1679,7 +1681,7 @@ func (v *Viper) marshalWriter(f afero.File, configType string) error {
if err != nil { if err != nil {
return ConfigMarshalError{err} return ConfigMarshalError{err}
} }
}
return nil return nil
} }