martinzink commented on code in PR #1987:
URL: https://github.com/apache/nifi-minifi-cpp/pull/1987#discussion_r2533411886


##########
libminifi/src/minifi-c.cpp:
##########
@@ -27,12 +47,205 @@ std::string toString(MinifiStringView sv) {
   return {sv.data, sv.length};
 }
 
+std::string_view toStringView(MinifiStringView sv) {
+  return {sv.data, sv.length};
+}
+
+minifi::core::annotation::Input toInputRequirement(MinifiInputRequirement req) 
{
+  switch (req) {
+    case MINIFI_INPUT_REQUIRED: return 
minifi::core::annotation::Input::INPUT_REQUIRED;
+    case MINIFI_INPUT_ALLOWED: return 
minifi::core::annotation::Input::INPUT_ALLOWED;
+    case MINIFI_INPUT_FORBIDDEN: return 
minifi::core::annotation::Input::INPUT_FORBIDDEN;
+  }
+  gsl_FailFast();
+}
+
+minifi::core::logging::LOG_LEVEL toLogLevel(MinifiLogLevel lvl) {
+  switch (lvl) {
+    case MINIFI_LOG_LEVEL_TRACE: return minifi::core::logging::trace;
+    case MINIFI_LOG_LEVEL_DEBUG: return minifi::core::logging::debug;
+    case MINIFI_LOG_LEVEL_INFO: return minifi::core::logging::info;
+    case MINIFI_LOG_LEVEL_WARNING: return minifi::core::logging::warn;
+    case MINIFI_LOG_LEVEL_ERROR: return minifi::core::logging::err;
+    case MINIFI_LOG_LEVEL_CRITICAL: return minifi::core::logging::critical;
+    case MINIFI_LOG_LEVEL_OFF: return minifi::core::logging::off;
+  }
+  gsl_FailFast();
+}
+
+minifi::core::Property createProperty(const MinifiProperty* 
property_description) {
+  gsl_Expects(property_description);
+  std::vector<std::string_view> allowed_values;
+  allowed_values.reserve(property_description->allowed_values_count);
+  for (size_t i = 0; i < property_description->allowed_values_count; ++i) {
+    
allowed_values.push_back(toStringView(property_description->allowed_values_ptr[i]));
+  }
+  std::vector<std::string_view> allowed_types;
+  if (property_description->type) {
+    allowed_types.push_back(toStringView(*property_description->type));
+  }
+  std::optional<std::string_view> default_value;
+  if (property_description->default_value) {
+    default_value = toStringView(*property_description->default_value);
+  }
+  return minifi::core::Property{minifi::core::PropertyReference{
+    toStringView(property_description->name),
+    toStringView(property_description->display_name),
+    toStringView(property_description->description),
+    static_cast<bool>(property_description->is_required),
+    static_cast<bool>(property_description->is_sensitive),
+    std::span(allowed_values),
+    std::span(allowed_types),
+    default_value,
+    gsl::make_not_null(reinterpret_cast<const 
minifi::core::PropertyValidator*>(property_description->validator)),
+    static_cast<bool>(property_description->supports_expression_language)
+  }};
+}
+
+class CProcessorFactory : public minifi::core::ProcessorFactory {
+ public:
+  CProcessorFactory(std::string group_name, std::string class_name, 
minifi::utils::CProcessorClassDescription class_description)
+    : group_name_(std::move(group_name)),
+      class_name_(std::move(class_name)),
+      class_description_(std::move(class_description)) {}
+  std::unique_ptr<minifi::core::ProcessorApi> 
create(minifi::core::ProcessorMetadata metadata) override {
+    return std::make_unique<minifi::utils::CProcessor>(class_description_, 
metadata);
+  }
+
+  [[nodiscard]] std::string getGroupName() const override {
+    return group_name_;
+  }
+
+  [[nodiscard]] std::string getClassName() const override {
+    return class_name_;
+  }
+
+  CProcessorFactory() = delete;
+  CProcessorFactory(const CProcessorFactory&) = delete;
+  CProcessorFactory& operator=(const CProcessorFactory&) = delete;
+  CProcessorFactory(CProcessorFactory&&) = delete;
+  CProcessorFactory& operator=(CProcessorFactory&&) = delete;
+  ~CProcessorFactory() override = default;
+
+ private:
+  std::string group_name_;
+  std::string class_name_;
+  minifi::utils::CProcessorClassDescription class_description_;
+};
+
 }  // namespace
 
+namespace org::apache::nifi::minifi::utils {
+
+void useCProcessorClassDescription(const MinifiProcessorClassDescription& 
class_description, const std::function<void(minifi::ClassDescription, 
minifi::utils::CProcessorClassDescription)>& fn) {
+  std::vector<minifi::core::Property> properties;
+  properties.reserve(class_description.class_properties_count);
+  for (size_t i = 0; i < class_description.class_properties_count; ++i) {
+    
properties.push_back(createProperty(&class_description.class_properties_ptr[i]));
+  }
+  std::vector<minifi::core::DynamicProperty> dynamic_properties;
+  dynamic_properties.reserve(class_description.dynamic_properties_count);
+  for (size_t i = 0; i < class_description.dynamic_properties_count; ++i) {
+    dynamic_properties.push_back(minifi::core::DynamicProperty{
+      .name = toStringView(class_description.dynamic_properties_ptr[i].name),
+      .value = toStringView(class_description.dynamic_properties_ptr[i].value),
+      .description = 
toStringView(class_description.dynamic_properties_ptr[i].description),
+      .supports_expression_language = 
static_cast<bool>(class_description.dynamic_properties_ptr[i].supports_expression_language)
+    });
+  }
+  std::vector<minifi::core::Relationship> relationships;
+  relationships.reserve(class_description.class_relationships_count);
+  for (size_t i = 0; i < class_description.class_relationships_count; ++i) {
+    relationships.push_back(minifi::core::Relationship{
+      toString(class_description.class_relationships_ptr[i].name),
+      toString(class_description.class_relationships_ptr[i].description)
+    });
+  }
+  std::vector<std::vector<minifi::core::RelationshipDefinition>> 
output_attribute_relationships;
+  std::vector<minifi::core::OutputAttributeReference> output_attributes;
+  for (size_t attribute_idx = 0; attribute_idx < 
class_description.output_attributes_count; ++attribute_idx) {
+    minifi::core::OutputAttributeReference 
ref{minifi::core::OutputAttributeDefinition{"", {}, ""}};
+    ref.name = 
toStringView(class_description.output_attributes_ptr[attribute_idx].name);
+    ref.description = 
toStringView(class_description.output_attributes_ptr[attribute_idx].description);

Review Comment:
   We dont copy the strings here, so this will cause problems later on (and 
since OutputAttributes only operate with string_view-s there is no good place 
to store these strings...)
   
   Currently its not a huge issue because we dont really use these anywhere 
except the documentation generating (and we are yet to do that for C 
processors, I was working on a PR to enable that when I encountered these)



-- 
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