feat(encoding): drop not found sentinel errors for now

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

View file

@ -1,6 +1,7 @@
package viper package viper
import ( import (
"errors"
"strings" "strings"
"sync" "sync"
@ -31,30 +32,18 @@ type Codec interface {
Decoder Decoder
} }
type encodingError string // TODO: consider adding specific errors for not found scenarios
func (e encodingError) Error() string {
return string(e)
}
const (
// ErrEncoderNotFound is returned when there is no encoder registered for a format.
ErrEncoderNotFound = encodingError("encoder not found for this format")
// ErrDecoderNotFound is returned when there is no decoder registered for a format.
ErrDecoderNotFound = encodingError("decoder not found for this format")
)
// EncoderRegistry returns an [Encoder] for a given format. // EncoderRegistry returns an [Encoder] for a given format.
// //
// The error is [ErrEncoderNotFound] 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)
} }
// DecoderRegistry returns an [Decoder] for a given format. // DecoderRegistry returns an [Decoder] for a given format.
// //
// The error is [ErrDecoderNotFound] 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)
} }
@ -94,7 +83,7 @@ type codecRegistry struct {
func (r codecRegistry) Encoder(format string) (Encoder, error) { func (r codecRegistry) Encoder(format string) (Encoder, error) {
encoder, ok := r.codec(format) encoder, ok := r.codec(format)
if !ok { if !ok {
return nil, ErrEncoderNotFound return nil, errors.New("encoder not found for this format")
} }
return encoder, nil return encoder, nil
@ -103,7 +92,7 @@ func (r codecRegistry) Encoder(format string) (Encoder, error) {
func (r codecRegistry) Decoder(format string) (Decoder, error) { func (r codecRegistry) Decoder(format string) (Decoder, error) {
decoder, ok := r.codec(format) decoder, ok := r.codec(format)
if !ok { if !ok {
return nil, ErrDecoderNotFound return nil, errors.New("decoder not found for this format")
} }
return decoder, nil return decoder, nil
@ -179,7 +168,7 @@ func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error {
func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) { func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
encoder, ok := r.codec(format) encoder, ok := r.codec(format)
if !ok { if !ok {
return nil, ErrEncoderNotFound return nil, errors.New("encoder not found for this format")
} }
return encoder, nil return encoder, nil
@ -188,7 +177,7 @@ func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) { func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) {
decoder, ok := r.codec(format) decoder, ok := r.codec(format)
if !ok { if !ok {
return nil, ErrDecoderNotFound return nil, errors.New("decoder not found for this format")
} }
return decoder, nil return decoder, nil