1+ //go:build go1.23
2+
13package topicsugar
24
35import (
46 "context"
57 "encoding/json"
8+ "slices"
9+
10+ "google.golang.org/protobuf/proto"
611
712 "github.com/ydb-platform/ydb-go-sdk/v3/internal/xiter"
813 "github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader"
914)
1015
11- // MessageReader is interface for topicreader.Message
16+ // TopicMessageReader is interface for topicreader.Message
1217//
1318// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
1419type TopicMessageReader interface {
1520 ReadMessage (ctx context.Context ) (* topicreader.Message , error )
1621}
1722
18- // TopicMessagesIterator is typed representation of cdc event
23+ // TopicMessageIterator iterator wrapper over topic reader
1924//
2025// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
2126func TopicMessageIterator (ctx context.Context , r TopicMessageReader ) xiter.Seq2 [* topicreader.Message , error ] {
@@ -33,24 +38,67 @@ func TopicMessageIterator(ctx context.Context, r TopicMessageReader) xiter.Seq2[
3338 }
3439}
3540
36- // TopicUnmarshalJSONIterator is typed representation of cdc event
41+ // BytesIterator produce iterator over topic messages with Data as []byte, []byte is content of the message
42+ func BytesIterator (
43+ ctx context.Context ,
44+ r TopicMessageReader ,
45+ ) xiter.Seq2 [* TypedTopicMessage [[]byte ], error ] {
46+ var unmarshalFunc TypedUnmarshalFunc [* []byte ] = func (data []byte , dst * []byte ) error {
47+ * dst = slices .Clone (data )
48+
49+ return nil
50+ }
51+
52+ return IteratorFunc [[]byte ](ctx , r , unmarshalFunc )
53+ }
54+
55+ // StringIterator produce iterator over topic messages with Data is string, created from message content
56+ func StringIterator (
57+ ctx context.Context ,
58+ r TopicMessageReader ,
59+ ) xiter.Seq2 [* TypedTopicMessage [string ], error ] {
60+ var unmarshalFunc TypedUnmarshalFunc [* string ] = func (data []byte , dst * string ) error {
61+ * dst = string (data )
62+
63+ return nil
64+ }
65+
66+ return IteratorFunc [string ](ctx , r , unmarshalFunc )
67+ }
68+
69+ // JSONIterator produce iterator over topic messages with Data is T, created unmarshalled from message
3770//
3871// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
39- func TopicUnmarshalJSONIterator [T any ](
72+ func JSONIterator [T any ](
4073 ctx context.Context ,
4174 r TopicMessageReader ,
4275) xiter.Seq2 [* TypedTopicMessage [T ], error ] {
4376 var unmarshalFunc TypedUnmarshalFunc [* T ] = func (data []byte , dst * T ) error {
4477 return json .Unmarshal (data , dst )
4578 }
4679
47- return TopicUnmarshalJSONFunc [T ](ctx , r , unmarshalFunc )
80+ return IteratorFunc [T ](ctx , r , unmarshalFunc )
81+ }
82+
83+ // ProtobufIterator produce iterator over topic messages with Data is T, created unmarshalled from message
84+ //
85+ // Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
86+ func ProtobufIterator [T proto.Message ](
87+ ctx context.Context ,
88+ r TopicMessageReader ,
89+ ) xiter.Seq2 [* TypedTopicMessage [T ], error ] {
90+ var unmarshalFunc TypedUnmarshalFunc [* T ] = func (data []byte , dst * T ) error {
91+ return proto .Unmarshal (data , * dst )
92+ }
93+
94+ return IteratorFunc [T ](ctx , r , unmarshalFunc )
4895}
4996
50- // TopicUnmarshalJSONIterator is typed representation of cdc event
97+ // IteratorFunc produce iterator over topic messages with Data is T,
98+ // created unmarshalled from message by custom function
5199//
52100// Experimental: https://github.com/ydb-platform/ydb-go-sdk/blob/master/VERSIONING.md#experimental
53- func TopicUnmarshalJSONFunc [T any ](
101+ func IteratorFunc [T any ](
54102 ctx context.Context ,
55103 r TopicMessageReader ,
56104 f TypedUnmarshalFunc [* T ],
0 commit comments