Skip to content

Commit a7e216e

Browse files
thedavekwonfacebook-github-bot
authored andcommitted
Introduce SourceIndexedTypeSystem::toSerializeableTypeSystem
Summary: populate SerializableThriftSourceInfo Reviewed By: praihan Differential Revision: D79908882 fbshipit-source-id: 05dba5e84a57567486b6103846b9867803ac1eba
1 parent b8024bf commit a7e216e

File tree

3 files changed

+50
-0
lines changed

3 files changed

+50
-0
lines changed

third-party/thrift/src/thrift/lib/cpp2/dynamic/TypeSystem.cpp

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,26 @@ SerializableTypeSystem TypeSystem::toSerializableTypeSystem(
524524
return result;
525525
}
526526

527+
SerializableTypeSystem SourceIndexedTypeSystem::toSerializableTypeSystem(
528+
const folly::F14FastSet<Uri>& uris) const {
529+
SerializableTypeSystem result;
530+
for (const auto& uri : uris) {
531+
auto def = getUserDefinedTypeOrThrow(uri);
532+
533+
SerializableTypeDefinitionEntry entry;
534+
entry.definition() = toSerializableDefinition(def);
535+
auto sourceInfoView = getSourceIdentiferForUserDefinedType(def);
536+
if (sourceInfoView.has_value()) {
537+
auto& s = entry.sourceInfo().ensure();
538+
s.locator() = sourceInfoView->location;
539+
s.name() = sourceInfoView->name;
540+
};
541+
result.types()->emplace(uri, std::move(entry));
542+
}
543+
544+
return result;
545+
}
546+
527547
} // namespace apache::thrift::type_system
528548

529549
std::size_t std::hash<apache::thrift::type_system::DefinitionRef>::operator()(

third-party/thrift/src/thrift/lib/cpp2/dynamic/TypeSystem.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -490,6 +490,12 @@ class SourceIndexedTypeSystem : public TypeSystem {
490490
*/
491491
virtual NameToDefinitionsMap getUserDefinedTypesAtLocation(
492492
std::string_view location) const = 0;
493+
494+
/**
495+
* Creates SerializbleTypeSystem from this TypeSystem.
496+
*/
497+
SerializableTypeSystem toSerializableTypeSystem(
498+
const folly::F14FastSet<Uri>& uris) const;
493499
};
494500

495501
/**

third-party/thrift/src/thrift/lib/cpp2/dynamic/test/TypeSystemTest.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1349,6 +1349,30 @@ TEST(TypeSystemTest, SourceIndexedTypeSystem) {
13491349
&typeSystem->getUserDefinedType("meta.com/thrift/test/OpaqueAlias")
13501350
->asOpaqueAlias());
13511351
}
1352+
1353+
auto sts = sym.toSerializableTypeSystem(*sym.getKnownUris());
1354+
EXPECT_EQ(sts.types()->size(), 4);
1355+
auto& structWithI32FieldEntry =
1356+
sts.types()->at("meta.com/thrift/test/StructWithI32Field");
1357+
EXPECT_EQ(
1358+
structWithI32FieldEntry.sourceInfo()->locator(), "file://foo/bar.thrift");
1359+
EXPECT_EQ(structWithI32FieldEntry.sourceInfo()->name(), "StructWithI32Field");
1360+
1361+
auto& enumEntry = sts.types()->at("meta.com/thrift/test/Enum");
1362+
EXPECT_EQ(enumEntry.sourceInfo()->locator(), "file://foo/bar.thrift");
1363+
EXPECT_EQ(enumEntry.sourceInfo()->name(), "Enum");
1364+
1365+
auto& unionWithI32FieldEntry =
1366+
sts.types()->at("meta.com/thrift/test/UnionWithI32Field");
1367+
EXPECT_EQ(
1368+
unionWithI32FieldEntry.sourceInfo()->locator(),
1369+
"file://foo/other.thrift");
1370+
EXPECT_EQ(unionWithI32FieldEntry.sourceInfo()->name(), "UnionWithI32Field");
1371+
1372+
auto& opaqueAliasEntry = sts.types()->at("meta.com/thrift/test/OpaqueAlias");
1373+
EXPECT_EQ(
1374+
opaqueAliasEntry.sourceInfo()->locator(), "file://foo/other.thrift");
1375+
EXPECT_EQ(opaqueAliasEntry.sourceInfo()->name(), "OpaqueAlias");
13521376
}
13531377

13541378
TEST(TypeSystemTest, SourceIndexedTypeSystemWithDuplicateEntries) {

0 commit comments

Comments
 (0)