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