feat(encoding): make format case-insensitive

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-24 11:41:23 +02:00 committed by Márk Sági-Kazár
parent 93bf64aaa3
commit d2221e2ef9

View file

@ -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 {