Commit 060471a0 authored by Yedidya Feldblum's avatar Yedidya Feldblum Committed by Facebook Github Bot

Apply clang-format to folly/experimental/bser/

Summary: [Folly] Apply `clang-format` to `folly/experimental/bser/`.

Reviewed By: Orvid

Differential Revision: D5460552

fbshipit-source-id: e103f8920eb29b7c14688f5bbb3e28129ef35e96
parent 93dc9876
...@@ -94,7 +94,8 @@ folly::dynamic parseBser(const folly::IOBuf*); ...@@ -94,7 +94,8 @@ folly::dynamic parseBser(const folly::IOBuf*);
size_t decodePduLength(const folly::IOBuf*); size_t decodePduLength(const folly::IOBuf*);
folly::fbstring toBser(folly::dynamic const&, const serialization_opts&); folly::fbstring toBser(folly::dynamic const&, const serialization_opts&);
std::unique_ptr<folly::IOBuf> toBserIOBuf(folly::dynamic const&, std::unique_ptr<folly::IOBuf> toBserIOBuf(
folly::dynamic const&,
const serialization_opts&); const serialization_opts&);
} }
} }
......
...@@ -27,14 +27,16 @@ namespace bser { ...@@ -27,14 +27,16 @@ namespace bser {
const uint8_t kMagic[2] = {0, 1}; const uint8_t kMagic[2] = {0, 1};
static void bserEncode(dynamic const& dyn, static void bserEncode(
dynamic const& dyn,
QueueAppender& appender, QueueAppender& appender,
const serialization_opts& opts); const serialization_opts& opts);
serialization_opts::serialization_opts() serialization_opts::serialization_opts()
: sort_keys(false), growth_increment(8192) {} : sort_keys(false), growth_increment(8192) {}
static const dynamic* getTemplate(const serialization_opts& opts, static const dynamic* getTemplate(
const serialization_opts& opts,
dynamic const& dynArray) { dynamic const& dynArray) {
if (!opts.templates.hasValue()) { if (!opts.templates.hasValue()) {
return nullptr; return nullptr;
...@@ -50,9 +52,9 @@ static const dynamic* getTemplate(const serialization_opts& opts, ...@@ -50,9 +52,9 @@ static const dynamic* getTemplate(const serialization_opts& opts,
static void bserEncodeInt(int64_t ival, QueueAppender& appender) { static void bserEncodeInt(int64_t ival, QueueAppender& appender) {
/* Return the smallest size int that can store the value */ /* Return the smallest size int that can store the value */
auto size = auto size =
((ival == ((int8_t)ival)) ? 1 : (ival == ((int16_t)ival)) ((ival == ((int8_t)ival))
? 2 ? 1
: (ival == ((int32_t)ival)) ? 4 : 8); : (ival == ((int16_t)ival)) ? 2 : (ival == ((int32_t)ival)) ? 4 : 8);
switch (size) { switch (size) {
case 1: case 1:
...@@ -82,7 +84,8 @@ static void bserEncodeString(folly::StringPiece str, QueueAppender& appender) { ...@@ -82,7 +84,8 @@ static void bserEncodeString(folly::StringPiece str, QueueAppender& appender) {
appender.push((uint8_t*)str.data(), str.size()); appender.push((uint8_t*)str.data(), str.size());
} }
static void bserEncodeArraySimple(dynamic const& dyn, static void bserEncodeArraySimple(
dynamic const& dyn,
QueueAppender& appender, QueueAppender& appender,
const serialization_opts& opts) { const serialization_opts& opts) {
appender.write((int8_t)BserType::Array); appender.write((int8_t)BserType::Array);
...@@ -92,10 +95,10 @@ static void bserEncodeArraySimple(dynamic const& dyn, ...@@ -92,10 +95,10 @@ static void bserEncodeArraySimple(dynamic const& dyn,
} }
} }
static void bserEncodeArray(dynamic const& dyn, static void bserEncodeArray(
dynamic const& dyn,
QueueAppender& appender, QueueAppender& appender,
const serialization_opts& opts) { const serialization_opts& opts) {
auto templ = getTemplate(opts, dyn); auto templ = getTemplate(opts, dyn);
if (UNLIKELY(templ != nullptr)) { if (UNLIKELY(templ != nullptr)) {
appender.write((int8_t)BserType::Template); appender.write((int8_t)BserType::Template);
...@@ -129,15 +132,16 @@ static void bserEncodeArray(dynamic const& dyn, ...@@ -129,15 +132,16 @@ static void bserEncodeArray(dynamic const& dyn,
bserEncodeArraySimple(dyn, appender, opts); bserEncodeArraySimple(dyn, appender, opts);
} }
static void bserEncodeObject(dynamic const& dyn, static void bserEncodeObject(
dynamic const& dyn,
QueueAppender& appender, QueueAppender& appender,
const serialization_opts& opts) { const serialization_opts& opts) {
appender.write((int8_t)BserType::Object); appender.write((int8_t)BserType::Object);
bserEncodeInt(int64_t(dyn.size()), appender); bserEncodeInt(int64_t(dyn.size()), appender);
if (opts.sort_keys) { if (opts.sort_keys) {
std::vector<std::pair<dynamic, dynamic>> sorted(dyn.items().begin(), std::vector<std::pair<dynamic, dynamic>> sorted(
dyn.items().end()); dyn.items().begin(), dyn.items().end());
std::sort(sorted.begin(), sorted.end()); std::sort(sorted.begin(), sorted.end());
for (const auto& item : sorted) { for (const auto& item : sorted) {
bserEncode(item.first, appender, opts); bserEncode(item.first, appender, opts);
...@@ -151,7 +155,8 @@ static void bserEncodeObject(dynamic const& dyn, ...@@ -151,7 +155,8 @@ static void bserEncodeObject(dynamic const& dyn,
} }
} }
static void bserEncode(dynamic const& dyn, static void bserEncode(
dynamic const& dyn,
QueueAppender& appender, QueueAppender& appender,
const serialization_opts& opts) { const serialization_opts& opts) {
switch (dyn.type()) { switch (dyn.type()) {
...@@ -183,7 +188,8 @@ static void bserEncode(dynamic const& dyn, ...@@ -183,7 +188,8 @@ static void bserEncode(dynamic const& dyn,
} }
} }
std::unique_ptr<folly::IOBuf> toBserIOBuf(folly::dynamic const& dyn, std::unique_ptr<folly::IOBuf> toBserIOBuf(
folly::dynamic const& dyn,
const serialization_opts& opts) { const serialization_opts& opts) {
IOBufQueue q(IOBufQueue::cacheChainLength()); IOBufQueue q(IOBufQueue::cacheChainLength());
uint8_t hdrbuf[sizeof(kMagic) + 1 + sizeof(int64_t)]; uint8_t hdrbuf[sizeof(kMagic) + 1 + sizeof(int64_t)];
......
...@@ -28,7 +28,8 @@ static dynamic parseBser(Cursor& curs); ...@@ -28,7 +28,8 @@ static dynamic parseBser(Cursor& curs);
template <typename... ARGS> template <typename... ARGS>
[[noreturn]] static void throwDecodeError(Cursor& curs, ARGS&&... args) { [[noreturn]] static void throwDecodeError(Cursor& curs, ARGS&&... args) {
throw BserDecodeError(folly::to<std::string>(std::forward<ARGS>(args)..., throw BserDecodeError(folly::to<std::string>(
std::forward<ARGS>(args)...,
" with ", " with ",
curs.length(), curs.length(),
" bytes remaining in cursor")); " bytes remaining in cursor"));
...@@ -65,7 +66,8 @@ static std::string decodeString(Cursor& curs) { ...@@ -65,7 +66,8 @@ static std::string decodeString(Cursor& curs) {
if (available == 0) { if (available == 0) {
// Saw this case when we decodeHeader was returning the incorrect length // Saw this case when we decodeHeader was returning the incorrect length
// and we were splitting off too few bytes from the IOBufQueue // and we were splitting off too few bytes from the IOBufQueue
throwDecodeError(curs, throwDecodeError(
curs,
"no data available while decoding a string, header was " "no data available while decoding a string, header was "
"not decoded properly"); "not decoded properly");
} }
......
...@@ -67,7 +67,8 @@ TEST(Bser, RoundTrip) { ...@@ -67,7 +67,8 @@ TEST(Bser, RoundTrip) {
} catch (const std::exception& err) { } catch (const std::exception& err) {
LOG(ERROR) << err.what() << "\nInput: " << dyn.typeName() << ": " << dyn LOG(ERROR) << err.what() << "\nInput: " << dyn.typeName() << ": " << dyn
<< " decoded back as " << decoded.typeName() << ": " << decoded << " decoded back as " << decoded.typeName() << ": " << decoded
<< "\n" << folly::hexDump(str.data(), str.size()); << "\n"
<< folly::hexDump(str.data(), str.size());
throw; throw;
} }
} }
...@@ -80,8 +81,8 @@ TEST(Bser, Template) { ...@@ -80,8 +81,8 @@ TEST(Bser, Template) {
decoded = folly::bser::parseBser( decoded = folly::bser::parseBser(
folly::ByteRange(template_blob, sizeof(template_blob) - 1)); folly::ByteRange(template_blob, sizeof(template_blob) - 1));
EXPECT_EQ(decoded, template_dynamic) EXPECT_EQ(decoded, template_dynamic)
<< "Didn't load template value." << "Didn't load template value.\n"
"\nInput: " << template_dynamic.typeName() << ": " << template_dynamic << "Input: " << template_dynamic.typeName() << ": " << template_dynamic
<< " decoded back as " << decoded.typeName() << ": " << decoded << "\n" << " decoded back as " << decoded.typeName() << ": " << decoded << "\n"
<< folly::hexDump(template_blob, sizeof(template_blob) - 1); << folly::hexDump(template_blob, sizeof(template_blob) - 1);
...@@ -92,7 +93,8 @@ TEST(Bser, Template) { ...@@ -92,7 +93,8 @@ TEST(Bser, Template) {
opts.templates = templates; opts.templates = templates;
str = folly::bser::toBser(decoded, opts); str = folly::bser::toBser(decoded, opts);
EXPECT_EQ(folly::ByteRange((const uint8_t*)str.data(), str.size()), EXPECT_EQ(
folly::ByteRange((const uint8_t*)str.data(), str.size()),
folly::ByteRange(template_blob, sizeof(template_blob) - 1)) folly::ByteRange(template_blob, sizeof(template_blob) - 1))
<< "Expected:\n" << "Expected:\n"
<< folly::hexDump(template_blob, sizeof(template_blob) - 1) << "\nGot:\n" << folly::hexDump(template_blob, sizeof(template_blob) - 1) << "\nGot:\n"
...@@ -100,13 +102,15 @@ TEST(Bser, Template) { ...@@ -100,13 +102,15 @@ TEST(Bser, Template) {
} }
TEST(Bser, PduLength) { TEST(Bser, PduLength) {
EXPECT_THROW([] { EXPECT_THROW(
[] {
// Try to decode PDU for a short buffer that doesn't even have the // Try to decode PDU for a short buffer that doesn't even have the
// complete length available // complete length available
auto buf = folly::IOBuf::wrapBuffer(template_blob, 3); auto buf = folly::IOBuf::wrapBuffer(template_blob, 3);
auto len = folly::bser::decodePduLength(&*buf); auto len = folly::bser::decodePduLength(&*buf);
LOG(ERROR) << "managed to return a length, but only had 3 bytes"; LOG(ERROR) << "managed to return a length, but only had 3 bytes";
}(), std::out_of_range); }(),
std::out_of_range);
auto buf = folly::IOBuf::wrapBuffer(template_blob, sizeof(template_blob)); auto buf = folly::IOBuf::wrapBuffer(template_blob, sizeof(template_blob));
auto len = folly::bser::decodePduLength(&*buf); auto len = folly::bser::decodePduLength(&*buf);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment