lidavidm commented on code in PR #86:
URL: https://github.com/apache/iceberg-cpp/pull/86#discussion_r2055474145


##########
test/formatter_test.cc:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.
+ */
+
+#include "iceberg/util/formatter.h"
+
+#include <format>
+#include <map>
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include <gtest/gtest.h>
+
+#include "iceberg/statistics_file.h"
+
+namespace iceberg {
+
+// Tests for the std::format specializations
+TEST(FormatterTest, VectorFormat) {
+  std::vector<int> empty;
+  EXPECT_EQ("[]", std::format("{}", empty));
+
+  std::vector<int> nums = {1, 2, 3, 4, 5};
+  EXPECT_EQ("[1, 2, 3, 4, 5]", std::format("{}", nums));
+
+  std::vector<std::string> names = {"Alice", "Bob", "Charlie"};
+  EXPECT_EQ("[Alice, Bob, Charlie]", std::format("{}", names));
+}
+
+TEST(FormatterTest, MapFormat) {
+  std::map<std::string, int> empty;
+  EXPECT_EQ("{}", std::format("{}", empty));
+
+  std::map<std::string, int> ages = {{"Alice", 30}, {"Bob", 25}, {"Charlie", 
35}};
+  EXPECT_EQ("{Alice: 30, Bob: 25, Charlie: 35}", std::format("{}", ages));
+}
+
+TEST(FormatterTest, UnorderedMapFormat) {
+  std::unordered_map<std::string, double> empty;
+  EXPECT_EQ("{}", std::format("{}", empty));
+
+  std::unordered_map<std::string, double> scores = {
+      {"Alice", 95.5}, {"Bob", 87.0}, {"Charlie", 92.3}};
+  std::string str = std::format("{}", scores);
+  EXPECT_TRUE(str.find("Alice: 95.5") != std::string::npos);
+  EXPECT_TRUE(str.find("Bob: 87") != std::string::npos);
+  EXPECT_TRUE(str.find("Charlie: 92.3") != std::string::npos);
+}
+
+TEST(FormatterTest, NestedContainersFormat) {
+  std::vector<std::map<std::string, int>> nested = {{{"a", 1}, {"b", 2}},
+                                                    {{"c", 3}, {"d", 4}}};
+
+  EXPECT_EQ("[{a: 1, b: 2}, {c: 3, d: 4}]", std::format("{}", nested));
+
+  std::map<std::string, std::vector<int>> nested_map = {
+      {"primes", {2, 3, 5, 7, 11}}, {"fibonacci", {1, 1, 2, 3, 5, 8, 13}}};
+  std::string result = std::format("{}", nested_map);
+  EXPECT_TRUE(result.find("primes") != std::string::npos);

Review Comment:
   Note that you can do `EXPECT_THAT(result, testing::HasSubstr("primes"));` 
which will also give a much better error on failure



##########
src/iceberg/util/formatter.h:
##########
@@ -40,3 +43,102 @@ struct std::formatter<Derived> : 
std::formatter<std::string_view> {
     return std::formatter<string_view>::format(obj.ToString(), ctx);
   }
 };
+
+/// \brief std::formatter specialization for std::vector
+template <typename T>
+struct std::formatter<std::vector<T>> : std::formatter<std::string_view> {

Review Comment:
   FWIW, I _think_ the "idiomatic" way is supposed to be `std::format::join` 
and ranges. I wonder if specializing on std types may come back to bite us 
later.
   
   Example: https://stackoverflow.com/a/77993463



##########
src/iceberg/util/formatter.h:
##########
@@ -40,3 +43,102 @@ struct std::formatter<Derived> : 
std::formatter<std::string_view> {
     return std::formatter<string_view>::format(obj.ToString(), ctx);
   }
 };
+
+/// \brief std::formatter specialization for std::vector
+template <typename T>
+struct std::formatter<std::vector<T>> : std::formatter<std::string_view> {
+  template <class FormatContext>
+  auto format(const std::vector<T>& vec, FormatContext& ctx) const {
+    std::string result = "[";
+
+    bool first = true;
+    for (const auto& item : vec) {
+      if (!first) {
+        std::format_to(std::back_inserter(result), ", ");
+      }
+      if constexpr (requires { *item; }) {
+        if (item) {
+          std::format_to(std::back_inserter(result), "{}", *item);
+        } else {
+          std::format_to(std::back_inserter(result), "null");
+        }
+      } else {
+        std::format_to(std::back_inserter(result), "{}", item);
+      }
+      first = false;
+    }
+
+    std::format_to(std::back_inserter(result), "]");
+    return std::formatter<std::string_view>::format(result, ctx);
+  }
+};
+
+/// \brief Helper template for formatting map-like containers
+template <typename MapType>
+std::string FormatMap(const MapType& map) {
+  std::string result = "{";
+
+  bool first = true;
+  for (const auto& [key, value] : map) {
+    if (!first) {
+      std::format_to(std::back_inserter(result), ", ");
+    }
+
+    // Format key (handle if it's a smart pointer)
+    if constexpr (requires { *key; }) {

Review Comment:
   Won't this also fire on something like `std::optional` that might be 
undesirable?



##########
src/iceberg/util/formatter.h:
##########
@@ -40,3 +43,102 @@ struct std::formatter<Derived> : 
std::formatter<std::string_view> {
     return std::formatter<string_view>::format(obj.ToString(), ctx);
   }
 };
+
+/// \brief std::formatter specialization for std::vector
+template <typename T>
+struct std::formatter<std::vector<T>> : std::formatter<std::string_view> {

Review Comment:
   Or another example, albeit C++23: https://stackoverflow.com/a/71196142



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to