-
Notifications
You must be signed in to change notification settings - Fork 74
/
decoders.go
47 lines (38 loc) · 1.27 KB
/
decoders.go
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
package topicreadercommon
import (
"compress/gzip"
"errors"
"fmt"
"io"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon"
"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
)
type DecoderMap struct {
m map[rawtopiccommon.Codec]PublicCreateDecoderFunc
}
func NewDecoderMap() DecoderMap {
return DecoderMap{
m: map[rawtopiccommon.Codec]PublicCreateDecoderFunc{
rawtopiccommon.CodecRaw: func(input io.Reader) (io.Reader, error) {
return input, nil
},
rawtopiccommon.CodecGzip: func(input io.Reader) (io.Reader, error) {
return gzip.NewReader(input)
},
},
}
}
func (m *DecoderMap) AddDecoder(codec rawtopiccommon.Codec, createFunc PublicCreateDecoderFunc) {
m.m[codec] = createFunc
}
func (m *DecoderMap) Decode(codec rawtopiccommon.Codec, input io.Reader) (io.Reader, error) {
if f := m.m[codec]; f != nil {
return f(input)
}
return nil, xerrors.WithStackTrace(xerrors.Wrap(
fmt.Errorf("ydb: failed decompress message with codec %v: %w", codec, ErrPublicUnexpectedCodec),
))
}
type PublicCreateDecoderFunc func(input io.Reader) (io.Reader, error)
// ErrPublicUnexpectedCodec return when try to read message content with unknown codec
var ErrPublicUnexpectedCodec = xerrors.Wrap(errors.New("ydb: unexpected codec"))