mirror of
https://github.com/spf13/viper
synced 2024-11-16 18:07:02 +00:00
feat(encoding): make format case-insensitive
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
93bf64aaa3
commit
d2221e2ef9
1 changed files with 7 additions and 1 deletions
|
@ -36,6 +36,8 @@ type Codec interface {
|
||||||
|
|
||||||
// EncoderRegistry returns an [Encoder] for a given format.
|
// EncoderRegistry returns an [Encoder] for a given format.
|
||||||
//
|
//
|
||||||
|
// Format is case-insensitive.
|
||||||
|
//
|
||||||
// [EncoderRegistry] returns an error if no [Encoder] is registered for the format.
|
// [EncoderRegistry] returns an error if no [Encoder] is registered for the format.
|
||||||
type EncoderRegistry interface {
|
type EncoderRegistry interface {
|
||||||
Encoder(format string) (Encoder, error)
|
Encoder(format string) (Encoder, error)
|
||||||
|
@ -43,6 +45,8 @@ type EncoderRegistry interface {
|
||||||
|
|
||||||
// DecoderRegistry returns an [Decoder] for a given format.
|
// DecoderRegistry returns an [Decoder] for a given format.
|
||||||
//
|
//
|
||||||
|
// Format is case-insensitive.
|
||||||
|
//
|
||||||
// [DecoderRegistry] returns an error if no [Decoder] is registered for the format.
|
// [DecoderRegistry] returns an error if no [Decoder] is registered for the format.
|
||||||
type DecoderRegistry interface {
|
type DecoderRegistry interface {
|
||||||
Decoder(format string) (Decoder, error)
|
Decoder(format string) (Decoder, error)
|
||||||
|
@ -99,7 +103,7 @@ func (r codecRegistry) Decoder(format string) (Decoder, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (r codecRegistry) codec(format string) (Codec, bool) {
|
func (r codecRegistry) codec(format string) (Codec, bool) {
|
||||||
switch format {
|
switch strings.ToLower(format) {
|
||||||
case "yaml", "yml":
|
case "yaml", "yml":
|
||||||
return yaml.Codec{}, true
|
return yaml.Codec{}, true
|
||||||
|
|
||||||
|
@ -187,6 +191,8 @@ func (r *DefaultCodecRegistry) codec(format string) (Codec, bool) {
|
||||||
r.mu.Lock()
|
r.mu.Lock()
|
||||||
defer r.mu.Unlock()
|
defer r.mu.Unlock()
|
||||||
|
|
||||||
|
format = strings.ToLower(format)
|
||||||
|
|
||||||
if r.codecs != nil {
|
if r.codecs != nil {
|
||||||
codec, ok := r.codecs[format]
|
codec, ok := r.codecs[format]
|
||||||
if ok {
|
if ok {
|
||||||
|
|
Loading…
Reference in a new issue