Skip to content

Commit 3da784b

Browse files
CJCombrinkJens-G
authored andcommitted
THRIFT-5868: UUID Support for TCompactProtocol
Client: cpp Patch: Carel Combrink This closes #3137
1 parent 63eeff9 commit 3da784b

File tree

4 files changed

+46
-2
lines changed

4 files changed

+46
-2
lines changed

lib/cpp/src/thrift/protocol/TCompactProtocol.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,8 @@ class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_>
140140

141141
uint32_t writeBinary(const std::string& str);
142142

143+
uint32_t writeUUID(const TUuid& str);
144+
143145
int getMinSerializedSize(TType type) override;
144146

145147
void checkReadBytesAvailable(TSet& set) override
@@ -213,6 +215,8 @@ class TCompactProtocolT : public TVirtualProtocol<TCompactProtocolT<Transport_>
213215

214216
uint32_t readBinary(std::string& str);
215217

218+
uint32_t readUUID(TUuid& str);
219+
216220
/*
217221
*These methods are here for the struct to call, but don't have any wire
218222
* encoding.

lib/cpp/src/thrift/protocol/TCompactProtocol.tcc

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,11 @@ enum Types {
6161
CT_LIST = 0x09,
6262
CT_SET = 0x0A,
6363
CT_MAP = 0x0B,
64-
CT_STRUCT = 0x0C
64+
CT_STRUCT = 0x0C,
65+
CT_UUID = 0x0D
6566
};
6667

67-
const int8_t TTypeToCType[16] = {
68+
const int8_t TTypeToCType[17] = {
6869
CT_STOP, // T_STOP
6970
0, // unused
7071
CT_BOOLEAN_TRUE, // T_BOOL
@@ -81,6 +82,7 @@ const int8_t TTypeToCType[16] = {
8182
CT_MAP, // T_MAP
8283
CT_SET, // T_SET
8384
CT_LIST, // T_LIST
85+
CT_UUID, // T_UUID
8486
};
8587

8688
}} // end detail::compact namespace
@@ -286,6 +288,15 @@ uint32_t TCompactProtocolT<Transport_>::writeBinary(const std::string& str) {
286288
return wsize;
287289
}
288290

291+
/**
292+
* Write a TUuid to the wire
293+
*/
294+
template <class Transport_>
295+
uint32_t TCompactProtocolT<Transport_>::writeUUID(const TUuid& uuid) {
296+
trans_->write(uuid.data(), uuid.size());
297+
return uuid.size();
298+
}
299+
289300
//
290301
// Internal Writing methods
291302
//
@@ -719,6 +730,15 @@ uint32_t TCompactProtocolT<Transport_>::readBinary(std::string& str) {
719730
return rsize + static_cast<uint32_t>(size);
720731
}
721732

733+
734+
/**
735+
* Read a TUuid from the wire.
736+
*/
737+
template <class Transport_>
738+
uint32_t TCompactProtocolT<Transport_>::readUUID(TUuid& uuid) {
739+
return trans_->readAll(uuid.begin(), uuid.size());
740+
}
741+
722742
/**
723743
* Read an i32 from the wire as a varint. The MSB of each byte is set
724744
* 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) {
826846
return T_MAP;
827847
case detail::compact::CT_STRUCT:
828848
return T_STRUCT;
849+
case detail::compact::CT_UUID:
850+
return T_UUID;
829851
default:
830852
throw TException(std::string("don't know what type: ") + static_cast<char>(type));
831853
}
@@ -850,6 +872,7 @@ int TCompactProtocolT<Transport_>::getMinSerializedSize(TType type)
850872
case T_MAP: return sizeof(int8_t); // element count
851873
case T_SET: return sizeof(int8_t); // element count
852874
case T_LIST: return sizeof(int8_t); // element count
875+
case T_UUID: return 16; // 16 bytes
853876
default: throw TProtocolException(TProtocolException::UNKNOWN, "unrecognized type code");
854877
}
855878
}

lib/cpp/test/AllProtocolTests.tcc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include <thrift/protocol/TBinaryProtocol.h>
2626
#include <thrift/transport/TBufferTransports.h>
2727
#include <thrift/Thrift.h>
28+
#include <thrift/TUuid.h>
2829

2930
#include "GenericHelpers.h"
3031

@@ -208,6 +209,9 @@ void testProtocol(const char* protoname) {
208209
testNaked<TProto, std::string>("a bit longer than the smallest possible");
209210
testNaked<TProto, std::string>("\x1\x2\x3\x4\x5\x6\x7\x8\x9\xA"); // kinda binary test
210211

212+
testNaked<TProto, TUuid>(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
213+
testField<TProto, T_UUID, TUuid>(TUuid("5e2ab188-1726-4e75-a04f-1ed9a6a89c4c"));
214+
211215
testField<TProto, T_STRING, std::string>("");
212216
testField<TProto, T_STRING, std::string>("short");
213217
testField<TProto, T_STRING, std::string>("borderlinetiny");

lib/cpp/test/GenericHelpers.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include <thrift/protocol/TProtocol.h>
2424
#include <memory>
2525
#include <thrift/Thrift.h>
26+
#include <thrift/TUuid.h>
2627

2728
/* ClassName Helper for cleaner exceptions */
2829
class ClassNames {
@@ -57,6 +58,10 @@ template <>
5758
const char* ClassNames::getName<std::string>() {
5859
return "string";
5960
}
61+
template <>
62+
const char* ClassNames::getName<apache::thrift::TUuid>() {
63+
return "uuid";
64+
}
6065

6166
/* Generic Protocol I/O function for tests */
6267
class GenericIO {
@@ -87,6 +92,10 @@ class GenericIO {
8792
return proto->writeString(val);
8893
}
8994

95+
static uint32_t write(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, const apache::thrift::TUuid& val) {
96+
return proto->writeUUID(val);
97+
}
98+
9099
/* Read functions */
91100

92101
static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, int8_t& val) { return proto->readByte(val); }
@@ -102,6 +111,10 @@ class GenericIO {
102111
static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, std::string& val) {
103112
return proto->readString(val);
104113
}
114+
115+
static uint32_t read(std::shared_ptr<apache::thrift::protocol::TProtocol> proto, apache::thrift::TUuid& val) {
116+
return proto->readUUID(val);
117+
}
105118
};
106119

107120
#endif

0 commit comments

Comments
 (0)