docs(encoding): add docs to codec registry

Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
Mark Sagi-Kazar 2024-06-24 11:53:35 +02:00 committed by Márk Sági-Kazár
parent 8492c8d451
commit e033c8e8dc

View file

@ -61,22 +61,22 @@ type CodecRegistry interface {
// WithEncoderRegistry sets a custom [EncoderRegistry]. // WithEncoderRegistry sets a custom [EncoderRegistry].
func WithEncoderRegistry(r EncoderRegistry) Option { func WithEncoderRegistry(r EncoderRegistry) Option {
return optionFunc(func(v *Viper) { return optionFunc(func(v *Viper) {
v.encoderRegistry2 = r v.encoderRegistry = r
}) })
} }
// WithDecoderRegistry sets a custom [DecoderRegistry]. // WithDecoderRegistry sets a custom [DecoderRegistry].
func WithDecoderRegistry(r DecoderRegistry) Option { func WithDecoderRegistry(r DecoderRegistry) Option {
return optionFunc(func(v *Viper) { return optionFunc(func(v *Viper) {
v.decoderRegistry2 = r v.decoderRegistry = r
}) })
} }
// WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry]. // WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry].
func WithCodecRegistry(r CodecRegistry) Option { func WithCodecRegistry(r CodecRegistry) Option {
return optionFunc(func(v *Viper) { return optionFunc(func(v *Viper) {
v.encoderRegistry2 = r v.encoderRegistry = r
v.decoderRegistry2 = r v.decoderRegistry = r
}) })
} }
@ -134,7 +134,7 @@ func (r codecRegistry) codec(format string) (Codec, bool) {
return nil, false return nil, false
} }
// DefaultCodecRegistry // DefaultCodecRegistry is a simple implementation of [CodecRegistry] that allows registering custom [Codec]s.
type DefaultCodecRegistry struct { type DefaultCodecRegistry struct {
codecs map[string]Codec codecs map[string]Codec
@ -158,6 +158,8 @@ func (r *DefaultCodecRegistry) init() {
} }
// RegisterCodec registers a custom [Codec]. // RegisterCodec registers a custom [Codec].
//
// Format is case-insensitive.
func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error { func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error {
r.init() r.init()
@ -169,6 +171,9 @@ func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error {
return nil return nil
} }
// Encoder implements the [EncoderRegistry] interface.
//
// Format is case-insensitive.
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 {
@ -178,6 +183,9 @@ func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
return encoder, nil return encoder, nil
} }
// Decoder implements the [DecoderRegistry] interface.
//
// Format is case-insensitive.
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 {