This is an automated email from the ASF dual-hosted git repository. jensg pushed a commit to branch 0.22.0 in repository https://gitbox.apache.org/repos/asf/thrift.git
commit 3da784bec37a9080ca2fee2fc2cac19f1c1efa25 Author: Carel Combrink <[email protected]> AuthorDate: Thu May 15 12:22:37 2025 +0000 THRIFT-5868: UUID Support for TCompactProtocol Client: cpp Patch: Carel Combrink This closes #3137 --- lib/cpp/src/thrift/protocol/TCompactProtocol.h | 4 ++++ lib/cpp/src/thrift/protocol/TCompactProtocol.tcc | 27 ++++++++++++++++++++++-- lib/cpp/test/AllProtocolTests.tcc | 4 ++++ lib/cpp/test/GenericHelpers.h | 13 ++++++++++++ 4 files changed, 46 insertions(+), 2 deletions(-) diff --git a/lib/cpp/src/thrift/protocol/TCompactProtocol.h b/lib/cpp/src/thrift/protocol/TCompactProtocol.h index 81db1f67b..c7d81eea6 100644 --- a/lib/cpp/src/thrift/protocol/TCompactProtocol.h +++ b/lib/cpp/src/thrift/protocol/TCompactProtocol.h @@ -140,6 +140,8 @@ public: uint32_t writeBinary(const std::string& str); + uint32_t writeUUID(const TUuid& str); + int getMinSerializedSize(TType type) override; void checkReadBytesAvailable(TSet& set) override @@ -213,6 +215,8 @@ public: uint32_t readBinary(std::string& str); + uint32_t readUUID(TUuid& str); + /* *These methods are here for the struct to call, but don't have any wire * encoding. diff --git a/lib/cpp/src/thrift/protocol/TCompactProtocol.tcc b/lib/cpp/src/thrift/protocol/TCompactProtocol.tcc index a872c23db..b57568f8d 100644 --- a/lib/cpp/src/thrift/protocol/TCompactProtocol.tcc +++ b/lib/cpp/src/thrift/protocol/TCompactProtocol.tcc @@ -61,10 +61,11 @@ enum Types { CT_LIST = 0x09, CT_SET = 0x0A, CT_MAP = 0x0B, - CT_STRUCT = 0x0C + CT_STRUCT = 0x0C, + CT_UUID = 0x0D }; -const int8_t TTypeToCType[16] = { +const int8_t TTypeToCType[17] = { CT_STOP, // T_STOP 0, // unused CT_BOOLEAN_TRUE, // T_BOOL @@ -81,6 +82,7 @@ const int8_t TTypeToCType[16] = { CT_MAP, // T_MAP CT_SET, // T_SET CT_LIST, // T_LIST + CT_UUID, // T_UUID }; }} // end detail::compact namespace @@ -286,6 +288,15 @@ uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) { return wsize; } +/** + * Write a TUuid to the wire + */ +template <class Transport_> +uint32_t TCompactProtocolT<Transport_>::writeUUID(const TUuid& uuid) { + trans_->write(uuid.data(), uuid.size()); + return uuid.size(); +} + // // Internal Writing methods // @@ -719,6 +730,15 @@ uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) { return rsize + static_cast<uint32_t>(size); } + +/** + * Read a TUuid from the wire. + */ +template <class Transport_> +uint32_t TCompactProtocolT<Transport_>::readUUID(TUuid& uuid) { + return trans_->readAll(uuid.begin(), uuid.size()); +} + /** * Read an i32 from the wire as a varint. The MSB of each byte is set * if there is another byte to follow. This can read up to 5 bytes. @@ -826,6 +846,8 @@ TType TCompactProtocolT<Transport_>::getTType(int8_t type) { return T_MAP; case detail::compact::CT_STRUCT: return T_STRUCT; + case detail::compact::CT_UUID: + return T_UUID; default: throw TException(std::string("don't know what type: ") + static_cast<char>(type)); } @@ -850,6 +872,7 @@ int TCompactProtocolT<Transport_>::getMinSerializedSize(TType type) case T_MAP: return sizeof(int8_t); // element count case T_SET: return sizeof(int8_t); // element count case T_LIST: return sizeof(int8_t); // element count + case T_UUID: return 16; // 16 bytes default: throw TProtocolException(TProtocolException::UNKNOWN, "unrecognized type code"); } } diff --git a/lib/cpp/test/AllProtocolTests.tcc b/lib/cpp/test/AllProtocolTests.tcc index 80a4ea097..cb98917d3 100644 --- a/lib/cpp/test/AllProtocolTests.tcc +++ b/lib/cpp/test/AllProtocolTests.tcc @@ -25,6 +25,7 @@ #include <thrift/protocol/TBinaryProtocol.h> #include <thrift/transport/TBufferTransports.h> #include <thrift/Thrift.h> +#include <thrift/TUuid.h> #include "GenericHelpers.h" @@ -208,6 +209,9 @@ void testProtocol(const char* protoname) { testNaked<TProto, std::string>("a bit longer than the smallest possible"); testNaked<TProto, std::string>("\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA"); // kinda binary test + testNaked<TProto, TUuid>(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")); + testField<TProto, T_UUID, TUuid>(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c")); + testField<TProto, T_STRING, std::string>(""); testField<TProto, T_STRING, std::string>("short"); testField<TProto, T_STRING, std::string>("borderlinetiny"); diff --git a/lib/cpp/test/GenericHelpers.h b/lib/cpp/test/GenericHelpers.h index bcef9f242..1eed4e2b5 100644 --- a/lib/cpp/test/GenericHelpers.h +++ b/lib/cpp/test/GenericHelpers.h @@ -23,6 +23,7 @@ #include <thrift/protocol/TProtocol.h> #include <memory> #include <thrift/Thrift.h> +#include <thrift/TUuid.h> /* ClassName Helper for cleaner exceptions */ class ClassNames { @@ -57,6 +58,10 @@ template <> const char* ClassNames::getName<std::string>() { return "string"; } +template <> +const char* ClassNames::getName<apache::thrift::TUuid>() { + return "uuid"; +} /* Generic Protocol I/O function for tests */ class GenericIO { @@ -87,6 +92,10 @@ public: return proto->writeString(val); } + static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const apache::thrift::TUuid& val) { + return proto->writeUUID(val); + } + /* Read functions */ static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int8_t& val) { return proto->readByte(val); } @@ -102,6 +111,10 @@ public: static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) { return proto->readString(val); } + + static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, apache::thrift::TUuid& val) { + return proto->readUUID(val); + } }; #endif
