Github user phrocker commented on a diff in the pull request:

    https://github.com/apache/nifi-minifi-cpp/pull/417#discussion_r225497705
  
    --- Diff: libminifi/src/capi/api.cpp ---
    @@ -237,25 +239,58 @@ void update_attribute(flow_file_record *ff, char 
*key, void *value, size_t size)
      * @param caller_attribute caller supplied object in which we will copy 
the data ptr
      * @return 0 if successful, -1 if the key does not exist
      */
    -uint8_t get_attribute(flow_file_record *ff, char *key, attribute 
*caller_attribute) {
    +uint8_t get_attribute(flow_file_record *ff, attribute *caller_attribute) {
    +  if (ff == nullptr) {
    +    return -1;
    +  }
       auto attribute_map = static_cast<string_map*>(ff->attributes);
    -  auto find = attribute_map->find(key);
    +  if (!attribute_map) {
    +    return -1;
    +  }
    +  auto find = attribute_map->find(caller_attribute->key);
       if (find != attribute_map->end()) {
    -    caller_attribute->key = key;
         caller_attribute->value = 
static_cast<void*>(const_cast<char*>(find->second.data()));
         caller_attribute->value_size = find->second.size();
         return 0;
       }
       return -1;
     }
     
    +attribute_set *get_all_attributes(const flow_file_record* ff) {
    +  if (ff == nullptr) {
    +    return nullptr;
    +  }
    +  auto attribute_map = static_cast<string_map*>(ff->attributes);
    +  if (!attribute_map || attribute_map->empty()) {
    +    return nullptr;
    +  }
    +  attribute_set * attr_set = new attribute_set;
    +  attr_set->size = attribute_map->size();
    +  attr_set->attributes = new attribute[attr_set->size];
    +  std::transform(attribute_map->begin(), attribute_map->end(), 
attr_set->attributes,
    +      [](const string_map::value_type& key_value) -> attribute {
    +    attribute ret_val;
    +    ret_val.key = key_value.first.data();
    +    ret_val.value = 
static_cast<void*>(const_cast<char*>(key_value.second.data()));
    +    ret_val.value_size = key_value.second.size();
    +    return ret_val;
    +  });
    +  return attr_set;
    +}
    +
    +void free_attribute_set(attribute_set * attr_set) {
    --- End diff --
    
    Been thinking a bit about safety in an API that requires calls to free. Do 
you think we should use malloc/free instead of new/delete since users could 
potentially free these objects themselves? I've always run with the guidance 
that a free function should be used but users do bad things and call free 
themselves. May want to eventually move all news to malloc. May be useful to 
begin using malloc on new stuff if we decide that. 


---

Reply via email to