Skip to content

Commit

Permalink
Fix issue 87
Browse files Browse the repository at this point in the history
  • Loading branch information
Loki-Astari committed Jul 16, 2024
1 parent 3b4fb77 commit 366e612
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/Serialize/Traits.h
Original file line number Diff line number Diff line change
Expand Up @@ -565,10 +565,20 @@ class Traits<DataType BUILDTEMPLATETYPEVALUE(THOR_TYPENAMEVALUEACTION, Count) >
return Traits<std::remove_cv_t<MemberType>>::getPrintSize(printer, object.*memPtr, false);\
} \
\
template<typename M> \
static std::pair<std::size_t, std::size_t> addSizeEachMemberItem(PrinterInterface& printer, MyType const& object, M item) \
template<typename M, typename C> \
static std::pair<std::size_t, std::size_t> addSizeEachMemberItem(PrinterInterface& printer, MyType const& object, std::pair<C, M*> const& item) \
{ \
if (!Filter<MyType>::filter(object, item.first, *(item.second))) { \
return std::make_pair(0UL,0UL); \
} \
auto partSize = addSizeOneMember(printer, object, item.second); \
auto nameSize = std::strlen(Override<MyType>::nameOverride(item.first)); \
return std::make_pair(partSize + nameSize, 1); \
} \
template<typename M, typename C> \
static std::pair<std::size_t, std::size_t> addSizeEachMemberItem(PrinterInterface& printer, MyType const& object, std::pair<C, M MyType::*> const& item) \
{ \
if (!Filter<MyType>::filter(object, item.first, item)) { \
if (!Filter<MyType>::filter(object, item.first, object.*(item.second))) { \
return std::make_pair(0UL,0UL); \
} \
auto partSize = addSizeOneMember(printer, object, item.second); \
Expand Down
46 changes: 46 additions & 0 deletions src/Serialize/test/Issue87Test.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include "gtest/gtest.h"

#include "Traits.h"
#include "BsonThor.h"

#include <optional>
#include <string>

using ThorsAnvil::Serialize::bsonExporter;
using namespace std::string_literals;

struct WriteConcern
{
std::optional<int> w;
bool j = false;
};

ThorsAnvil_MakeTrait(WriteConcern, w, j);


TEST(Issue87Test, FieldJOnly)
{
WriteConcern a;
std::stringstream output;
output << bsonExporter(a);

EXPECT_EQ( "\x09\x00\x00\x00"
"\x08" "j\x00" "\x00"
"\x00"s,
output.str());
}

TEST(Issue87Test, FieldJAndW)
{
WriteConcern a;
a.w = 7;
std::stringstream output;
output << bsonExporter(a);

EXPECT_EQ( "\x10\x00\x00\x00"
"\x10" "w\x00" "\x07\x00\x00\x00"
"\x08" "j\x00" "\x00"
"\x00"s,
output.str());
}

0 comments on commit 366e612

Please sign in to comment.