Skip to content

Commit dc18eab

Browse files
praihanfacebook-github-bot
authored andcommitted
Add ergonomic accessors to TypeSystem
Reviewed By: sadroeck, pranavtbhat Differential Revision: D79479781 fbshipit-source-id: 9f019b71b5875d73bd63ca712c1ecd659e2fb913
1 parent 1acd89b commit dc18eab

File tree

3 files changed

+139
-20
lines changed

3 files changed

+139
-20
lines changed

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

Lines changed: 122 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ class TypeSystem {
200200
* Resolves an arbitrary TypeId ito a TypeRef.
201201
*
202202
* Throws:
203-
* - InvalidTypeError if the TypeId references user-defined tyeps that are
203+
* - InvalidTypeError if the TypeId references user-defined types that are
204204
* not defined in the type system.
205205
*/
206206
TypeRef resolveTypeId(const TypeId& typeId) const;
@@ -218,6 +218,71 @@ class TypeSystem {
218218
* serializable.
219219
*/
220220
virtual std::optional<folly::F14FastSet<Uri>> getKnownUris() const = 0;
221+
222+
/**
223+
* Creates a TypeRef for the `bool` type — either true or false.
224+
*/
225+
static TypeRef Bool() noexcept;
226+
/**
227+
* Creates a TypeRef for the `byte` type — 8-bit signed integer.
228+
*/
229+
static TypeRef Byte() noexcept;
230+
/**
231+
* Creates a TypeRef for the `i16` type — 16-bit signed integer.
232+
*/
233+
static TypeRef I16() noexcept;
234+
/**
235+
* Creates a TypeRef for the `i32` type — 32-bit signed integer.
236+
*/
237+
static TypeRef I32() noexcept;
238+
/**
239+
* Creates a TypeRef for the `i64` type — 64-bit signed integer.
240+
*/
241+
static TypeRef I64() noexcept;
242+
/**
243+
* Creates a TypeRef for the `float` type — IEEE 754 binary32.
244+
*/
245+
static TypeRef Float() noexcept;
246+
/**
247+
* Creates a TypeRef for the `double` type — IEEE 754 binary64.
248+
*/
249+
static TypeRef Double() noexcept;
250+
/**
251+
* Creates a TypeRef for the `string` type — UTF-8 encoded bytes.
252+
*/
253+
static TypeRef String() noexcept;
254+
/**
255+
* Creates a TypeRef for the `binary` type — a sequence of bytes.
256+
*/
257+
static TypeRef Binary() noexcept;
258+
/**
259+
* Creates a TypeRef for the `any` type — a type-erased Thrift value.
260+
*/
261+
static TypeRef Any() noexcept;
262+
263+
/**
264+
* Creates a TypeRef for a `list` type.
265+
* The provided element type must from the same type system.
266+
*/
267+
TypeRef ListOf(TypeRef elementType) const;
268+
/**
269+
* Creates a TypeRef for a `set` type.
270+
* The provided element type must from the same type system.
271+
*/
272+
TypeRef SetOf(TypeRef elementType) const;
273+
/**
274+
* Creates a TypeRef for a `list` type.
275+
* The provided key and value types must from the same type system.
276+
*/
277+
TypeRef MapOf(TypeRef keyType, TypeRef valueType) const;
278+
279+
/**
280+
* Creates a TypeRef for a user-defined type.
281+
*
282+
* Throws:
283+
* - InvalidTypeError if the type is not defined in the type system.
284+
*/
285+
TypeRef UserDefined(UriView) const;
221286
};
222287

223288
/**
@@ -997,6 +1062,62 @@ template <typename K, typename V>
9971062

9981063
} // namespace detail
9991064

1065+
/* static */ inline TypeRef TypeSystem::Bool() noexcept {
1066+
return TypeRef(TypeRef::Bool());
1067+
}
1068+
1069+
/* static */ inline TypeRef TypeSystem::Byte() noexcept {
1070+
return TypeRef(TypeRef::Byte());
1071+
}
1072+
1073+
/* static */ inline TypeRef TypeSystem::I16() noexcept {
1074+
return TypeRef(TypeRef::I16());
1075+
}
1076+
1077+
/* static */ inline TypeRef TypeSystem::I32() noexcept {
1078+
return TypeRef(TypeRef::I32());
1079+
}
1080+
1081+
/* static */ inline TypeRef TypeSystem::I64() noexcept {
1082+
return TypeRef(TypeRef::I64());
1083+
}
1084+
1085+
/* static */ inline TypeRef TypeSystem::Float() noexcept {
1086+
return TypeRef(TypeRef::Float());
1087+
}
1088+
1089+
/* static */ inline TypeRef TypeSystem::Double() noexcept {
1090+
return TypeRef(TypeRef::Double());
1091+
}
1092+
1093+
/* static */ inline TypeRef TypeSystem::String() noexcept {
1094+
return TypeRef(TypeRef::String());
1095+
}
1096+
1097+
/* static */ inline TypeRef TypeSystem::Binary() noexcept {
1098+
return TypeRef(TypeRef::Binary());
1099+
}
1100+
1101+
/* static */ inline TypeRef TypeSystem::Any() noexcept {
1102+
return TypeRef(TypeRef::Any());
1103+
}
1104+
1105+
inline TypeRef TypeSystem::ListOf(TypeRef elementType) const {
1106+
return TypeRef(TypeRef::List::of(std::move(elementType)));
1107+
}
1108+
1109+
inline TypeRef TypeSystem::SetOf(TypeRef elementType) const {
1110+
return TypeRef(TypeRef::Set::of(std::move(elementType)));
1111+
}
1112+
1113+
inline TypeRef TypeSystem::MapOf(TypeRef keyType, TypeRef valueType) const {
1114+
return TypeRef(TypeRef::Map::of(std::move(keyType), std::move(valueType)));
1115+
}
1116+
1117+
inline TypeRef TypeSystem::UserDefined(UriView uri) const {
1118+
return TypeRef::fromDefinition(this->getUserDefinedTypeOrThrow(uri));
1119+
}
1120+
10001121
} // namespace apache::thrift::type_system
10011122

