-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcodec.go
More file actions
202 lines (154 loc) · 5.1 KB
/
Copy pathcodec.go
File metadata and controls
202 lines (154 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
package streaming
import (
"encoding/json"
"fmt"
"sync"
streaming "github.com/xraph/forge/extensions/streaming/internal"
)
// Codec handles encoding and decoding messages for a specific content type.
type Codec interface {
// ContentType returns the MIME type this codec handles (e.g. "application/json").
ContentType() string
// Encode serializes a message to bytes.
Encode(msg *streaming.Message) ([]byte, error)
// Decode deserializes bytes into a message.
Decode(data []byte, msg *streaming.Message) error
}
// CodecRegistry manages codecs by content type and provides
// encode/decode dispatch based on message content type.
type CodecRegistry struct {
mu sync.RWMutex
codecs map[string]Codec
defaultCodec Codec
}
// NewCodecRegistry creates a new codec registry pre-loaded with a JSON codec as default.
func NewCodecRegistry() *CodecRegistry {
jsonCodec := &JSONCodec{}
r := &CodecRegistry{
codecs: map[string]Codec{
jsonCodec.ContentType(): jsonCodec,
},
defaultCodec: jsonCodec,
}
// Register built-in codecs.
binaryCodec := &BinaryCodec{}
r.codecs[binaryCodec.ContentType()] = binaryCodec
textCodec := &TextCodec{}
r.codecs[textCodec.ContentType()] = textCodec
return r
}
// Register adds a codec. If a codec for the same content type already exists, it is replaced.
func (r *CodecRegistry) Register(codec Codec) {
r.mu.Lock()
defer r.mu.Unlock()
r.codecs[codec.ContentType()] = codec
}
// Get returns the codec for the given content type.
func (r *CodecRegistry) Get(contentType string) (Codec, bool) {
r.mu.RLock()
defer r.mu.RUnlock()
c, ok := r.codecs[contentType]
return c, ok
}
// Default returns the default codec (JSON).
func (r *CodecRegistry) Default() Codec {
r.mu.RLock()
defer r.mu.RUnlock()
return r.defaultCodec
}
// SetDefault changes the default codec to the one registered for the given content type.
func (r *CodecRegistry) SetDefault(contentType string) error {
r.mu.Lock()
defer r.mu.Unlock()
c, ok := r.codecs[contentType]
if !ok {
return fmt.Errorf("no codec registered for content type %q", contentType)
}
r.defaultCodec = c
return nil
}
// Encode serializes a message using the codec matching msg.ContentType,
// or the default codec if ContentType is empty.
func (r *CodecRegistry) Encode(msg *streaming.Message) ([]byte, error) {
ct := msg.ContentType
if ct == "" {
return r.Default().Encode(msg)
}
r.mu.RLock()
c, ok := r.codecs[ct]
r.mu.RUnlock()
if !ok {
return nil, fmt.Errorf("no codec registered for content type %q", ct)
}
return c.Encode(msg)
}
// Decode deserializes bytes into a message using the default codec.
// If decoding fails with the default codec, it returns the error.
func (r *CodecRegistry) Decode(data []byte, msg *streaming.Message) error {
return r.Default().Decode(data, msg)
}
// DecodeWithType deserializes bytes into a message using the codec for the given content type.
func (r *CodecRegistry) DecodeWithType(contentType string, data []byte, msg *streaming.Message) error {
if contentType == "" {
return r.Default().Decode(data, msg)
}
r.mu.RLock()
c, ok := r.codecs[contentType]
r.mu.RUnlock()
if !ok {
return fmt.Errorf("no codec registered for content type %q", contentType)
}
return c.Decode(data, msg)
}
// --- Built-in Codecs ---
// JSONCodec encodes/decodes messages as JSON. This is the default codec.
type JSONCodec struct{}
func (c *JSONCodec) ContentType() string { return streaming.ContentTypeJSON }
func (c *JSONCodec) Encode(msg *streaming.Message) ([]byte, error) {
return json.Marshal(msg)
}
func (c *JSONCodec) Decode(data []byte, msg *streaming.Message) error {
return json.Unmarshal(data, msg)
}
// BinaryCodec handles raw binary data. On decode, it stores the raw bytes
// in msg.RawData and sets the content type. On encode, it returns msg.RawData directly.
type BinaryCodec struct{}
func (c *BinaryCodec) ContentType() string { return streaming.ContentTypeBinary }
func (c *BinaryCodec) Encode(msg *streaming.Message) ([]byte, error) {
if msg.RawData != nil {
return msg.RawData, nil
}
// Fall back to JSON encoding the whole message if no RawData.
return json.Marshal(msg)
}
func (c *BinaryCodec) Decode(data []byte, msg *streaming.Message) error {
msg.ContentType = streaming.ContentTypeBinary
msg.RawData = make([]byte, len(data))
copy(msg.RawData, data)
if msg.Type == "" {
msg.Type = streaming.MessageTypeMessage
}
return nil
}
// TextCodec handles plain text data. On decode, it stores the text in msg.Data
// as a string. On encode, it converts msg.Data to a string and returns bytes.
type TextCodec struct{}
func (c *TextCodec) ContentType() string { return streaming.ContentTypeText }
func (c *TextCodec) Encode(msg *streaming.Message) ([]byte, error) {
if msg.RawData != nil {
return msg.RawData, nil
}
if s, ok := msg.Data.(string); ok {
return []byte(s), nil
}
// Fall back to JSON for non-string data.
return json.Marshal(msg.Data)
}
func (c *TextCodec) Decode(data []byte, msg *streaming.Message) error {
msg.ContentType = streaming.ContentTypeText
msg.Data = string(data)
if msg.Type == "" {
msg.Type = streaming.MessageTypeMessage
}
return nil
}