Skip to content

Commit 204155d

Browse files
ami7ofacebook-github-bot
authored andcommitted
op::decode for basic types
Summary: Added `op::decode(Protocol& prot, T& value)` to Decode.h, which decodes the value to the given protocol using the type tag. Support for containers, structs, and adapted types will be in next diff. Reviewed By: Mizuchi Differential Revision: D39058761 fbshipit-source-id: 74f33be018beed7f9712412d35120c60ace329de
1 parent 74820e0 commit 204155d

File tree

3 files changed

+130
-0
lines changed

3 files changed

+130
-0
lines changed

third-party/thrift/src/thrift/lib/cpp2/op/Encode.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,12 @@ namespace op {
2828
template <typename Tag>
2929
FOLLY_INLINE_VARIABLE constexpr detail::Encode<Tag> encode{};
3030

31+
// Decodes the value from the given protocol using the type tag.
32+
// This handles adapted type.
33+
// For example: decode<type::int16_t>(prot, i); // decode to variable i
34+
template <typename Tag>
35+
FOLLY_INLINE_VARIABLE constexpr detail::Decode<Tag> decode{};
36+
3137
} // namespace op
3238
} // namespace thrift
3339
} // namespace apache

third-party/thrift/src/thrift/lib/cpp2/op/EncodeTest.cpp

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,43 @@ TEST(EncodeTest, EncodeAdapted) {
213213
testEncodeAdapted<conformance::StandardProtocol::Binary>();
214214
testEncodeAdapted<conformance::StandardProtocol::Compact>();
215215
}
216+
217+
template <conformance::StandardProtocol Protocol, typename Tag, typename T>
218+
void testDecode(T value) {
219+
SCOPED_TRACE(folly::pretty_name<Tag>());
220+
protocol_writer_t<Protocol> writer;
221+
folly::IOBufQueue queue;
222+
writer.setOutput(&queue);
223+
encode<Tag>(writer, value);
224+
225+
protocol_reader_t<Protocol> reader;
226+
auto serialized = queue.move();
227+
reader.setInput(serialized.get());
228+
T result;
229+
decode<Tag>(reader, result);
230+
EXPECT_EQ(result, value);
231+
}
232+
233+
template <conformance::StandardProtocol Protocol>
234+
void testDecodeBasicTypes() {
235+
SCOPED_TRACE(apache::thrift::util::enumNameSafe(Protocol));
236+
testDecode<Protocol, type::bool_t>(true);
237+
testDecode<Protocol, type::byte_t>((int8_t)1);
238+
testDecode<Protocol, type::i16_t>((int16_t)11);
239+
testDecode<Protocol, type::i32_t>((int32_t)11);
240+
testDecode<Protocol, type::i64_t>((int64_t)11);
241+
testDecode<Protocol, type::float_t>(1.5f);
242+
testDecode<Protocol, type::double_t>(1.5);
243+
testDecode<Protocol, type::string_t>(std::string("foo"));
244+
testDecode<Protocol, type::binary_t>(std::string("foo"));
245+
enum class MyEnum { value = 1 };
246+
testDecode<Protocol, type::enum_t<MyEnum>>(MyEnum::value);
247+
}
248+
249+
TEST(DecodeTest, DecodeBasicTypes) {
250+
testDecodeBasicTypes<conformance::StandardProtocol::Binary>();
251+
testDecodeBasicTypes<conformance::StandardProtocol::Compact>();
252+
}
253+
216254
} // namespace
217255
} // namespace apache::thrift::op

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

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#pragma once
1818

19+
#include <utility>
1920
#include <thrift/lib/cpp/protocol/TType.h>
2021
#include <thrift/lib/cpp2/type/NativeType.h>
2122
#include <thrift/lib/cpp2/type/Tag.h>
@@ -276,6 +277,91 @@ struct Encode<type::adapted<Adapter, Tag>> {
276277
}
277278
};
278279

280+
template <typename>
281+
struct Decode;
282+
283+
template <>
284+
struct Decode<type::bool_t> {
285+
template <typename Protocol>
286+
void operator()(Protocol& prot, bool& b) const {
287+
prot.readBool(b);
288+
}
289+
};
290+
291+
template <>
292+
struct Decode<type::byte_t> {
293+
template <typename Protocol>
294+
void operator()(Protocol& prot, int8_t& i) const {
295+
prot.readByte(i);
296+
}
297+
};
298+
299+
template <>
300+
struct Decode<type::i16_t> {
301+
template <typename Protocol>
302+
void operator()(Protocol& prot, int16_t& i) const {
303+
prot.readI16(i);
304+
}
305+
};
306+
307+
template <>
308+
struct Decode<type::i32_t> {
309+
template <typename Protocol>
310+
void operator()(Protocol& prot, int32_t& i) const {
311+
prot.readI32(i);
312+
}
313+
};
314+
315+
template <>
316+
struct Decode<type::i64_t> {
317+
template <typename Protocol>
318+
void operator()(Protocol& prot, int64_t& i) const {
319+
prot.readI64(i);
320+
}
321+
};
322+
323+
template <>
324+
struct Decode<type::float_t> {
325+
template <typename Protocol>
326+
void operator()(Protocol& prot, float& f) const {
327+
prot.readFloat(f);
328+
}
329+
};
330+
331+
template <>
332+
struct Decode<type::double_t> {
333+
template <typename Protocol>
334+
void operator()(Protocol& prot, double& d) const {
335+
prot.readDouble(d);
336+
}
337+
};
338+
339+
template <>
340+
struct Decode<type::string_t> {
341+
template <typename Protocol, typename StrType>
342+
void operator()(Protocol& prot, StrType& s) const {
343+
prot.readString(s);
344+
}
345+
};
346+
347+
template <>
348+
struct Decode<type::binary_t> {
349+
template <typename Protocol, typename StrType>
350+
void operator()(Protocol& prot, StrType& s) const {
351+
prot.readBinary(s);
352+
}
353+
};
354+
355+
template <typename T>
356+
struct Decode<type::enum_t<T>> {
357+
template <typename Protocol>
358+
void operator()(Protocol& prot, T& t) const {
359+
int32_t i;
360+
prot.readI32(i);
361+
t = static_cast<T>(i);
362+
}
363+
};
364+
279365
} // namespace detail
280366
} // namespace op
281367
} // namespace thrift

0 commit comments

Comments
 (0)