BewareMyPower commented on PR #149:
URL: 
https://github.com/apache/pulsar-client-cpp/pull/149#issuecomment-1366471300

   @shibd See the point I mentioned in this PR:
   
   > Templatizing Producer, Consumer or Message could expose all internal code.
   
   You can try implementing the templated producer yourself. Then you might 
know better what I meant.
   
   BTW, `std::function` is flexible enough. The `SchemaInfo` can be catched in 
a `std::function`, for example, assuming here is a `StudentSchema` class to 
serialize a `Student` class:
   
   ```c++
   struct Student {
       int age;
       std::string name;
   };
   
   class StudentSchema {
      public:
       StudentSchema() {}
   
       SchemaInfo getSchemaInfo() const { return schemaInfo_; }
   
       std::string operator()(const Student& student) const {
           // TODO: serialize student using the schemaInfo_ and Avro C++ client
           return R"("age": )" + std::to_string(student.age) + R"(, "name": ")" 
+ student.name + R"(")";
       }
   
      private:
       SchemaInfo schemaInfo_{SchemaType::AVRO, "<name>", "<avro-string>"};
   };
   ```
   
   Then, users can write a producer like:
   
   ```c++
       StudentSchema schema;
       Client client(lookupUrl);
       Producer producer;
       ProducerConfiguration conf;
       conf.setSchema(schema.getSchemaInfo());
       client.createProducer("my-topic", conf, producer);
       // NOTE: schema is a StudentSchema, which could be treated as
       // a std::function<std::string(const Student&)>
       producer.send(TypedMessageBuilder<Student>{schema}.setValue(Student{10, 
"xyz"}).build());
   ```


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to