1717 * under the License.
1818 */
1919#include " SchemaInfo.h"
20+ #include < pulsar/ConsumerConfiguration.h>
21+ #include < pulsar/ProducerConfiguration.h>
2022#include < map>
2123
2224static const std::string CFG_SCHEMA_TYPE = " schemaType" ;
2325static const std::string CFG_NAME = " name" ;
2426static const std::string CFG_SCHEMA = " schema" ;
2527static const std::string CFG_PROPS = " properties" ;
2628
27- static const std::map<std::string, pulsar_schema_type> SCHEMA_TYPE = {{" None" , pulsar_None},
28- {" String" , pulsar_String},
29- {" Json" , pulsar_Json},
30- {" Protobuf" , pulsar_Protobuf},
31- {" Avro" , pulsar_Avro},
32- {" Boolean" , pulsar_Boolean},
33- {" Int8" , pulsar_Int8},
34- {" Int16" , pulsar_Int16},
35- {" Int32" , pulsar_Int32},
36- {" Int64" , pulsar_Int64},
37- {" Float32" , pulsar_Float32},
38- {" Float64" , pulsar_Float64},
39- {" KeyValue" , pulsar_KeyValue},
40- {" Bytes" , pulsar_Bytes},
41- {" AutoConsume" , pulsar_AutoConsume},
42- {" AutoPublish" , pulsar_AutoPublish}};
29+ struct _pulsar_producer_configuration {
30+ pulsar::ProducerConfiguration conf;
31+ };
4332
44- SchemaInfo::SchemaInfo (const Napi::Object &schemaInfo) : cSchemaType(pulsar_Bytes), name(" BYTES" ), schema() {
45- this ->cProperties = pulsar_string_map_create ();
33+ struct _pulsar_consumer_configuration {
34+ pulsar::ConsumerConfiguration consumerConfiguration;
35+ };
36+
37+ static const std::map<std::string, pulsar::SchemaType> SCHEMA_TYPE = {
38+ {" None" , static_cast <pulsar::SchemaType>(0 )},
39+ {" String" , static_cast <pulsar::SchemaType>(1 )},
40+ {" Json" , static_cast <pulsar::SchemaType>(2 )},
41+ {" Protobuf" , static_cast <pulsar::SchemaType>(3 )},
42+ {" Avro" , static_cast <pulsar::SchemaType>(4 )},
43+ {" Boolean" , static_cast <pulsar::SchemaType>(5 )},
44+ {" Int8" , static_cast <pulsar::SchemaType>(6 )},
45+ {" Int16" , static_cast <pulsar::SchemaType>(7 )},
46+ {" Int32" , static_cast <pulsar::SchemaType>(8 )},
47+ {" Int64" , static_cast <pulsar::SchemaType>(9 )},
48+ {" Float32" , static_cast <pulsar::SchemaType>(10 )},
49+ {" Float64" , static_cast <pulsar::SchemaType>(11 )},
50+ {" KeyValue" , static_cast <pulsar::SchemaType>(15 )},
51+ {" ProtobufNative" , static_cast <pulsar::SchemaType>(20 )},
52+ {" Bytes" , static_cast <pulsar::SchemaType>(-1 )},
53+ {" AutoConsume" , static_cast <pulsar::SchemaType>(-3 )},
54+ {" AutoPublish" , static_cast <pulsar::SchemaType>(-4 )}};
55+
56+ SchemaInfo::SchemaInfo (const Napi::Object &schemaInfo)
57+ : schemaType(static_cast <pulsar::SchemaType>(-1 )), name(" BYTES" ), schema() {
4658 if (schemaInfo.Has (CFG_SCHEMA_TYPE ) && schemaInfo.Get (CFG_SCHEMA_TYPE ).IsString ()) {
47- this ->name = schemaInfo.Get (CFG_SCHEMA_TYPE ).ToString ().Utf8Value ();
48- this ->cSchemaType = SCHEMA_TYPE .at (schemaInfo.Get (CFG_SCHEMA_TYPE ).ToString ().Utf8Value ());
59+ std::string typeStr = schemaInfo.Get (CFG_SCHEMA_TYPE ).ToString ().Utf8Value ();
60+ auto it = SCHEMA_TYPE .find (typeStr);
61+ if (it == SCHEMA_TYPE .end ()) {
62+ Napi::TypeError::New (schemaInfo.Env (), " Unknown schemaType: " + typeStr).ThrowAsJavaScriptException ();
63+ return ;
64+ }
65+ this ->name = typeStr;
66+ this ->schemaType = it->second ;
4967 }
5068 if (schemaInfo.Has (CFG_NAME ) && schemaInfo.Get (CFG_NAME ).IsString ()) {
5169 this ->name = schemaInfo.Get (CFG_NAME ).ToString ().Utf8Value ();
@@ -60,19 +78,19 @@ SchemaInfo::SchemaInfo(const Napi::Object &schemaInfo) : cSchemaType(pulsar_Byte
6078 for (int i = 0 ; i < size; i++) {
6179 Napi::String key = arr.Get (i).ToString ();
6280 Napi::String value = propObj.Get (key).ToString ();
63- pulsar_string_map_put ( this ->cProperties , key.Utf8Value (). c_str (), value.Utf8Value (). c_str () );
81+ this ->properties [ key.Utf8Value ()] = value.Utf8Value ();
6482 }
6583 }
6684}
6785
6886void SchemaInfo::SetProducerSchema (std::shared_ptr<pulsar_producer_configuration_t > cProducerConfiguration) {
69- pulsar_producer_configuration_set_schema_info ( cProducerConfiguration. get (), this -> cSchemaType ,
70- this ->name . c_str () , this ->schema . c_str () , this ->cProperties );
87+ cProducerConfiguration-> conf . setSchema (
88+ pulsar::SchemaInfo ( this -> schemaType , this ->name , this ->schema , this ->properties ) );
7189}
7290
7391void SchemaInfo::SetConsumerSchema (std::shared_ptr<pulsar_consumer_configuration_t > cConsumerConfiguration) {
74- pulsar_consumer_configuration_set_schema_info ( cConsumerConfiguration. get (), this -> cSchemaType ,
75- this ->name . c_str () , this ->schema . c_str () , this ->cProperties );
92+ cConsumerConfiguration-> consumerConfiguration . setSchema (
93+ pulsar::SchemaInfo ( this -> schemaType , this ->name , this ->schema , this ->properties ) );
7694}
7795
78- SchemaInfo::~SchemaInfo () { pulsar_string_map_free ( this -> cProperties ); }
96+ SchemaInfo::~SchemaInfo () {}
0 commit comments