Skip to content

Commit

Permalink
Make InferTag SFINAE-friendly
Browse files Browse the repository at this point in the history
Reviewed By: iahs

Differential Revision: D63038626

fbshipit-source-id: 404d84879b765b630eff6a2bc1b5a78277a9fd65
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Sep 25, 2024
1 parent ef25d8e commit 5fcf01b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 3 deletions.
17 changes: 17 additions & 0 deletions thrift/lib/cpp2/op/StdSerializerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,5 +89,22 @@ TEST(StdSerializerTest, PrimaryTypes) {
FBTHRIFT_SCOPED_CHECK((testRoundTrip<std::string, type::binary_t>("hi")));
}

TEST(StdSerializerTest, ContainerWithString) {
using namespace type;
using Tag = map<string_t, list<set<string_t>>>;
apache::thrift::op::
StdSerializer<Tag, apache::thrift::type::StandardProtocol::Compact>
serializer;
auto& registry = apache::thrift::type::detail::getGeneratedTypeRegistry();
registry.registerSerializer(serializer, Tag{});
std::map<std::string, std::vector<std::set<std::string>>> myData;
myData["foo"] = {{"1"}, {"2"}};
auto anyData =
registry.store<apache::thrift::type::StandardProtocol::Compact>(
apache::thrift::type::ConstRef(Tag{}, myData));
auto myData2 = registry.load(anyData).as<Tag>();
EXPECT_EQ(myData, myData2);
}

} // namespace
} // namespace apache::thrift::op
20 changes: 17 additions & 3 deletions thrift/lib/cpp2/type/detail/NativeType.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,15 +185,23 @@ template <typename VTag>
struct NativeTypes<type::list<VTag>> : parameterized_type<std::vector, VTag> {};

template <typename T, bool GuessStringTag>
struct InferTag<std::vector<T>, GuessStringTag> {
struct InferTag<
std::vector<T>,
GuessStringTag,
// Make InferTag SFINAE-friendly
folly::void_t<typename InferTag<T, GuessStringTag>::type>> {
using type = type::list<typename InferTag<T, GuessStringTag>::type>;
};

// Traits for sets.
template <typename KTag>
struct NativeTypes<set<KTag>> : parameterized_type<std::set, KTag> {};
template <typename T, bool GuessStringTag>
struct InferTag<std::set<T>, GuessStringTag> {
struct InferTag<
std::set<T>,
GuessStringTag,
// Make InferTag SFINAE-friendly
folly::void_t<typename InferTag<T, GuessStringTag>::type>> {
using type = type::set<typename InferTag<T, GuessStringTag>::type>;
};

Expand All @@ -202,7 +210,13 @@ template <typename KTag, typename VTag>
struct NativeTypes<map<KTag, VTag>> : parameterized_type<std::map, KTag, VTag> {
};
template <typename K, typename V, bool GuessStringTag>
struct InferTag<std::map<K, V>, GuessStringTag> {
struct InferTag<
std::map<K, V>,
GuessStringTag,
folly::void_t<
// Make InferTag SFINAE-friendly
typename InferTag<K, GuessStringTag>::type,
typename InferTag<V, GuessStringTag>::type>> {
using type = type::map<
typename InferTag<K, GuessStringTag>::type,
typename InferTag<V, GuessStringTag>::type>;
Expand Down

0 comments on commit 5fcf01b

Please sign in to comment.