You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iceberg.apache.org by fo...@apache.org on 2025/04/23 19:18:59 UTC
(iceberg-cpp) branch main updated: feat: add formatter specialization (#86)
This is an automated email from the ASF dual-hosted git repository.
fokko pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg-cpp.git
The following commit(s) were added to refs/heads/main by this push:
new 60bff25 feat: add formatter specialization (#86)
60bff25 is described below
commit 60bff25728b9ef16448eb7eafa61bf8c7f3e8801
Author: Gang Wu <us...@gmail.com>
AuthorDate: Thu Apr 24 03:18:53 2025 +0800
feat: add formatter specialization (#86)
- Added std::formatter specialization for std::map, std::unordered_map
and std::vector when their elements are also formattable.
- Added std::formatter specialization for classes that have a ToString
at hands.
---
src/iceberg/statistics_file.cc | 38 ++------
src/iceberg/util/formatter.h | 12 +++
src/iceberg/util/formatter_internal.h | 121 +++++++++++++++++++++++++
test/CMakeLists.txt | 10 +--
test/formatter_test.cc | 163 ++++++++++++++++++++++++++++++++++
5 files changed, 309 insertions(+), 35 deletions(-)
diff --git a/src/iceberg/statistics_file.cc b/src/iceberg/statistics_file.cc
index 5ae753a..acff3c6 100644
--- a/src/iceberg/statistics_file.cc
+++ b/src/iceberg/statistics_file.cc
@@ -21,6 +21,8 @@
#include <format>
+#include "iceberg/util/formatter_internal.h"
+
namespace iceberg {
std::string ToString(const BlobMetadata& blob_metadata) {
@@ -29,26 +31,9 @@ std::string ToString(const BlobMetadata& blob_metadata) {
"type='{}',sourceSnapshotId={},sourceSnapshotSequenceNumber={},",
blob_metadata.type, blob_metadata.source_snapshot_id,
blob_metadata.source_snapshot_sequence_number);
- std::format_to(std::back_inserter(repr), "fields=[");
- for (auto iter = blob_metadata.fields.cbegin(); iter != blob_metadata.fields.cend();
- ++iter) {
- if (iter != blob_metadata.fields.cbegin()) {
- std::format_to(std::back_inserter(repr), ",{}", *iter);
- } else {
- std::format_to(std::back_inserter(repr), "{}", *iter);
- }
- }
- std::format_to(std::back_inserter(repr), "],properties=[");
- for (auto iter = blob_metadata.properties.cbegin();
- iter != blob_metadata.properties.cend(); ++iter) {
- const auto& [key, value] = *iter;
- if (iter != blob_metadata.properties.cbegin()) {
- std::format_to(std::back_inserter(repr), ",{}:{}", key, value);
- } else {
- std::format_to(std::back_inserter(repr), "{}:{}", key, value);
- }
- }
- repr += "]]";
+ std::format_to(std::back_inserter(repr), "fields={},", blob_metadata.fields);
+ std::format_to(std::back_inserter(repr), "properties={}", blob_metadata.properties);
+ std::format_to(std::back_inserter(repr), "]");
return repr;
}
@@ -59,16 +44,9 @@ std::string ToString(const StatisticsFile& statistics_file) {
statistics_file.snapshot_id, statistics_file.path,
statistics_file.file_size_in_bytes,
statistics_file.file_footer_size_in_bytes);
- std::format_to(std::back_inserter(repr), "blobMetadata=[");
- for (auto iter = statistics_file.blob_metadata.cbegin();
- iter != statistics_file.blob_metadata.cend(); ++iter) {
- if (iter != statistics_file.blob_metadata.cbegin()) {
- std::format_to(std::back_inserter(repr), ",{}", ToString(*iter));
- } else {
- std::format_to(std::back_inserter(repr), "{}", ToString(*iter));
- }
- }
- repr += "]]";
+ std::format_to(std::back_inserter(repr), "blobMetadata={}",
+ statistics_file.blob_metadata);
+ std::format_to(std::back_inserter(repr), "]");
return repr;
}
diff --git a/src/iceberg/util/formatter.h b/src/iceberg/util/formatter.h
index d9a6741..be2e246 100644
--- a/src/iceberg/util/formatter.h
+++ b/src/iceberg/util/formatter.h
@@ -40,3 +40,15 @@ struct std::formatter<Derived> : std::formatter<std::string_view> {
return std::formatter<string_view>::format(obj.ToString(), ctx);
}
};
+
+/// \brief std::formatter specialization for any type that has a ToString function
+template <typename T>
+ requires requires(const T& t) {
+ { ToString(t) } -> std::convertible_to<std::string>;
+ }
+struct std::formatter<T> : std::formatter<std::string_view> {
+ template <class FormatContext>
+ auto format(const T& value, FormatContext& ctx) const {
+ return std::formatter<std::string_view>::format(ToString(value), ctx);
+ }
+};
diff --git a/src/iceberg/util/formatter_internal.h b/src/iceberg/util/formatter_internal.h
new file mode 100644
index 0000000..f802bbe
--- /dev/null
+++ b/src/iceberg/util/formatter_internal.h
@@ -0,0 +1,121 @@
+/*
+ * 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.
+ */
+
+#pragma once
+
+#include <concepts>
+#include <format>
+#include <map>
+#include <ranges>
+#include <sstream>
+#include <string_view>
+#include <unordered_map>
+#include <vector>
+
+#include "iceberg/util/formatter.h"
+
+/// \brief Concept for smart pointer types
+template <typename T>
+concept SmartPointerType = requires(T t) {
+ { t.operator->() } -> std::same_as<typename T::element_type*>;
+ { *t } -> std::convertible_to<typename T::element_type&>;
+ { static_cast<bool>(t) } -> std::same_as<bool>;
+ typename T::element_type;
+};
+
+/// \brief Helper function to format an item using concepts to differentiate types
+template <typename T>
+std::string FormatItem(const T& item) {
+ if constexpr (SmartPointerType<T>) {
+ if (item) {
+ return std::format("{}", *item);
+ } else {
+ return "null";
+ }
+ } else {
+ return std::format("{}", item);
+ }
+}
+
+/// \brief Generic function to join a range of elements with a separator and wrap with
+/// delimiters
+template <std::ranges::input_range Range>
+std::string FormatRange(const Range& range, std::string_view separator,
+ std::string_view prefix, std::string_view suffix) {
+ if (std::ranges::empty(range)) {
+ return std::format("{}{}", prefix, suffix);
+ }
+
+ std::stringstream ss;
+ ss << prefix;
+
+ bool first = true;
+ for (const auto& element : range) {
+ if (!first) {
+ ss << separator;
+ }
+ ss << element;
+ first = false;
+ }
+
+ ss << suffix;
+ return ss.str();
+}
+
+/// \brief Helper template for formatting map-like containers
+template <typename MapType>
+std::string FormatMap(const MapType& map) {
+ // Transform the map items into formatted key-value pairs
+ std::ranges::transform_view formatted_range =
+ map | std::views::transform([](const auto& pair) -> std::string {
+ const auto& [key, value] = pair;
+ return std::format("{}: {}", FormatItem(key), FormatItem(value));
+ });
+ return FormatRange(formatted_range, ", ", "{", "}");
+}
+
+/// \brief std::formatter specialization for std::map
+template <typename K, typename V>
+struct std::formatter<std::map<K, V>> : std::formatter<std::string_view> {
+ template <class FormatContext>
+ auto format(const std::map<K, V>& map, FormatContext& ctx) const {
+ return std::formatter<std::string_view>::format(FormatMap(map), ctx);
+ }
+};
+
+/// \brief std::formatter specialization for std::unordered_map
+template <typename K, typename V>
+struct std::formatter<std::unordered_map<K, V>> : std::formatter<std::string_view> {
+ template <class FormatContext>
+ auto format(const std::unordered_map<K, V>& map, FormatContext& ctx) const {
+ return std::formatter<std::string_view>::format(FormatMap(map), 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 {
+ auto formatted_range =
+ vec | std::views::transform([](const auto& item) { return FormatItem(item); });
+ return std::formatter<std::string_view>::format(
+ FormatRange(formatted_range, ", ", "[", "]"), ctx);
+ }
+};
diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt
index c31c6e0..4178640 100644
--- a/test/CMakeLists.txt
+++ b/test/CMakeLists.txt
@@ -42,11 +42,6 @@ target_sources(schema_test
target_link_libraries(schema_test PRIVATE iceberg_static GTest::gtest_main GTest::gmock)
add_test(NAME schema_test COMMAND schema_test)
-add_executable(expected_test)
-target_sources(expected_test PRIVATE expected_test.cc)
-target_link_libraries(expected_test PRIVATE iceberg_static GTest::gtest_main GTest::gmock)
-add_test(NAME expected_test COMMAND expected_test)
-
add_executable(expression_test)
target_sources(expression_test PRIVATE expression_test.cc)
target_link_libraries(expression_test PRIVATE iceberg_static GTest::gtest_main
@@ -61,6 +56,11 @@ target_link_libraries(json_serde_test PRIVATE iceberg_static GTest::gtest_main
GTest::gmock)
add_test(NAME json_serde_test COMMAND json_serde_test)
+add_executable(util_test)
+target_sources(util_test PRIVATE expected_test.cc formatter_test.cc)
+target_link_libraries(util_test PRIVATE iceberg_static GTest::gtest_main GTest::gmock)
+add_test(NAME util_test COMMAND util_test)
+
if(ICEBERG_BUILD_BUNDLE)
add_executable(avro_test)
target_sources(avro_test PRIVATE avro_test.cc)
diff --git a/test/formatter_test.cc b/test/formatter_test.cc
new file mode 100644
index 0000000..767020b
--- /dev/null
+++ b/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 <format>
+#include <map>
+#include <memory>
+#include <string>
+#include <unordered_map>
+#include <vector>
+
+#include <gmock/gmock-matchers.h>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+
+#include "iceberg/statistics_file.h"
+#include "iceberg/util/formatter_internal.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_THAT(str, ::testing::HasSubstr("Alice: 95.5"));
+ EXPECT_THAT(str, ::testing::HasSubstr("Bob: 87"));
+ EXPECT_THAT(str, ::testing::HasSubstr("Charlie: 92.3"));
+}
+
+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_THAT(result, ::testing::HasSubstr("primes"));
+ EXPECT_THAT(result, ::testing::HasSubstr("fibonacci"));
+ EXPECT_THAT(result, ::testing::HasSubstr("[2, 3, 5, 7, 11]"));
+ EXPECT_THAT(result, ::testing::HasSubstr("[1, 1, 2, 3, 5, 8, 13]"));
+}
+
+TEST(FormatterTest, EdgeCasesFormat) {
+ std::vector<int> single_vec = {42};
+ EXPECT_EQ("[42]", std::format("{}", single_vec));
+
+ std::map<std::string, int> single_map = {{"key", 42}};
+ EXPECT_EQ("{key: 42}", std::format("{}", single_map));
+
+ std::vector<std::vector<int>> nested_empty = {{}, {1, 2}, {}};
+ EXPECT_EQ("[[], [1, 2], []]", std::format("{}", nested_empty));
+}
+
+TEST(FormatterTest, SmartPointerFormat) {
+ std::vector<std::shared_ptr<int>> int_ptrs = {
+ std::make_shared<int>(42),
+ std::make_shared<int>(123),
+ nullptr,
+ };
+ EXPECT_EQ("[42, 123, null]", std::format("{}", int_ptrs));
+
+ std::vector<std::shared_ptr<std::string>> str_ptrs = {
+ std::make_shared<std::string>("hello"),
+ std::make_shared<std::string>("world"),
+ nullptr,
+ };
+ EXPECT_EQ("[hello, world, null]", std::format("{}", str_ptrs));
+
+ std::map<std::string, std::shared_ptr<int>> map_with_ptr_values = {
+ {"one", std::make_shared<int>(1)},
+ {"two", std::make_shared<int>(2)},
+ {"null", nullptr},
+ };
+ EXPECT_EQ("{null: null, one: 1, two: 2}", std::format("{}", map_with_ptr_values));
+
+ std::unordered_map<std::string, std::shared_ptr<double>> scores = {
+ {"Alice", std::make_shared<double>(95.5)},
+ {"Bob", std::make_shared<double>(87.0)},
+ {"Charlie", nullptr},
+ };
+ std::string str = std::format("{}", scores);
+ EXPECT_THAT(str, ::testing::HasSubstr("Alice: 95.5"));
+ EXPECT_THAT(str, ::testing::HasSubstr("Bob: 87"));
+ EXPECT_THAT(str, ::testing::HasSubstr("Charlie: null"));
+
+ std::vector<std::map<std::string, std::shared_ptr<int>>> nested = {
+ {{"a", std::make_shared<int>(1)}, {"b", std::make_shared<int>(2)}},
+ {{"c", std::make_shared<int>(3)}, {"d", nullptr}},
+ };
+ EXPECT_EQ("[{a: 1, b: 2}, {c: 3, d: null}]", std::format("{}", nested));
+}
+
+TEST(FormatterTest, StatisticsFileFormat) {
+ StatisticsFile statistics_file{
+ .snapshot_id = 123,
+ .path = "test_path",
+ .file_size_in_bytes = 100,
+ .file_footer_size_in_bytes = 20,
+ .blob_metadata = {BlobMetadata{.type = "type1",
+ .source_snapshot_id = 1,
+ .source_snapshot_sequence_number = 1,
+ .fields = {1, 2, 3},
+ .properties = {{"key1", "value1"}}},
+ BlobMetadata{.type = "type2",
+ .source_snapshot_id = 2,
+ .source_snapshot_sequence_number = 2,
+ .fields = {4, 5, 6},
+ .properties = {}}}};
+
+ const std::string expected =
+ "StatisticsFile["
+ "snapshotId=123,path=test_path,fileSizeInBytes=100,fileFooterSizeInBytes=20,"
+ "blobMetadata=["
+ "BlobMetadata[type='type1',sourceSnapshotId=1,sourceSnapshotSequenceNumber=1,"
+ "fields=[1, 2, 3],properties={key1: value1}], "
+ "BlobMetadata[type='type2',sourceSnapshotId=2,sourceSnapshotSequenceNumber=2,"
+ "fields=[4, 5, 6],properties={}]"
+ "]"
+ "]";
+ EXPECT_EQ(expected, std::format("{}", statistics_file));
+}
+
+} // namespace iceberg