mirror of https://github.com/spf13/viper.git
refactor: cleanup unused encoding code
Signed-off-by: Mark Sagi-Kazar <mark.sagikazar@gmail.com>
This commit is contained in:
parent
852d126bfa
commit
e2ab48ad87
|
@ -1,61 +0,0 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Decoder decodes the contents of b into v.
|
||||
// It's primarily used for decoding contents of a file into a map[string]any.
|
||||
type Decoder interface {
|
||||
Decode(b []byte, v map[string]any) error
|
||||
}
|
||||
|
||||
const (
|
||||
// ErrDecoderNotFound is returned when there is no decoder registered for a format.
|
||||
ErrDecoderNotFound = encodingError("decoder not found for this format")
|
||||
|
||||
// ErrDecoderFormatAlreadyRegistered is returned when an decoder is already registered for a format.
|
||||
ErrDecoderFormatAlreadyRegistered = encodingError("decoder already registered for this format")
|
||||
)
|
||||
|
||||
// DecoderRegistry can choose an appropriate Decoder based on the provided format.
|
||||
type DecoderRegistry struct {
|
||||
decoders map[string]Decoder
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewDecoderRegistry returns a new, initialized DecoderRegistry.
|
||||
func NewDecoderRegistry() *DecoderRegistry {
|
||||
return &DecoderRegistry{
|
||||
decoders: make(map[string]Decoder),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterDecoder registers a Decoder for a format.
|
||||
// Registering a Decoder for an already existing format is not supported.
|
||||
func (e *DecoderRegistry) RegisterDecoder(format string, enc Decoder) error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if _, ok := e.decoders[format]; ok {
|
||||
return ErrDecoderFormatAlreadyRegistered
|
||||
}
|
||||
|
||||
e.decoders[format] = enc
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Decode calls the underlying Decoder based on the format.
|
||||
func (e *DecoderRegistry) Decode(format string, b []byte, v map[string]any) error {
|
||||
e.mu.RLock()
|
||||
decoder, ok := e.decoders[format]
|
||||
e.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return ErrDecoderNotFound
|
||||
}
|
||||
|
||||
return decoder.Decode(b, v)
|
||||
}
|
|
@ -1,69 +0,0 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type decoder struct {
|
||||
v map[string]any
|
||||
}
|
||||
|
||||
func (d decoder) Decode(_ []byte, v map[string]any) error {
|
||||
for key, value := range d.v {
|
||||
v[key] = value
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func TestDecoderRegistry_RegisterDecoder(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("AlreadyRegistered", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder{})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = registry.RegisterDecoder("myformat", decoder{})
|
||||
assert.ErrorIs(t, err, ErrDecoderFormatAlreadyRegistered)
|
||||
})
|
||||
}
|
||||
|
||||
func TestDecoderRegistry_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
decoder := decoder{
|
||||
v: map[string]any{
|
||||
"key": "value",
|
||||
},
|
||||
}
|
||||
|
||||
err := registry.RegisterDecoder("myformat", decoder)
|
||||
require.NoError(t, err)
|
||||
|
||||
v := map[string]any{}
|
||||
|
||||
err = registry.Decode("myformat", []byte("key: value"), v)
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, decoder.v, v)
|
||||
})
|
||||
|
||||
t.Run("DecoderNotFound", func(t *testing.T) {
|
||||
registry := NewDecoderRegistry()
|
||||
|
||||
v := map[string]any{}
|
||||
|
||||
err := registry.Decode("myformat", nil, v)
|
||||
assert.ErrorIs(t, err, ErrDecoderNotFound)
|
||||
})
|
||||
}
|
|
@ -1,60 +0,0 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"sync"
|
||||
)
|
||||
|
||||
// Encoder encodes the contents of v into a byte representation.
|
||||
// It's primarily used for encoding a map[string]any into a file format.
|
||||
type Encoder interface {
|
||||
Encode(v map[string]any) ([]byte, error)
|
||||
}
|
||||
|
||||
const (
|
||||
// ErrEncoderNotFound is returned when there is no encoder registered for a format.
|
||||
ErrEncoderNotFound = encodingError("encoder not found for this format")
|
||||
|
||||
// ErrEncoderFormatAlreadyRegistered is returned when an encoder is already registered for a format.
|
||||
ErrEncoderFormatAlreadyRegistered = encodingError("encoder already registered for this format")
|
||||
)
|
||||
|
||||
// EncoderRegistry can choose an appropriate Encoder based on the provided format.
|
||||
type EncoderRegistry struct {
|
||||
encoders map[string]Encoder
|
||||
|
||||
mu sync.RWMutex
|
||||
}
|
||||
|
||||
// NewEncoderRegistry returns a new, initialized EncoderRegistry.
|
||||
func NewEncoderRegistry() *EncoderRegistry {
|
||||
return &EncoderRegistry{
|
||||
encoders: make(map[string]Encoder),
|
||||
}
|
||||
}
|
||||
|
||||
// RegisterEncoder registers an Encoder for a format.
|
||||
// Registering a Encoder for an already existing format is not supported.
|
||||
func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error {
|
||||
e.mu.Lock()
|
||||
defer e.mu.Unlock()
|
||||
|
||||
if _, ok := e.encoders[format]; ok {
|
||||
return ErrEncoderFormatAlreadyRegistered
|
||||
}
|
||||
|
||||
e.encoders[format] = enc
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (e *EncoderRegistry) Encode(format string, v map[string]any) ([]byte, error) {
|
||||
e.mu.RLock()
|
||||
encoder, ok := e.encoders[format]
|
||||
e.mu.RUnlock()
|
||||
|
||||
if !ok {
|
||||
return nil, ErrEncoderNotFound
|
||||
}
|
||||
|
||||
return encoder.Encode(v)
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
type encoder struct {
|
||||
b []byte
|
||||
}
|
||||
|
||||
func (e encoder) Encode(_ map[string]any) ([]byte, error) {
|
||||
return e.b, nil
|
||||
}
|
||||
|
||||
func TestEncoderRegistry_RegisterEncoder(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder{})
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("AlreadyRegistered", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder{})
|
||||
require.NoError(t, err)
|
||||
|
||||
err = registry.RegisterEncoder("myformat", encoder{})
|
||||
assert.ErrorIs(t, err, ErrEncoderFormatAlreadyRegistered)
|
||||
})
|
||||
}
|
||||
|
||||
func TestEncoderRegistry_Decode(t *testing.T) {
|
||||
t.Run("OK", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
encoder := encoder{
|
||||
b: []byte("key: value"),
|
||||
}
|
||||
|
||||
err := registry.RegisterEncoder("myformat", encoder)
|
||||
require.NoError(t, err)
|
||||
|
||||
b, err := registry.Encode("myformat", map[string]any{"key": "value"})
|
||||
require.NoError(t, err)
|
||||
|
||||
assert.Equal(t, "key: value", string(b))
|
||||
})
|
||||
|
||||
t.Run("EncoderNotFound", func(t *testing.T) {
|
||||
registry := NewEncoderRegistry()
|
||||
|
||||
_, err := registry.Encode("myformat", map[string]any{"key": "value"})
|
||||
assert.ErrorIs(t, err, ErrEncoderNotFound)
|
||||
})
|
||||
}
|
|
@ -1,7 +0,0 @@
|
|||
package encoding
|
||||
|
||||
type encodingError string
|
||||
|
||||
func (e encodingError) Error() string {
|
||||
return string(e)
|
||||
}
|
Loading…
Reference in New Issue