Skip to content

Commit b3d41c2

Browse files
iahsfacebook-github-bot
authored andcommitted
Allow converting individual nodes to TypeSystem
Summary: Avoids the need to roundtrip from a definition node to its URI to convert between systems, which also adds support for nodes without URIs. Accessing the URI of a node that does not have one is changed to throw, which will ensure we don't accidentally serialize it as part of the type system, use it to create an Any, or otherwise leak the partial nodes. Reviewed By: pranavtbhat Differential Revision: D75178791 fbshipit-source-id: c09b43ec6c2a190c5749fb2c22e866e51f152c0b
1 parent 892fe42 commit b3d41c2

File tree

5 files changed

+48
-10
lines changed

5 files changed

+48
-10
lines changed

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,14 @@ std::string_view kindToString(TypeRef::Kind k) noexcept {
319319
folly::assume_unreachable();
320320
}
321321

322-
const Uri& DefinitionRef::uri() const noexcept {
322+
const Uri& DefinitionNode::uri() const {
323+
if (uri_.empty()) {
324+
folly::throw_exception<InvalidTypeError>("Type does not have URI set.");
325+
}
326+
return uri_;
327+
}
328+
329+
const Uri& DefinitionRef::uri() const {
323330
return visit(std::mem_fn(&DefinitionNode::uri));
324331
}
325332

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,11 @@ struct FastFieldHandle {
567567

568568
class DefinitionNode {
569569
public:
570-
const Uri& uri() const noexcept { return uri_; }
570+
/**
571+
* Returns the URI associated with this definition.
572+
* Throws `InvalidTypeError` if the type does not have a URI.
573+
*/
574+
const Uri& uri() const;
571575

572576
protected:
573577
Uri uri_;
@@ -790,10 +794,10 @@ class DefinitionRef final {
790794
}
791795

792796
/**
793-
* Returns the URI associated with this definition. Since all user-defined
794-
* types must have URIs, this function is infallible.
797+
* Returns the URI associated with this definition.
798+
* Throws `InvalidTypeError` if the type does not have a URI.
795799
*/
796-
const Uri& uri() const noexcept;
800+
const Uri& uri() const;
797801

798802
/**
799803
* An `std::visit`-like API for pattern-matching on the active variant

third-party/thrift/src/thrift/lib/cpp2/schema/SyntaxGraph.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,6 @@ class TypeSystemFacade final : public type_system::TypeSystem {
712712
return {};
713713
}
714714

715-
private:
716715
// Convert the definition to TypeSystem's representation.
717716
// The approach is:
718717
// 1. Traverse the root definition node's fields to gather the set of
@@ -855,6 +854,7 @@ class TypeSystemFacade final : public type_system::TypeSystem {
855854
});
856855
}
857856

857+
private:
858858
type_system::TypeRef convertType(const TypeRef& type) {
859859
return type.trueType().visit(
860860
[](const Primitive& primitive) {
@@ -933,6 +933,24 @@ type_system::TypeSystem& SyntaxGraph::asTypeSystem() {
933933
"SyntaxGraph instance does not support URI-based lookup");
934934
}
935935

936+
type_system::DefinitionRef SyntaxGraph::asTypeSystemDefinitionRef(
937+
const DefinitionNode& node) {
938+
auto& facade = static_cast<TypeSystemFacade&>(asTypeSystem());
939+
return facade.convertUserDefinedType(&node);
940+
}
941+
const type_system::StructNode& SyntaxGraph::asTypeSystemStructNode(
942+
const StructNode& node) {
943+
return asTypeSystemDefinitionRef(node.definition()).asStruct();
944+
}
945+
const type_system::UnionNode& SyntaxGraph::asTypeSystemUnionNode(
946+
const UnionNode& node) {
947+
return asTypeSystemDefinitionRef(node.definition()).asUnion();
948+
}
949+
const type_system::EnumNode& SyntaxGraph::asTypeSystemEnumNode(
950+
const EnumNode& node) {
951+
return asTypeSystemDefinitionRef(node.definition()).asEnum();
952+
}
953+
936954
} // namespace apache::thrift::syntax_graph
937955

938956
#endif // THRIFT_SCHEMA_AVAILABLE

third-party/thrift/src/thrift/lib/cpp2/schema/SyntaxGraph.h

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,12 +45,9 @@
4545
#include <vector>
4646

4747
#include <thrift/common/tree_printer.h>
48+
#include <thrift/lib/cpp2/dynamic/TypeSystem.h>
4849
#include <thrift/lib/cpp2/schema/gen-cpp2/syntax_graph_types.h>
4950

50-
namespace apache::thrift::type_system {
51-
class TypeSystem;
52-
}
53-
5451
namespace apache::thrift::syntax_graph {
5552

5653
/**
@@ -1575,6 +1572,17 @@ class SyntaxGraph final : public detail::WithDebugPrinting<SyntaxGraph> {
15751572
*/
15761573
type_system::TypeSystem& asTypeSystem();
15771574

1575+
/**
1576+
* Allows converting a SyntaxGraph node into its corresponding TypeSystem
1577+
* node. Equivalent to `asTypeSystem().getUserDefinedType(node.uri())`, but
1578+
* more efficient and supports nodes without URIs.
1579+
*/
1580+
type_system::DefinitionRef asTypeSystemDefinitionRef(
1581+
const DefinitionNode& node);
1582+
const type_system::StructNode& asTypeSystemStructNode(const StructNode& node);
1583+
const type_system::UnionNode& asTypeSystemUnionNode(const UnionNode& node);
1584+
const type_system::EnumNode& asTypeSystemEnumNode(const EnumNode& node);
1585+
15781586
explicit SyntaxGraph(std::unique_ptr<detail::Resolver> resolver);
15791587

15801588
void printTo(

third-party/thrift/src/thrift/lib/cpp2/schema/test/SyntaxGraphTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -711,6 +711,7 @@ TEST_F(ServiceSchemaTest, asTypeSystem) {
711711
EXPECT_EQ(structNode.fields()[0].identity().id(), FieldId{1});
712712
EXPECT_EQ(structNode.fields()[0].type().asStruct().uri(), uri);
713713
EXPECT_EQ(&structNode.fields()[0].type().asStruct(), &structNode);
714+
EXPECT_EQ(&syntaxGraph.asTypeSystemStructNode(def->asStruct()), &structNode);
714715

715716
uri = "meta.com/thrift_test/TestEnum";
716717
const type_system::EnumNode& enumNode =

0 commit comments

Comments
 (0)