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*);
size_t decodePduLength(const folly::IOBuf*);
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&);
}
}
......
......@@ -27,14 +27,16 @@ namespace bser {
const uint8_t kMagic[2] = {0, 1};
static void bserEncode(dynamic const& dyn,
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,
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,7 +84,8 @@ static void bserEncodeString(folly::StringPiece str, QueueAppender& appender) {
appender.push((uint8_t*)str.data(), str.size());
}
static void bserEncodeArraySimple(dynamic const& dyn,
static void bserEncodeArraySimple(
dynamic const& dyn,
QueueAppender& appender,
const serialization_opts& opts) {
appender.write((int8_t)BserType::Array);
......@@ -92,10 +95,10 @@ static void bserEncodeArraySimple(dynamic const& dyn,
}
}
static void bserEncodeArray(dynamic const& dyn,
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,
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,7 +155,8 @@ static void bserEncodeObject(dynamic const& dyn,
}
}
static void bserEncode(dynamic const& dyn,
static void bserEncode(
dynamic const& dyn,
QueueAppender& appender,
const serialization_opts& opts) {
switch (dyn.type()) {
......@@ -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) {
IOBufQueue q(IOBufQueue::cacheChainLength());
uint8_t hdrbuf[sizeof(kMagic) + 1 + sizeof(int64_t)];
......
......@@ -28,7 +28,8 @@ 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)...,
throw BserDecodeError(folly::to<std::string>(
std::forward<ARGS>(args)...,
" with ",
curs.length(),
" bytes remaining in cursor"));
......@@ -65,7 +66,8 @@ 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,
throwDecodeError(
curs,
"no data available while decoding a string, header was "
"not decoded properly");
}
......
......@@ -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,7 +93,8 @@ TEST(Bser, Template) {
opts.templates = templates;
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))
<< "Expected:\n"
<< folly::hexDump(template_blob, sizeof(template_blob) - 1) << "\nGot:\n"
......@@ -100,13 +102,15 @@ TEST(Bser, Template) {
}
TEST(Bser, PduLength) {
EXPECT_THROW([] {
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);
}(),
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