From e2ab48ad877a004acf5ae1f5bc42d4c712b1c077 Mon Sep 17 00:00:00 2001 From: Mark Sagi-Kazar Date: Tue, 23 Jul 2024 19:58:34 +0200 Subject: [PATCH] refactor: cleanup unused encoding code Signed-off-by: Mark Sagi-Kazar --- internal/encoding/decoder.go | 61 --------------------------- internal/encoding/decoder_test.go | 69 ------------------------------- internal/encoding/encoder.go | 60 --------------------------- internal/encoding/encoder_test.go | 59 -------------------------- internal/encoding/error.go | 7 ---- 5 files changed, 256 deletions(-) delete mode 100644 internal/encoding/decoder.go delete mode 100644 internal/encoding/decoder_test.go delete mode 100644 internal/encoding/encoder.go delete mode 100644 internal/encoding/encoder_test.go delete mode 100644 internal/encoding/error.go diff --git a/internal/encoding/decoder.go b/internal/encoding/decoder.go deleted file mode 100644 index 8a7b1db..0000000 --- a/internal/encoding/decoder.go +++ /dev/null @@ -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) -} diff --git a/internal/encoding/decoder_test.go b/internal/encoding/decoder_test.go deleted file mode 100644 index 7533e47..0000000 --- a/internal/encoding/decoder_test.go +++ /dev/null @@ -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) - }) -} diff --git a/internal/encoding/encoder.go b/internal/encoding/encoder.go deleted file mode 100644 index 6595859..0000000 --- a/internal/encoding/encoder.go +++ /dev/null @@ -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) -} diff --git a/internal/encoding/encoder_test.go b/internal/encoding/encoder_test.go deleted file mode 100644 index 9dd5e7a..0000000 --- a/internal/encoding/encoder_test.go +++ /dev/null @@ -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) - }) -} diff --git a/internal/encoding/error.go b/internal/encoding/error.go deleted file mode 100644 index e4cde02..0000000 --- a/internal/encoding/error.go +++ /dev/null @@ -1,7 +0,0 @@ -package encoding - -type encodingError string - -func (e encodingError) Error() string { - return string(e) -}