Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add schema support to Reader #741

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions pulsar/reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ type ReaderOptions struct {

// Decryption represents the encryption related fields required by the reader to decrypt a message.
Decryption *MessageDecryptionInfo

// Schema represents the schema implementation.
Schema Schema
}

// Reader can be used to scan through all the messages currently available in a topic.
Expand Down
1 change: 1 addition & 0 deletions pulsar/reader_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func newReader(client *client, options ReaderOptions) (Reader, error) {
nackRedeliveryDelay: defaultNackRedeliveryDelay,
replicateSubscriptionState: false,
decryption: options.Decryption,
schema: options.Schema,
}

reader := &reader{
Expand Down
43 changes: 43 additions & 0 deletions pulsar/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,3 +710,46 @@ func TestProducerReaderRSAEncryption(t *testing.T) {
assert.Equal(t, []byte(expectMsg), msg.Payload())
}
}

func TestReaderWithSchema(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
})

assert.Nil(t, err)
defer client.Close()

topic := newTopicName()
schema := NewStringSchema(nil)

// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topic,
Schema: schema,
})
assert.Nil(t, err)
defer producer.Close()

value := "hello pulsar"
_, err = producer.Send(context.Background(), &ProducerMessage{
Value: value,
})
assert.Nil(t, err)

// create reader
reader, err := client.CreateReader(ReaderOptions{
Topic: topic,
StartMessageID: EarliestMessageID(),
Schema: schema,
})
assert.Nil(t, err)
defer reader.Close()

msg, err := reader.Next(context.Background())
assert.NoError(t, err)

var res *string
err = msg.GetSchemaValue(&res)
assert.Nil(t, err)
assert.Equal(t, *res, value)
}