From d2221e2ef97b29d1f430ec855974e2ac6bd69ebe Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Mon, 24 Jun 2024 11:41:23 +0200 Subject: [PATCH] feat(encoding): make format case-insensitive Signed-off-by: Mark Sagi-Kazar --- encoding.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/encoding.go b/encoding.go index be36c64..3e7950e 100644 --- a/encoding.go +++ b/encoding.go @@ -36,6 +36,8 @@ type Codec interface { // EncoderRegistry returns an [Encoder] for a given format. // +// Format is case-insensitive. +// // [EncoderRegistry] returns an error if no [Encoder] is registered for the format. type EncoderRegistry interface { Encoder(format string) (Encoder, error) @@ -43,6 +45,8 @@ type EncoderRegistry interface { // DecoderRegistry returns an [Decoder] for a given format. // +// Format is case-insensitive. +// // [DecoderRegistry] returns an error if no [Decoder] is registered for the format. type DecoderRegistry interface { 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) { - switch format { + switch strings.ToLower(format) { case "yaml", "yml": return yaml.Codec{}, true @@ -187,6 +191,8 @@ func (r *DefaultCodecRegistry) codec(format string) (Codec, bool) { r.mu.Lock() defer r.mu.Unlock() + format = strings.ToLower(format) + if r.codecs != nil { codec, ok := r.codecs[format] if ok {