Skip to content

Commit

Permalink
Add env to write
Browse files Browse the repository at this point in the history
  • Loading branch information
PJutch committed Jul 23, 2024
1 parent 485e479 commit 3e159ab
Show file tree
Hide file tree
Showing 13 changed files with 40 additions and 38 deletions.
2 changes: 1 addition & 1 deletion include/JutchsON/write/bool.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ namespace JutchsON {

template <>
struct Writer<bool> {
std::string operator() (bool b, Context context) {
std::string operator() (bool b, const auto&, Context context) {
return writeBool(b, context == Context::LINE_REST);
}
};
Expand Down
8 changes: 4 additions & 4 deletions include/JutchsON/write/dict.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ namespace JutchsON {

template <typename Key, typename Value>
struct Writer<std::unordered_multimap<Key, Value>> {
std::string operator() (const std::unordered_multimap<Key, Value>& map, Context context) {
std::string operator() (const std::unordered_multimap<Key, Value>& map, const auto& env, Context context) {
std::vector<std::pair<std::string, std::string>> pairs;
pairs.reserve(std::ssize(map));
for (const auto& [key, value] : map) {
pairs.emplace_back(write(key, Context::OBJECT), write(value, Context::LINE_REST));
pairs.emplace_back(write(key, env, Context::OBJECT), write(value, env, Context::LINE_REST));
}

return writeDict(pairs);
Expand All @@ -44,11 +44,11 @@ namespace JutchsON {

template <typename Key, typename Value>
struct Writer<std::unordered_map<Key, Value>> {
std::string operator() (const std::unordered_map<Key, Value>& map, Context) {
std::string operator() (const std::unordered_map<Key, Value>& map, const auto& env, Context) {
std::vector<std::pair<std::string, std::string>> pairs;
pairs.reserve(std::ssize(map));
for (const auto& [key, value] : map) {
pairs.emplace_back(write(key, Context::OBJECT), write(value, Context::LINE_REST));
pairs.emplace_back(write(key, env, Context::OBJECT), write(value, env, Context::LINE_REST));
}

return writeDict(pairs);
Expand Down
4 changes: 2 additions & 2 deletions include/JutchsON/write/list.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ namespace JutchsON {

template <typename T>
struct Writer<std::vector<T>> {
std::string operator() (const std::vector<T>& list, Context context) {
std::string operator() (const std::vector<T>& list, const auto& env, Context context) {
bool multiline = shouldBeMultiline(list);

std::vector<std::string> elements;
elements.reserve(std::ssize(list));
for (const auto& element : list) {
elements.push_back(write(element, multiline ? Context::LINE : Context::OBJECT));
elements.push_back(write(element, env, multiline ? Context::LINE : Context::OBJECT));
}

if (multiline) {
Expand Down
8 changes: 4 additions & 4 deletions include/JutchsON/write/num.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,28 +91,28 @@ namespace JutchsON {

template <UnsignedInteger T>
struct Writer<T> {
std::string operator() (T n, Context) {
std::string operator() (T n, const auto&, Context) {
return writeUint<T>(n);
}
};

template <SignedInteger T>
struct Writer<T> {
std::string operator() (T n, Context) {
std::string operator() (T n, const auto&, Context) {
return writeInt<T>(n);
}
};

template <UnsignedFloat T>
struct Writer<T> {
std::string operator() (T x, Context) {
std::string operator() (T x, const auto&, Context) {
return writeNonnegativeFloat<T>(x);
}
};

template <SignedFloat T>
struct Writer<T> {
std::string operator() (T x, Context) {
std::string operator() (T x, const auto&, Context) {
return writeFloat<T>(x);
}
};
Expand Down
5 changes: 3 additions & 2 deletions include/JutchsON/write/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ namespace JutchsON {

template <typename T>
struct Writer<std::optional<T>> {
std::string operator() (std::optional<T> v, Context context) {
template <typename Env>
std::string operator() (std::optional<T> v, Env&& env, Context context) {
if (v) {
return writeOptional(write(*v, context));
return writeOptional(write(*v, std::forward<Env>(env), context));
} else {
return "null";
}
Expand Down
10 changes: 5 additions & 5 deletions include/JutchsON/write/str.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,35 @@ namespace JutchsON {

template <>
struct Writer<std::string> {
std::string operator() (const std::string& s, Context context) {
std::string operator() (const std::string& s, const auto&, Context context) {
return writeStr(s, context == Context::OBJECT);
}
};

template <>
struct Writer<std::string_view> {
std::string operator() (std::string_view s, Context context) {
std::string operator() (std::string_view s, const auto&, Context context) {
return writeStr(s, context == Context::OBJECT);
}
};

template <>
struct Writer<const char*> {
std::string operator() (const char* s, Context context) {
std::string operator() (const char* s, const auto&, Context context) {
return writeStr(s, context == Context::OBJECT);
}
};

template <>
struct Writer<char*> {
std::string operator() (char* s, Context context) {
std::string operator() (char* s, const auto&, Context context) {
return writeStr(s, context == Context::OBJECT);
}
};

template <>
struct Writer<StringView> {
std::string operator() (StringView s, Context context) {
std::string operator() (StringView s, const auto&, Context context) {
return writeStr(s, context == Context::OBJECT);
}
};
Expand Down
4 changes: 2 additions & 2 deletions include/JutchsON/write/struct.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
namespace JutchsON {
template <Described T>
struct Writer<T> {
std::string operator() (T t, Context) {
std::string operator() (T t, const auto& env, Context) {
std::vector<std::pair<std::string, std::string>> pairs;

boost::mp11::mp_for_each<boost::describe::describe_members<T, boost::describe::mod_public>>([&](auto d) {
Expand All @@ -24,7 +24,7 @@ namespace JutchsON {
}

if (shouldWrite) {
pairs.emplace_back(write(d.name), write(t.*d.pointer));
pairs.emplace_back(write(d.name, env), write(t.*d.pointer, env));
}
});

Expand Down
10 changes: 5 additions & 5 deletions include/JutchsON/write/tuple.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,22 @@

namespace JutchsON {
template <typename T, size_t i>
void tupleElements(const T& tuple, Context context, std::vector<std::string>& elements) {
void tupleElements(const T& tuple, const auto& env, Context context, std::vector<std::string>& elements) {
if constexpr (i >= std::tuple_size_v<T>) {
return;
} else {
elements.push_back(write(get<i>(tuple), context));
return tupleElements<T, i + 1>(tuple, context, elements);
elements.push_back(write(get<i>(tuple), env, context));
return tupleElements<T, i + 1>(tuple, env, context, elements);
}
}

template <Tuplelike T>
struct Writer<T> {
std::string operator() (const T& tuple, Context context) {
std::string operator() (const T& tuple, const auto& env, Context context) {
bool multiline = shouldBeMultiline(tuple);

std::vector<std::string> elements;
tupleElements<T, 0>(tuple, multiline ? Context::LINE : Context::OBJECT, elements);
tupleElements<T, 0>(tuple, env, multiline ? Context::LINE : Context::OBJECT, elements);

if (multiline) {
return writeMultilineList(elements);
Expand Down
7 changes: 4 additions & 3 deletions include/JutchsON/write/variant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,10 @@ namespace JutchsON {

template <typename... Types>
struct Writer<std::variant<Types...>> {
std::string operator() (std::variant<Types...> v, Context context) {
return writeVariant(write(v.index(), Context::OBJECT), std::visit([&](const auto& val) {
return write(val, Context::LINE_REST);
template <typename Env>
std::string operator() (std::variant<Types...> v, Env&& env, Context context) {
return writeVariant(write(v.index(), env, Context::OBJECT), std::visit([&](const auto& val) {
return write(val, std::forward<Env>(env), Context::LINE_REST);
}, v), context == Context::OBJECT);
}
};
Expand Down
12 changes: 6 additions & 6 deletions include/JutchsON/write/write.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace JutchsON {
template <typename T>
struct Writer;

template <typename T>
std::string write(T t, Context context = Context::LINE) {
return Writer<T>{}(t, context);
template <typename T, typename Env = EmptyEnv>
std::string write(T t, Env&& env = {}, Context context = Context::LINE) {
return Writer<T>{}(t, std::forward<Env>(env), context);
}

template <typename T>
void writeFile(const std::filesystem::path& path, T t) {
writeWholeFile(path, write(t));
template <typename T, typename Env = EmptyEnv>
void writeFile(const std::filesystem::path& path, T t, Env&& env = {}) {
writeWholeFile(path, write(t), std::forward<Env>(env));
}
}

Expand Down
2 changes: 1 addition & 1 deletion tests/write/bool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,5 +27,5 @@ TEST(Bool, writeGenericFalse) {
}

TEST(Bool, writeGenericTrueEmpty) {
EXPECT_EQ(JutchsON::write(true, JutchsON::Context::LINE_REST), "");
EXPECT_EQ(JutchsON::write(true, {}, JutchsON::Context::LINE_REST), "");
}
2 changes: 1 addition & 1 deletion tests/write/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ TEST(List, writeVectorString) {
}

TEST(List, writeVectorQuoted) {
EXPECT_EQ(JutchsON::write(std::vector{1, 2, 3}, JutchsON::Context::OBJECT), "[1 2 3]");
EXPECT_EQ(JutchsON::write(std::vector{1, 2, 3}, {}, JutchsON::Context::OBJECT), "[1 2 3]");
}

TEST(List, writeVectorVector) {
Expand Down
4 changes: 2 additions & 2 deletions tests/write/variant.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ TEST(Variant, writeStdVariantSecond) {
}

TEST(Variant, wrtieStdVariantChevrons) {
EXPECT_EQ((JutchsON::write(std::variant<int, std::string>{123}, JutchsON::Context::OBJECT)), "<0 123>");
EXPECT_EQ((JutchsON::write(std::variant<int, std::string>{123}, {}, JutchsON::Context::OBJECT)), "<0 123>");
}

TEST(Variant, wrtieStdVariantValueContext) {
EXPECT_EQ((JutchsON::write(std::variant<int, bool>{true})), "1");
}

TEST(Variant, wrtieStdVariantChevronsValueContext) {
EXPECT_EQ((JutchsON::write(std::variant<int, bool>{true}, JutchsON::Context::OBJECT)), "<1>");
EXPECT_EQ((JutchsON::write(std::variant<int, bool>{true}, {}, JutchsON::Context::OBJECT)), "<1>");
}

0 comments on commit 3e159ab

Please sign in to comment.