Skip to content

Commit 2187b59

Browse files
Alfred Fullerfacebook-github-bot
authored andcommitted
Add clear support to NumberPatch
Differential Revision: D39161903 fbshipit-source-id: 1c7e2aa33a600b4bcdb48ce7b544c61e183beb59
1 parent 0f89389 commit 2187b59

File tree

4 files changed

+57
-8
lines changed

4 files changed

+57
-8
lines changed

third-party/thrift/src/thrift/lib/cpp2/op/detail/ValuePatch.h

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,13 @@ class BoolPatch : public BaseClearPatch<Patch, BoolPatch<Patch>> {
9696

9797
// Patch must have the following fields:
9898
// optional T assign;
99+
// bool clear;
99100
// T add;
100101
template <typename Patch>
101-
class NumberPatch : public BaseAssignPatch<Patch, NumberPatch<Patch>> {
102-
using Base = BaseAssignPatch<Patch, NumberPatch>;
102+
class NumberPatch : public BaseClearPatch<Patch, NumberPatch<Patch>> {
103+
using Base = BaseClearPatch<Patch, NumberPatch>;
103104
using T = typename Base::value_type;
105+
using Tag = type::infer_tag<T>;
104106

105107
public:
106108
using Base::apply;
@@ -131,14 +133,14 @@ class NumberPatch : public BaseAssignPatch<Patch, NumberPatch<Patch>> {
131133
}
132134

133135
void apply(T& val) const {
134-
if (!applyAssign(val)) {
136+
if (!Base::template applyAssignAndClear<Tag>(val)) {
135137
val += *data_.add();
136138
}
137139
}
138140

139141
template <typename U>
140142
void merge(U&& next) {
141-
if (!mergeAssign(std::forward<U>(next))) {
143+
if (!mergeAssignAndClear(std::forward<U>(next))) {
142144
*data_.add() += *next.toThrift().add();
143145
}
144146
}
@@ -156,10 +158,9 @@ class NumberPatch : public BaseAssignPatch<Patch, NumberPatch<Patch>> {
156158
}
157159

158160
private:
159-
using Base::applyAssign;
160161
using Base::assignOr;
161162
using Base::data_;
162-
using Base::mergeAssign;
163+
using Base::mergeAssignAndClear;
163164

164165
template <typename U>
165166
friend NumberPatch operator+(NumberPatch lhs, U&& rhs) {

third-party/thrift/src/thrift/lib/cpp2/protocol/Patch.cpp

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,16 @@ bool applyAssign(const Object& patch, value_native_type<Tag>& value) {
104104
template <typename Tag, typename T>
105105
void applyNumericPatch(const Object& patch, T& value) {
106106
constexpr auto valueType = static_cast<Value::Type>(type::base_type_v<Tag>);
107-
checkOps(patch, valueType, {PatchOp::Assign, PatchOp::Add});
107+
checkOps(patch, valueType, {PatchOp::Assign, PatchOp::Clear, PatchOp::Add});
108108
if (applyAssign<Tag>(patch, value)) {
109109
return; // Ignore all other ops.
110110
}
111+
if (auto* clear = findOp(patch, PatchOp::Clear)) {
112+
if (argAs<type::bool_t>(*clear)) {
113+
value = {};
114+
}
115+
}
116+
111117
if (auto* arg = findOp(patch, PatchOp::Add)) {
112118
value += argAs<Tag>(*arg);
113119
}

third-party/thrift/src/thrift/lib/thrift/patch.thrift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,9 @@ struct BytePatch {
8080
*/
8181
1: optional byte assign;
8282

83+
/** Clear any set value. */
84+
2: bool clear;
85+
8386
/** Add to a given value. */
8487
8: byte add;
8588
}
@@ -99,6 +102,9 @@ struct I16Patch {
99102
*/
100103
1: optional i16 assign;
101104

105+
/** Clear any set value. */
106+
2: bool clear;
107+
102108
/** Add to a given value. */
103109
8: i16 add;
104110
}
@@ -118,6 +124,9 @@ struct I32Patch {
118124
*/
119125
1: optional i32 assign;
120126

127+
/** Clear any set value. */
128+
2: bool clear;
129+
121130
/** Add to a given value. */
122131
8: i32 add;
123132
}
@@ -137,6 +146,9 @@ struct I64Patch {
137146
*/
138147
1: optional i64 assign;
139148

149+
/** Clear any set value. */
150+
2: bool clear;
151+
140152
/** Add to a given value. */
141153
8: i64 add;
142154
}
@@ -156,6 +168,9 @@ struct FloatPatch {
156168
*/
157169
1: optional float assign;
158170

171+
/** Clear any set value. */
172+
2: bool clear;
173+
159174
/** Add to a given value. */
160175
8: float add;
161176
}
@@ -175,6 +190,9 @@ struct DoublePatch {
175190
*/
176191
1: optional double assign;
177192

193+
/** Clear any set value. */
194+
2: bool clear;
195+
178196
/** Add to a given value. */
179197
8: double add;
180198
}

third-party/thrift/src/thrift/test/StructPatchTest.cpp

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ TEST(StructPatchTest, Clear) {
116116
test::expectPatch(op::StringPatch::createClear(), {"hi"}, "");
117117
}
118118

119-
TEST(StructPatchTest, ClearField_Bool) {
119+
TEST(StructPatchTest, ClearField_BoolPatch) {
120120
MyStruct actual;
121121

122122
actual.boolVal() = true;
@@ -128,6 +128,30 @@ TEST(StructPatchTest, ClearField_Bool) {
128128
EXPECT_FALSE(actual.optBoolVal().has_value());
129129
}
130130

131+
TEST(StructPatchTest, ClearField_NumberPatch) {
132+
MyStruct actual;
133+
134+
actual.i16Val() = 1;
135+
op::I16Patch::createClear().apply(actual.i16Val());
136+
EXPECT_EQ(*actual.boolVal(), 0);
137+
138+
actual.optDoubleVal() = 1;
139+
op::DoublePatch::createClear().apply(actual.optDoubleVal());
140+
EXPECT_FALSE(actual.optDoubleVal().has_value());
141+
}
142+
143+
TEST(StructPatchTest, ClearField_StringPatch) {
144+
MyStruct actual;
145+
146+
actual.stringVal() = "hi";
147+
op::StringPatch::createClear().apply(actual.stringVal());
148+
EXPECT_EQ(*actual.stringVal(), "");
149+
150+
actual.optBinaryVal().ensure();
151+
op::BinaryPatch::createClear().apply(actual.optBinaryVal());
152+
EXPECT_FALSE(actual.optBinaryVal().has_value());
153+
}
154+
131155
TEST(StructPatchTest, Patch) {
132156
MyStruct val;
133157
val.stringVal() = "hi";

0 commit comments

Comments
 (0)