IBM / sarama

Sarama is a Go library for Apache Kafka.
MIT License
11.57k stars 1.76k forks source link

stringEncoder how to decode to json/map/struct/ ? #2211

Closed 825644691 closed 1 year ago

825644691 commented 2 years ago

msg := &sarama.ProducerMessage{} msg.Topic = "aaa" item := {'aa':aa,'bb':bb} (struct) res,_ := json.Marshal(item) msg.Value = sarama.StringEncoder(res)

has any way decode the StringEncoder to json/struct/map?

dnwe commented 2 years ago

@825644691 I don't understand the question? Here you've essentially populated a ProducerMessage with a string value containing the json body that you want to publish via Sarama. You've (correctly) used the StringEncoder type alias so the producer has the Encode() func available via the receiver

When you consume that message with a Consumer, you'll just get a []byte containing your string value, you can decode that back to struct as usual (json.Unmarshal)

https://github.com/Shopify/sarama/blob/095b45ab2157e80e41799e606b09c6c4dab3f5b6/utils.go#L68-L78

github-actions[bot] commented 1 year ago

Thank you for taking the time to raise this issue. However, it has not had any activity on it in the past 90 days and will be closed in 30 days if no updates occur. Please check if the main branch has already resolved the issue since it was raised. If you believe the issue is still valid and you would like input from the maintainers then please comment to ask for it to be reviewed.

dnwe commented 1 year ago

Closing as presumed to have been resolved. Please reopen if not