Skip to content

Commit

Permalink
Rename Patch structs for hack
Browse files Browse the repository at this point in the history
Summary: Inject the `edward-io.Name{name = "{original_name}PatchStructInternalDoNotUse"}` annotation  into the Patch structs to caution developers against accidental usage.

Reviewed By: vitaut

Differential Revision: D56032363

fbshipit-source-id: 45ccfd9ee09b422f9b4a3e8daabe2246f47910dc
  • Loading branch information
yoney authored and facebook-github-bot committed Apr 13, 2024
1 parent 99cfdce commit 07a0418
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 55 deletions.
16 changes: 16 additions & 0 deletions thrift/compiler/sema/patch_mutator.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,20 @@ struct StructGen {
operator t_struct&() { return generated; }
operator t_type_ref() { return generated; }

void add_hack_name(std::string name) {
const t_type* annotation = dynamic_cast<const t_type*>(
program_.scope()->find_by_uri(kHackNameUri));
assert(annotation);
auto value = t_const_value::make_map();
value->add_map(
std::make_unique<t_const_value>("name"),
std::make_unique<t_const_value>(std::move(name)));
value->set_ttype(*annotation);
auto hack_name =
std::make_unique<t_const>(&program_, annotation, "", std::move(value));
generated.add_structured_annotation(std::move(hack_name));
}

void add_frozen_exclude() {
const t_type* annotation = dynamic_cast<const t_type*>(
program_.scope()->find_by_uri(kCppFrozen2ExcludeUri));
Expand Down Expand Up @@ -453,6 +467,8 @@ t_struct& patch_generator::add_struct_patch(
const t_const& annot, t_structured& value_type) {
PatchGen gen{
{annot, gen_suffix_struct(annot, value_type, "Patch"), program_}};
// Rename it for Hack to caution developers against accidental usage
gen.add_hack_name(value_type.name() + "PatchStructInternalDoNotUse");
gen.assign(value_type);
gen.clear();
if (get_assign_only_annotation_or_null(value_type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public function getInstanceKey()[write_props]: string {
* FBStructPatch
*/
<<\ThriftTypeInfo(shape('uri' => 'test.dev/hack/ns2/FBStructPatch'))>>
class FBStructPatch implements \IThriftSyncStruct, \IThriftStructMetadata, \IThriftStructWithClearTerseFields {
class FBStructPatchStructInternalDoNotUse implements \IThriftSyncStruct, \IThriftStructMetadata, \IThriftStructWithClearTerseFields {
use \ThriftSerializationTrait;

const \ThriftStructTypes::TSpec SPEC = dict[
Expand Down Expand Up @@ -266,7 +266,7 @@ public static function fromShape(self::TConstructorShape $shape)[]: this {
}

public function getName()[]: string {
return 'FBStructPatch';
return 'FBStructPatchStructInternalDoNotUse';
}

public function clearTerseFields()[write_props]: void {
Expand Down Expand Up @@ -391,6 +391,11 @@ public static function getStructMetadata()[]: \tmeta_ThriftStruct {
public static function getAllStructuredAnnotations()[write_props]: \TStructAnnotations {
return shape(
'struct' => dict[
'\facebook\thrift\annotation\hack\Name' => \facebook\thrift\annotation\hack\Name::fromShape(
shape(
"name" => "FBStructPatchStructInternalDoNotUse",
)
),
'\facebook\thrift\annotation\cpp\Adapter' => \facebook\thrift\annotation\cpp\Adapter::fromShape(
shape(
"name" => "::apache::thrift::op::detail::StructPatchAdapter<::test::hack::ns2::FBStructPatchStruct>",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ StructMetadata<::test::fixtures::patch::MyDataPatchStruct>::gen(ThriftMetadata&
field.structured_annotations() = f.structured_annotations;
module_MyDataPatch.fields()->push_back(std::move(field));
}
module_MyDataPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("MyDataPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_MyDataPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::MyDataPatchStruct>") }, {"underlyingName", cvString("MyDataPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -374,6 +375,7 @@ StructMetadata<::test::fixtures::patch::MyDataWithCustomDefaultPatchStruct>::gen
field.structured_annotations() = f.structured_annotations;
module_MyDataWithCustomDefaultPatch.fields()->push_back(std::move(field));
}
module_MyDataWithCustomDefaultPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("MyDataWithCustomDefaultPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_MyDataWithCustomDefaultPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::MyDataWithCustomDefaultPatchStruct>") }, {"underlyingName", cvString("MyDataWithCustomDefaultPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -614,6 +616,7 @@ StructMetadata<::test::fixtures::patch::MyStructPatchStruct>::gen(ThriftMetadata
field.structured_annotations() = f.structured_annotations;
module_MyStructPatch.fields()->push_back(std::move(field));
}
module_MyStructPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("MyStructPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_MyStructPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::MyStructPatchStruct>") }, {"underlyingName", cvString("MyStructPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -902,6 +905,7 @@ StructMetadata<::test::fixtures::patch::LateDefStructPatchStruct>::gen(ThriftMet
field.structured_annotations() = f.structured_annotations;
module_LateDefStructPatch.fields()->push_back(std::move(field));
}
module_LateDefStructPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("LateDefStructPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_LateDefStructPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::LateDefStructPatchStruct>") }, {"underlyingName", cvString("LateDefStructPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -974,6 +978,7 @@ StructMetadata<::test::fixtures::patch::RecursivePatchStruct>::gen(ThriftMetadat
field.structured_annotations() = f.structured_annotations;
module_RecursivePatch.fields()->push_back(std::move(field));
}
module_RecursivePatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("RecursivePatchStructInternalDoNotUse") } }).cv_struct_ref());
module_RecursivePatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::RecursivePatchStruct>") }, {"underlyingName", cvString("RecursivePatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -1094,6 +1099,7 @@ StructMetadata<::test::fixtures::patch::BarPatchStruct>::gen(ThriftMetadata& met
field.structured_annotations() = f.structured_annotations;
module_BarPatch.fields()->push_back(std::move(field));
}
module_BarPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("BarPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_BarPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::BarPatchStruct>") }, {"underlyingName", cvString("BarPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -1190,6 +1196,7 @@ StructMetadata<::test::fixtures::patch::LoopPatchStruct>::gen(ThriftMetadata& me
field.structured_annotations() = f.structured_annotations;
module_LoopPatch.fields()->push_back(std::move(field));
}
module_LoopPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("LoopPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_LoopPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::AssignPatchAdapter<::test::fixtures::patch::LoopPatchStruct>") }, {"underlyingName", cvString("LoopPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down Expand Up @@ -1238,6 +1245,7 @@ StructMetadata<::test::fixtures::patch::RefFieldsPatchStruct>::gen(ThriftMetadat
field.structured_annotations() = f.structured_annotations;
module_RefFieldsPatch.fields()->push_back(std::move(field));
}
module_RefFieldsPatch.structured_annotations()->push_back(*cvStruct("hack.Name", { {"name", cvString("RefFieldsPatchStructInternalDoNotUse") } }).cv_struct_ref());
module_RefFieldsPatch.structured_annotations()->push_back(*cvStruct("cpp.Adapter", { {"name", cvString("::apache::thrift::op::detail::StructPatchAdapter<::test::fixtures::patch::RefFieldsPatchStruct>") }, {"underlyingName", cvString("RefFieldsPatchStruct") }, {"extraNamespace", cvString("") } }).cv_struct_ref());
return res.first->second;
}
Expand Down
Loading

0 comments on commit 07a0418

Please sign in to comment.