Skip to content

Commit

Permalink
Add cpp.ref examples for deprecated_terse_writes
Browse files Browse the repository at this point in the history
Reviewed By: thedavekwon

Differential Revision: D68517732

fbshipit-source-id: f70c9ac34a6f3948125d6e527fe197c6f43e4aad
  • Loading branch information
TJ Yin authored and facebook-github-bot committed Jan 23, 2025
1 parent bf9f16d commit dbfa22a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
21 changes: 20 additions & 1 deletion third-party/thrift/src/thrift/test/StructTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -792,6 +792,12 @@ TEST_F(StructTest, TerseFields) {
// Custom default on union is not honored.
EXPECT_EQ(terse.union_field()->getType(), terse.union_field()->__EMPTY__);

// cpp.ref Structures
EXPECT_NE(terse.cpp_ref_struct_field(), nullptr);
EXPECT_NE(terse.cpp_ref_union_field(), nullptr);
// cpp.ref exceptions are nullptr by default
EXPECT_EQ(terse.cpp_ref_exception_field(), nullptr);

// Numeric fields are not serialized if they equal custom default
EXPECT_FALSE(serializedField<ident::bool_field>(terse));
EXPECT_FALSE(serializedField<ident::byte_field>(terse));
Expand All @@ -813,9 +819,19 @@ TEST_F(StructTest, TerseFields) {
EXPECT_TRUE(serializedField<ident::struct_field>(terse));
EXPECT_TRUE(serializedField<ident::union_field>(terse));
EXPECT_TRUE(serializedField<ident::exception_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_ref_struct_field>(terse));
EXPECT_TRUE(serializedField<ident::cpp_ref_union_field>(terse));

// cpp.ref exceptions are nullptr by default
EXPECT_FALSE(serializedField<ident::cpp_ref_exception_field>(terse));
terse.cpp_ref_exception_field() = std::make_unique<NestedException>();
EXPECT_TRUE(serializedField<ident::cpp_ref_exception_field>(terse));

// Change `terse` to intrinsic default
apache::thrift::clear(terse);
terse.cpp_ref_struct_field() = nullptr;
terse.cpp_ref_union_field() = nullptr;
terse.cpp_ref_exception_field() = nullptr;

// Numeric fields are serialized if they don't equal custom default
EXPECT_TRUE(serializedField<ident::bool_field>(terse));
Expand All @@ -838,6 +854,9 @@ TEST_F(StructTest, TerseFields) {
EXPECT_TRUE(serializedField<ident::struct_field>(terse));
EXPECT_TRUE(serializedField<ident::union_field>(terse));
EXPECT_TRUE(serializedField<ident::exception_field>(terse));
}

EXPECT_FALSE(serializedField<ident::cpp_ref_struct_field>(terse));
EXPECT_FALSE(serializedField<ident::cpp_ref_union_field>(terse));
EXPECT_FALSE(serializedField<ident::cpp_ref_exception_field>(terse));
}
} // namespace
10 changes: 10 additions & 0 deletions third-party/thrift/src/thrift/test/structs_terse.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -125,4 +125,14 @@ struct TerseFieldsWithCustomDefault {
13: NestedStruct struct_field;
14: NestedUnion union_field;
15: NestedException exception_field;

@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
16: NestedStruct cpp_ref_struct_field;
@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
17: NestedUnion cpp_ref_union_field;
@cpp.Ref{type = cpp.RefType.Unique}
@cpp.AllowLegacyNonOptionalRef
18: NestedException cpp_ref_exception_field;
}

0 comments on commit dbfa22a

Please sign in to comment.