Skip to content

Commit f4c4c8a

Browse files
authored
Merge pull request #1475 some refactor of experimental topic helpers
2 parents 4d75f71 + eb6cb0c commit f4c4c8a

File tree

5 files changed

+137
-9
lines changed

5 files changed

+137
-9
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* refactored experimental topic iterators in topicsugar package
2+
13
## v3.80.9
24
* Fixed bug in experimental api: `ydb.ParamsBuilder().Param().Optional()` receive pointer and really produce optional value.
35

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//go:build go1.23
2+
3+
package topicreaderexamples
4+
5+
import (
6+
"context"
7+
"fmt"
8+
9+
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader"
10+
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicsugar"
11+
firestore "google.golang.org/genproto/firestore/bundle"
12+
)
13+
14+
// IterateOverMessagesAsString is simple example for easy start read messages
15+
// it is not recommend way for heavy-load processing, batch processing usually will faster
16+
func IterateOverMessagesAsString(ctx context.Context, reader *topicreader.Reader) error {
17+
for msg, err := range topicsugar.StringIterator(ctx, reader) {
18+
if err != nil {
19+
return err
20+
}
21+
fmt.Println(msg.Data)
22+
_ = reader.Commit(msg.Context(), msg)
23+
}
24+
25+
return nil
26+
}
27+
28+
// IterateOverStructUnmarshalledFromJSON is example for effective way for unmarshal json message content to value
29+
func IterateOverStructUnmarshalledFromJSON(ctx context.Context, r *topicreader.Reader) error {
30+
//nolint:tagliatelle
31+
type S struct {
32+
MyField int `json:"my_field"`
33+
}
34+
35+
for msg, err := range topicsugar.JSONIterator[S](ctx, r) {
36+
if err != nil {
37+
return err
38+
}
39+
fmt.Println(msg.Data.MyField)
40+
}
41+
42+
return nil
43+
}
44+
45+
// IterateOverProtobufMessages is example for effective way for unmarshal protobuf message content to value
46+
func IterateOverProtobufMessages(ctx context.Context, r *topicreader.Reader) error {
47+
for msg, err := range topicsugar.ProtobufIterator[*firestore.BundledDocumentMetadata](ctx, r) {
48+
if err != nil {
49+
return err
50+
}
51+
fmt.Println(msg.Data.GetName())
52+
}
53+
54+
return nil
55+
}

tests/integration/topic_helpers_test.go

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,29 @@ func TestMessageReaderIterator(t *testing.T) {
4646
require.Equal(t, []string{"asd", "ddd", "ggg"}, results)
4747
}
4848

49+
func TestStringReaderIterator(t *testing.T) {
50+
scope := newScope(t)
51+
ctx := scope.Ctx
52+
53+
err := scope.TopicWriter().Write(ctx,
54+
topicwriter.Message{Data: strings.NewReader("asd")},
55+
topicwriter.Message{Data: strings.NewReader("ddd")},
56+
topicwriter.Message{Data: strings.NewReader("ggg")},
57+
)
58+
require.NoError(t, err)
59+
60+
var results []string
61+
for mess, err := range topicsugar.StringIterator(ctx, scope.TopicReader()) {
62+
require.NoError(t, err)
63+
64+
results = append(results, mess.Data)
65+
if len(results) == 3 {
66+
break
67+
}
68+
}
69+
require.Equal(t, []string{"asd", "ddd", "ggg"}, results)
70+
}
71+
4972
func TestMessageJsonUnmarshalIterator(t *testing.T) {
5073
scope := newScope(t)
5174
ctx := scope.Ctx
@@ -71,7 +94,7 @@ func TestMessageJsonUnmarshalIterator(t *testing.T) {
7194
var results []testStruct
7295
expectedSeqno := int64(1)
7396
expectedOffset := int64(0)
74-
for mess, err := range topicsugar.TopicUnmarshalJSONIterator[testStruct](ctx, scope.TopicReader()) {
97+
for mess, err := range topicsugar.JSONIterator[testStruct](ctx, scope.TopicReader()) {
7598
require.NoError(t, err)
7699
require.Equal(t, expectedSeqno, mess.SeqNo)
77100
require.Equal(t, expectedOffset, mess.Offset)

topic/topicsugar/cdc-reader.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,5 +87,5 @@ func UnmarshalCDCStream[T YDBCDCItem[K], K any](
8787
return json.Unmarshal(data, dst)
8888
}
8989

90-
return TopicUnmarshalJSONFunc[YDBCDCMessage[T, K]](ctx, reader, unmarshal)
90+
return IteratorFunc[YDBCDCMessage[T, K]](ctx, reader, unmarshal)
9191
}

topic/topicsugar/iterators.go

Lines changed: 55 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,26 @@
1+
//go:build go1.23
2+
13
package topicsugar
24

35
import (
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
1419
type 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
2126
func 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

Comments
 (0)