paleolimbot commented on code in PR #399:
URL: https://github.com/apache/arrow-nanoarrow/pull/399#discussion_r1557625729


##########
src/nanoarrow/nanoarrow_testing.hpp:
##########
@@ -2749,6 +2790,79 @@ class TestingJSONComparison {
       differences_.push_back({path, actual_json, expected_json});
     }
 
+    NANOARROW_RETURN_NOT_OK(CompareMetadata(actual->metadata, 
expected->metadata, error,
+                                            path + std::string(".metadata")));
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode CompareMetadata(const char* actual, const char* expected,
+                                 ArrowError* error, const std::string& path = 
"") {
+    std::stringstream ss;
+
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_actual_.WriteMetadata(ss, 
actual), error);
+    std::string actual_json = ss.str();
+
+    ss.str("");
+    NANOARROW_RETURN_NOT_OK_WITH_ERROR(writer_expected_.WriteMetadata(ss, 
expected),
+                                       error);
+    std::string expected_json = ss.str();
+
+    bool metadata_equal = actual_json == expected_json;
+
+    // If there is a difference in the rendered JSON but we aren't being 
strict about
+    // order, check again using the KeyValue comparison.
+    if (!metadata_equal && !compare_metadata_order_) {
+      NANOARROW_RETURN_NOT_OK(MetadataEqualKeyValue(actual, expected, 
&metadata_equal));
+    }
+
+    // If we still have an inequality, add a difference.
+    if (!metadata_equal) {
+      differences_.push_back({path, actual_json, expected_json});
+    }
+
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode MetadataEqualKeyValue(const char* actual, const char* 
expected,
+                                       bool* out) {
+    std::unordered_map<std::string, std::string> actual_map, expected_map;
+    NANOARROW_RETURN_NOT_OK(MetadataToMap(actual, &actual_map));
+    NANOARROW_RETURN_NOT_OK(MetadataToMap(expected, &expected_map));
+
+    if (actual_map.size() != expected_map.size()) {
+      *out = false;
+      return NANOARROW_OK;
+    }
+
+    for (const auto& item : expected_map) {
+      const auto& actual_item = actual_map.find(item.first);
+      if (actual_item == actual_map.end()) {
+        *out = false;
+        return NANOARROW_OK;
+      }
+
+      if (actual_item->second != item.second) {
+        *out = false;
+        return NANOARROW_OK;
+      }
+    }
+
+    *out = true;
+    return NANOARROW_OK;
+  }
+
+  ArrowErrorCode MetadataToMap(const char* metadata,
+                               std::unordered_map<std::string, std::string>* 
out) {
+    ArrowMetadataReader reader;
+    NANOARROW_RETURN_NOT_OK(ArrowMetadataReaderInit(&reader, metadata));
+
+    ArrowStringView key, value;
+    while (reader.remaining_keys > 0) {
+      NANOARROW_RETURN_NOT_OK(ArrowMetadataReaderRead(&reader, &key, &value));
+      out->insert({std::string(key.data, key.size_bytes),

Review Comment:
   Great catch! I will workshop this...



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