Copilot commented on code in PR #486:
URL:
https://github.com/apache/pulsar-client-node/pull/486#discussion_r3298172885
##########
src/SchemaInfo.cc:
##########
@@ -17,35 +17,47 @@
* under the License.
*/
#include "SchemaInfo.h"
+#include <pulsar/ConsumerConfiguration.h>
+#include <pulsar/ProducerConfiguration.h>
#include <map>
static const std::string CFG_SCHEMA_TYPE = "schemaType";
static const std::string CFG_NAME = "name";
static const std::string CFG_SCHEMA = "schema";
static const std::string CFG_PROPS = "properties";
-static const std::map<std::string, pulsar_schema_type> SCHEMA_TYPE = {{"None",
pulsar_None},
-
{"String", pulsar_String},
- {"Json",
pulsar_Json},
-
{"Protobuf", pulsar_Protobuf},
- {"Avro",
pulsar_Avro},
-
{"Boolean", pulsar_Boolean},
- {"Int8",
pulsar_Int8},
-
{"Int16", pulsar_Int16},
-
{"Int32", pulsar_Int32},
-
{"Int64", pulsar_Int64},
-
{"Float32", pulsar_Float32},
-
{"Float64", pulsar_Float64},
-
{"KeyValue", pulsar_KeyValue},
-
{"Bytes", pulsar_Bytes},
-
{"AutoConsume", pulsar_AutoConsume},
-
{"AutoPublish", pulsar_AutoPublish}};
+struct _pulsar_producer_configuration {
+ pulsar::ProducerConfiguration conf;
+};
-SchemaInfo::SchemaInfo(const Napi::Object &schemaInfo) :
cSchemaType(pulsar_Bytes), name("BYTES"), schema() {
- this->cProperties = pulsar_string_map_create();
+struct _pulsar_consumer_configuration {
+ pulsar::ConsumerConfiguration consumerConfiguration;
+};
+
+static const std::map<std::string, pulsar::SchemaType> SCHEMA_TYPE = {
+ {"None", static_cast<pulsar::SchemaType>(0)},
+ {"String", static_cast<pulsar::SchemaType>(1)},
+ {"Json", static_cast<pulsar::SchemaType>(2)},
+ {"Protobuf", static_cast<pulsar::SchemaType>(3)},
+ {"Avro", static_cast<pulsar::SchemaType>(4)},
+ {"Boolean", static_cast<pulsar::SchemaType>(5)},
+ {"Int8", static_cast<pulsar::SchemaType>(6)},
+ {"Int16", static_cast<pulsar::SchemaType>(7)},
+ {"Int32", static_cast<pulsar::SchemaType>(8)},
+ {"Int64", static_cast<pulsar::SchemaType>(9)},
+ {"Float32", static_cast<pulsar::SchemaType>(10)},
+ {"Float64", static_cast<pulsar::SchemaType>(11)},
+ {"KeyValue", static_cast<pulsar::SchemaType>(15)},
+ {"ProtobufNative", static_cast<pulsar::SchemaType>(20)},
+ {"Bytes", static_cast<pulsar::SchemaType>(-1)},
+ {"AutoConsume", static_cast<pulsar::SchemaType>(-3)},
+ {"AutoPublish", static_cast<pulsar::SchemaType>(-4)}};
+
+SchemaInfo::SchemaInfo(const Napi::Object &schemaInfo)
+ : schemaType(static_cast<pulsar::SchemaType>(-1)), name("BYTES"), schema()
{
if (schemaInfo.Has(CFG_SCHEMA_TYPE) &&
schemaInfo.Get(CFG_SCHEMA_TYPE).IsString()) {
this->name = schemaInfo.Get(CFG_SCHEMA_TYPE).ToString().Utf8Value();
- this->cSchemaType =
SCHEMA_TYPE.at(schemaInfo.Get(CFG_SCHEMA_TYPE).ToString().Utf8Value());
+ this->schemaType =
SCHEMA_TYPE.at(schemaInfo.Get(CFG_SCHEMA_TYPE).ToString().Utf8Value());
}
Review Comment:
`SCHEMA_TYPE.at(...)` will throw `std::out_of_range` if a user passes an
unknown `schemaType`, which can crash the Node process via an unhandled C++
exception. Prefer checking `SCHEMA_TYPE.count(...)` / `find(...)` and throwing
a `Napi::Error` (or defaulting) with a clear message instead of relying on
`at()`.
##########
src/ProtobufNativeSchema.js:
##########
@@ -0,0 +1,76 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+require('protobufjs/ext/descriptor');
+
Review Comment:
`protobufjs/ext/descriptor` is required twice (once for side effects and
once assigned to `descriptor`). The second `require` already triggers module
initialization due to Node's caching semantics, so the extra top-level
`require('protobufjs/ext/descriptor')` is redundant and can be removed to avoid
confusion about ordering/side effects.
##########
src/SchemaInfo.cc:
##########
@@ -17,35 +17,47 @@
* under the License.
*/
#include "SchemaInfo.h"
+#include <pulsar/ConsumerConfiguration.h>
+#include <pulsar/ProducerConfiguration.h>
#include <map>
static const std::string CFG_SCHEMA_TYPE = "schemaType";
static const std::string CFG_NAME = "name";
static const std::string CFG_SCHEMA = "schema";
static const std::string CFG_PROPS = "properties";
-static const std::map<std::string, pulsar_schema_type> SCHEMA_TYPE = {{"None",
pulsar_None},
-
{"String", pulsar_String},
- {"Json",
pulsar_Json},
-
{"Protobuf", pulsar_Protobuf},
- {"Avro",
pulsar_Avro},
-
{"Boolean", pulsar_Boolean},
- {"Int8",
pulsar_Int8},
-
{"Int16", pulsar_Int16},
-
{"Int32", pulsar_Int32},
-
{"Int64", pulsar_Int64},
-
{"Float32", pulsar_Float32},
-
{"Float64", pulsar_Float64},
-
{"KeyValue", pulsar_KeyValue},
-
{"Bytes", pulsar_Bytes},
-
{"AutoConsume", pulsar_AutoConsume},
-
{"AutoPublish", pulsar_AutoPublish}};
+struct _pulsar_producer_configuration {
+ pulsar::ProducerConfiguration conf;
+};
-SchemaInfo::SchemaInfo(const Napi::Object &schemaInfo) :
cSchemaType(pulsar_Bytes), name("BYTES"), schema() {
- this->cProperties = pulsar_string_map_create();
+struct _pulsar_consumer_configuration {
+ pulsar::ConsumerConfiguration consumerConfiguration;
+};
+
+static const std::map<std::string, pulsar::SchemaType> SCHEMA_TYPE = {
+ {"None", static_cast<pulsar::SchemaType>(0)},
+ {"String", static_cast<pulsar::SchemaType>(1)},
+ {"Json", static_cast<pulsar::SchemaType>(2)},
+ {"Protobuf", static_cast<pulsar::SchemaType>(3)},
+ {"Avro", static_cast<pulsar::SchemaType>(4)},
+ {"Boolean", static_cast<pulsar::SchemaType>(5)},
+ {"Int8", static_cast<pulsar::SchemaType>(6)},
+ {"Int16", static_cast<pulsar::SchemaType>(7)},
+ {"Int32", static_cast<pulsar::SchemaType>(8)},
+ {"Int64", static_cast<pulsar::SchemaType>(9)},
+ {"Float32", static_cast<pulsar::SchemaType>(10)},
+ {"Float64", static_cast<pulsar::SchemaType>(11)},
+ {"KeyValue", static_cast<pulsar::SchemaType>(15)},
+ {"ProtobufNative", static_cast<pulsar::SchemaType>(20)},
+ {"Bytes", static_cast<pulsar::SchemaType>(-1)},
+ {"AutoConsume", static_cast<pulsar::SchemaType>(-3)},
+ {"AutoPublish", static_cast<pulsar::SchemaType>(-4)}};
+
+SchemaInfo::SchemaInfo(const Napi::Object &schemaInfo)
+ : schemaType(static_cast<pulsar::SchemaType>(-1)), name("BYTES"), schema()
{
Review Comment:
The `SCHEMA_TYPE` mapping relies on hard-coded numeric
`static_cast<pulsar::SchemaType>(...)` values (including negative values). This
is brittle and makes it hard to validate correctness when the Pulsar C++ enum
changes; prefer using the named `pulsar::SchemaType` enumerators (or a single
authoritative mapping from the Pulsar headers) instead of magic numbers.
--
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]