Skip to content

Commit

Permalink
Add Thrift Patch support for cpp.Type of std::unique_ptr<folly::IOBuf>
Browse files Browse the repository at this point in the history
Summary: Add support for `cpp.type = std::unique_ptr<folly::IOBuf>`. This type is special in Thrift where it expects to work with all features provided by Thrift.

Reviewed By: Mizuchi

Differential Revision: D70417418

fbshipit-source-id: c1cc8ec77c0ebed66b3725ea4ed2ae9f016f9be4
  • Loading branch information
thedavekwon authored and facebook-github-bot committed Mar 1, 2025
1 parent 0373011 commit a9a8789
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
6 changes: 6 additions & 0 deletions thrift/lib/cpp2/op/detail/StructPatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,12 @@ class BaseEnsurePatch : public BaseClearPatch<Patch, Derived> {
}
}
}
template <typename Id>
std::enable_if_t<type::is_optional_or_union_field_v<T, Id>> ensure(
const std::unique_ptr<folly::IOBuf>& defaultVal) {
ensure(std::make_unique<folly::IOBuf>(*defaultVal));
}

/// Ensures the given field is initalized, and return the associated patch
/// object.
template <class Id>
Expand Down
9 changes: 9 additions & 0 deletions thrift/lib/cpp2/op/detail/ValuePatch.h
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,8 @@ class BinaryPatch : public BaseStringPatch<Patch, BinaryPatch<Patch>> {
void assign(std::string s) {
assign(*folly::IOBuf::fromString(std::move(s)));
}
void assign(std::unique_ptr<folly::IOBuf>&& s) { assign(std::move(*s)); }
void assign(const std::unique_ptr<folly::IOBuf>& s) { assign(*s); }

template <typename T>
BinaryPatch& operator=(T&& other) {
Expand Down Expand Up @@ -427,6 +429,13 @@ class BinaryPatch : public BaseStringPatch<Patch, BinaryPatch<Patch>> {
val = buf->toString();
}

void apply(std::unique_ptr<folly::IOBuf>& val) const {
if (!val) {
val = std::make_unique<folly::IOBuf>();
}
apply(*val);
}

private:
using Base::assignOr;
using Base::data_;
Expand Down

0 comments on commit a9a8789

Please sign in to comment.