10021123
namespace std {

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -135,14 +135,14 @@ class TypeSystemImpl final : public SourceIndexedTypeSystem {
135135
fmt::format("Definition for uri '{}' was not found", uri));
136136
},
137137
[&](const TypeId::List& list) -> TypeRef {
138-
return TypeRef(TypeRef::List(typeOf(list.elementType())));
138+
return TypeSystem::ListOf(typeOf(list.elementType()));
139139
},
140140
[&](const TypeId::Set& set) -> TypeRef {
141-
return TypeRef(TypeRef::Set(typeOf(set.elementType())));
141+
return TypeSystem::SetOf(typeOf(set.elementType()));
142142
},
143143
[&](const TypeId::Map& map) -> TypeRef {
144-
return TypeRef(
145-
TypeRef::Map(typeOf(map.keyType()), typeOf(map.valueType())));
144+
return TypeSystem::MapOf(
145+
typeOf(map.keyType()), typeOf(map.valueType()));
146146
},
147147
[](const auto& primitive) -> TypeRef { return TypeRef(primitive); });
148148
}

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

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -912,23 +912,23 @@ class TypeSystemFacade final : public type_system::TypeSystem {
912912
[](const Primitive& primitive) {
913913
switch (primitive) {
914914
case Primitive::BOOL:
915-
return type_system::TypeRef{type_system::TypeRef::Bool{}};
915+
return type_system::TypeSystem::Bool();
916916
case Primitive::BYTE:
917-
return type_system::TypeRef{type_system::TypeRef::Byte{}};
917+
return type_system::TypeSystem::Byte();
918918
case Primitive::I16:
919-
return type_system::TypeRef{type_system::TypeRef::I16{}};
919+
return type_system::TypeSystem::I16();
920920
case Primitive::I32:
921-
return type_system::TypeRef{type_system::TypeRef::I32{}};
921+
return type_system::TypeSystem::I32();
922922
case Primitive::I64:
923-
return type_system::TypeRef{type_system::TypeRef::I64{}};
923+
return type_system::TypeSystem::I64();
924924
case Primitive::FLOAT:
925-
return type_system::TypeRef{type_system::TypeRef::Float{}};
925+
return type_system::TypeSystem::Float();
926926
case Primitive::DOUBLE:
927-
return type_system::TypeRef{type_system::TypeRef::Double{}};
927+
return type_system::TypeSystem::Double();
928928
case Primitive::STRING:
929-
return type_system::TypeRef{type_system::TypeRef::String{}};
929+
return type_system::TypeSystem::String();
930930
case Primitive::BINARY:
931-
return type_system::TypeRef{type_system::TypeRef::Binary{}};
931+
return type_system::TypeSystem::Binary();
932932
}
933933
},
934934
[&](const StructNode& s) {
@@ -952,16 +952,14 @@ class TypeSystemFacade final : public type_system::TypeSystem {
952952
"Typedefs should have been resolved by trueType call");
953953
},
954954
[&](const List& l) {
955-
return type_system::TypeRef{
956-
type_system::TypeRef::List{convertType(l.elementType())}};
955+
return type_system::TypeSystem::ListOf(convertType(l.elementType()));
957956
},
958957
[&](const Set& s) {
959-
return type_system::TypeRef{
960-
type_system::TypeRef::Set{convertType(s.elementType())}};
958+
return type_system::TypeSystem::SetOf(convertType(s.elementType()));
961959
},
962960
[&](const Map& m) {
963-
return type_system::TypeRef{type_system::TypeRef::Map{
964-
convertType(m.keyType()), convertType(m.valueType())}};
961+
return type_system::TypeSystem::MapOf(
962+
convertType(m.keyType()), convertType(m.valueType()));
965963
});
966964
}
967965

0 commit comments

Comments
 (0)