-
Notifications
You must be signed in to change notification settings - Fork 83
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1475 some refactor of experimental topic helpers
- Loading branch information
Showing
5 changed files
with
137 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
examples/topic/topicreader/topicreader_simple_iterators.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
//go:build go1.23 | ||
|
||
package topicreaderexamples | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
|
||
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicreader" | ||
"github.com/ydb-platform/ydb-go-sdk/v3/topic/topicsugar" | ||
firestore "google.golang.org/genproto/firestore/bundle" | ||
) | ||
|
||
// IterateOverMessagesAsString is simple example for easy start read messages | ||
// it is not recommend way for heavy-load processing, batch processing usually will faster | ||
func IterateOverMessagesAsString(ctx context.Context, reader *topicreader.Reader) error { | ||
for msg, err := range topicsugar.StringIterator(ctx, reader) { | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(msg.Data) | ||
_ = reader.Commit(msg.Context(), msg) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IterateOverStructUnmarshalledFromJSON is example for effective way for unmarshal json message content to value | ||
func IterateOverStructUnmarshalledFromJSON(ctx context.Context, r *topicreader.Reader) error { | ||
//nolint:tagliatelle | ||
type S struct { | ||
MyField int `json:"my_field"` | ||
} | ||
|
||
for msg, err := range topicsugar.JSONIterator[S](ctx, r) { | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(msg.Data.MyField) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
// IterateOverProtobufMessages is example for effective way for unmarshal protobuf message content to value | ||
func IterateOverProtobufMessages(ctx context.Context, r *topicreader.Reader) error { | ||
for msg, err := range topicsugar.ProtobufIterator[*firestore.BundledDocumentMetadata](ctx, r) { | ||
if err != nil { | ||
return err | ||
} | ||
fmt.Println(msg.Data.GetName()) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters