Skip to content

Commit

Permalink
docs(encoding): add docs to codec registry
Browse files Browse the repository at this point in the history
Signed-off-by: Mark Sagi-Kazar <[email protected]>
  • Loading branch information
sagikazarmark committed Jun 24, 2024
1 parent 8492c8d commit e033c8e
Showing 1 changed file with 13 additions and 5 deletions.
18 changes: 13 additions & 5 deletions encoding.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,22 @@ type CodecRegistry interface {
// WithEncoderRegistry sets a custom [EncoderRegistry].
func WithEncoderRegistry(r EncoderRegistry) Option {
return optionFunc(func(v *Viper) {
v.encoderRegistry2 = r
v.encoderRegistry = r
})
}

// WithDecoderRegistry sets a custom [DecoderRegistry].
func WithDecoderRegistry(r DecoderRegistry) Option {
return optionFunc(func(v *Viper) {
v.decoderRegistry2 = r
v.decoderRegistry = r
})
}

// WithCodecRegistry sets a custom [EncoderRegistry] and [DecoderRegistry].
func WithCodecRegistry(r CodecRegistry) Option {
return optionFunc(func(v *Viper) {
v.encoderRegistry2 = r
v.decoderRegistry2 = r
v.encoderRegistry = r
v.decoderRegistry = r
})
}

Expand Down Expand Up @@ -134,7 +134,7 @@ func (r codecRegistry) codec(format string) (Codec, bool) {
return nil, false
}

// DefaultCodecRegistry
// DefaultCodecRegistry is a simple implementation of [CodecRegistry] that allows registering custom [Codec]s.
type DefaultCodecRegistry struct {
codecs map[string]Codec

Expand All @@ -158,6 +158,8 @@ func (r *DefaultCodecRegistry) init() {
}

// RegisterCodec registers a custom [Codec].
//
// Format is case-insensitive.
func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error {
r.init()

Expand All @@ -169,6 +171,9 @@ func (r *DefaultCodecRegistry) RegisterCodec(format string, codec Codec) error {
return nil
}

// Encoder implements the [EncoderRegistry] interface.
//
// Format is case-insensitive.
func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
encoder, ok := r.codec(format)
if !ok {
Expand All @@ -178,6 +183,9 @@ func (r *DefaultCodecRegistry) Encoder(format string) (Encoder, error) {
return encoder, nil
}

// Decoder implements the [DecoderRegistry] interface.
//
// Format is case-insensitive.
func (r *DefaultCodecRegistry) Decoder(format string) (Decoder, error) {
decoder, ok := r.codec(format)
if !ok {
Expand Down

0 comments on commit e033c8e

Please sign in to comment.