...
1 package encoding
2
3 import (
4 "sync"
5 )
6
7
8
9 type Encoder interface {
10 Encode(v map[string]any) ([]byte, error)
11 }
12
13 const (
14
15 ErrEncoderNotFound = encodingError("encoder not found for this format")
16
17
18 ErrEncoderFormatAlreadyRegistered = encodingError("encoder already registered for this format")
19 )
20
21
22 type EncoderRegistry struct {
23 encoders map[string]Encoder
24
25 mu sync.RWMutex
26 }
27
28
29 func NewEncoderRegistry() *EncoderRegistry {
30 return &EncoderRegistry{
31 encoders: make(map[string]Encoder),
32 }
33 }
34
35
36
37 func (e *EncoderRegistry) RegisterEncoder(format string, enc Encoder) error {
38 e.mu.Lock()
39 defer e.mu.Unlock()
40
41 if _, ok := e.encoders[format]; ok {
42 return ErrEncoderFormatAlreadyRegistered
43 }
44
45 e.encoders[format] = enc
46
47 return nil
48 }
49
50 func (e *EncoderRegistry) Encode(format string, v map[string]any) ([]byte, error) {
51 e.mu.RLock()
52 encoder, ok := e.encoders[format]
53 e.mu.RUnlock()
54
55 if !ok {
56 return nil, ErrEncoderNotFound
57 }
58
59 return encoder.Encode(v)
60 }
61
View as plain text