github.com/ydb-platform/ydb-go-sdk/v3@v3.57.0/internal/topic/topicreaderinternal/decoders.go (about)

     1  package topicreaderinternal
     2  
     3  import (
     4  	"compress/gzip"
     5  	"fmt"
     6  	"io"
     7  
     8  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/grpcwrapper/rawtopic/rawtopiccommon"
     9  	"github.com/ydb-platform/ydb-go-sdk/v3/internal/xerrors"
    10  )
    11  
    12  type decoderMap struct {
    13  	m map[rawtopiccommon.Codec]PublicCreateDecoderFunc
    14  }
    15  
    16  func newDecoderMap() decoderMap {
    17  	return decoderMap{
    18  		m: map[rawtopiccommon.Codec]PublicCreateDecoderFunc{
    19  			rawtopiccommon.CodecRaw: func(input io.Reader) (io.Reader, error) {
    20  				return input, nil
    21  			},
    22  			rawtopiccommon.CodecGzip: func(input io.Reader) (io.Reader, error) {
    23  				return gzip.NewReader(input)
    24  			},
    25  		},
    26  	}
    27  }
    28  
    29  func (m *decoderMap) AddDecoder(codec rawtopiccommon.Codec, createFunc PublicCreateDecoderFunc) {
    30  	m.m[codec] = createFunc
    31  }
    32  
    33  func (m *decoderMap) Decode(codec rawtopiccommon.Codec, input io.Reader) (io.Reader, error) {
    34  	if f := m.m[codec]; f != nil {
    35  		return f(input)
    36  	}
    37  
    38  	return nil, xerrors.WithStackTrace(xerrors.Wrap(
    39  		fmt.Errorf("ydb: failed decompress message with codec %v: %w", codec, ErrPublicUnexpectedCodec),
    40  	))
    41  }
    42  
    43  type PublicCreateDecoderFunc func(input io.Reader) (io.Reader, error)