Skip to content

Commit

Permalink
github action update and adatpor/fmt change
Browse files Browse the repository at this point in the history
- name change: serde::literal -> serde::string_converter
- new function: serde::to_string, serde::to_str
- to_string pring change
    - before
    {"key : data", "key2: data2"}
    - after
    {key : "data", key2: "data2"}
- new default attribute name default_ == default_se
  • Loading branch information
injae committed Jul 17, 2021
1 parent 6dcf0b8 commit 3f2fb25
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 32 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/linux.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
ubuntu-bionic-gcc:
runs-on: ubuntu-18.04
env:
CMAKE_FLAGS: '-DUSE_CPPM_PATH=ON -DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down Expand Up @@ -42,7 +42,7 @@ jobs:
ubuntu-bionic-llvm:
runs-on: ubuntu-18.04
env:
CMAKE_FLAGS: '-DUSE_CPPM_PATH=ON -DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_BENCHMARK=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON -DSERDEPP_USE_BENCHMARK=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/macos.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
macos-clang:
runs-on: macos-latest
env:
CMAKE_FLAGS: '-DUSE_CPPM_PATH=ON -DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/window.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
windows-msvc:
runs-on: windows-2019
env:
CMAKE_FLAGS: '-DUSE_CPPM_PATH=ON -DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
CMAKE_FLAGS: '-DSERDEPP_USE_NLOHMANN_JSON=ON -DSERDEPP_USE_TOML11=ON -DSERDEPP_USE_YAML-CPP=ON -DSERDEPP_BUILD_EXAMPLES=ON'
strategy:
matrix:
build-type: ['Release', 'Debug']
Expand Down
8 changes: 8 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ SOURCES

endif()

if(SERDEPP_BUILD_BENCHMARKS)

cppm_target_define(benchmark BINARY
SOURCES
benchmark/benchmark.cpp
)

endif()

set(serdepp_global_deps PRIVATE benchmark
PUBLIC nlohmann_json toml11 yaml-cpp nameof magic_enum fmt)
Expand Down
1 change: 1 addition & 0 deletions examples/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ int main()

// try {
test t = serde::serialize<test>(v);
fmt::print("{}\n",serde::to_str(t.io));

auto v_to_json = serde::deserialize<nlohmann::json>(t);
auto v_to_toml = serde::deserialize<serde::toml_v>(t);
Expand Down
72 changes: 45 additions & 27 deletions include/serdepp/adaptor/fmt.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once

#ifndef __SERDEPP_ADAPTOR_FMT_HPP__

#define __SERDEPP_ADAPTOR_FMT_HPP__

#include <serdepp/serializer.hpp>
Expand All @@ -10,49 +9,68 @@

namespace serde {
using namespace fmt::literals;
struct literal {
std::vector<std::string> iter;
struct string_converter {
struct _element {
std::string_view id;
std::string value;
explicit _element(std::string_view id, const std::string& value) noexcept : id(id), value(value) {}
std::string to_str() const {
return id.empty() ? value : "{} : \"{}\""_format(id, value);
}
};
std::vector<_element> iter;
template<typename T>
literal& add(std::string_view key, const T& data) {
iter.push_back("{}: {}"_format(key, data));
string_converter& add(std::string_view key, const T& data) {
iter.push_back(_element(key, fmt::format("{}", data)));
return *this;
}
std::string to_string() {
return fmt::format("{}", iter);
}
std::string to_str() const { return iter.size() == 1 ? fmt::format("{}", iter[0]) : fmt::format("{}", iter); }
};

template<> struct serde_adaptor_helper<literal>: derive_serde_adaptor_helper<literal> {
inline constexpr static bool is_null(literal& adaptor, std::string_view key) { return false; }
inline constexpr static size_t size(literal& adaptor) { return 1; }
inline constexpr static bool is_struct(literal& adaptor) { return true; }
template<> struct serde_adaptor_helper<string_converter>: derive_serde_adaptor_helper<string_converter> {
inline constexpr static bool is_null(string_converter& adaptor, std::string_view key) { return false; }
inline constexpr static size_t size(string_converter& adaptor) { return 1; }
inline constexpr static bool is_struct(string_converter& adaptor) { return true; }
};

template<typename T> struct serde_adaptor<literal, T, type::struct_t> {
static void from(literal& s, std::string_view key, T& data) {
throw serde::unimplemented_error(fmt::format("serde_adaptor<{}>::from(literal, key data)",
nameof::nameof_short_type<literal>()));
}
static void into(literal& s, std::string_view key, const T& data) {
s.add(key, deserialize<serde::literal>(data).to_string());
template<typename T, typename U> struct serde_adaptor<string_converter, T, U> {
static void from(string_converter& s, std::string_view key, T& data){
throw serde::unimplemented_error(fmt::format("serde_adaptor<{}>::from(string_converter, key data)",
nameof::nameof_short_type<string_converter>()));
}
static void into(string_converter& s, std::string_view key, const T& data) { s.add(key, data); }
};

template<typename T, typename U> struct serde_adaptor<literal, T, U> {
static void from(literal& s, std::string_view key, T& data){
throw serde::unimplemented_error(fmt::format("serde_adaptor<{}>::from(literal, key data)",
nameof::nameof_short_type<literal>()));
}
static void into(literal& s, std::string_view key, const T& data) { s.add(key, data); }
};

template<typename T>
inline std::string to_string(const T& type) { return deserialize<string_converter>(type).to_str(); }
template<typename T>
inline std::string to_str(const T& type) { return deserialize<string_converter>(type).to_str(); }
}

template <typename T>
struct fmt::formatter<T, std::enable_if_t<serde::type::is_struct_v<T>, char>>
: fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const T& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde::deserialize<serde::literal>(serde_type).to_string(), ctx);
return formatter<std::string>::format(serde::deserialize<serde::string_converter>(serde_type).to_str(), ctx);
}
};

template<>
struct fmt::formatter<serde::string_converter> : fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const serde::string_converter& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.to_str(), ctx);
}
};

template<>
struct fmt::formatter<serde::string_converter::_element> : fmt::formatter<std::string> {
template <typename format_ctx>
auto format(const serde::string_converter::_element& serde_type, format_ctx& ctx) {
return formatter<std::string>::format(serde_type.to_str(), ctx);
}
};

#endif
2 changes: 2 additions & 0 deletions include/serdepp/attribute/default.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace serde::attribute {
};
// deduce guide
template<typename D> default_se(D&&) -> default_se<D>;

template<typename T> using default_ = default_se<T>;
}


Expand Down
2 changes: 1 addition & 1 deletion include/serdepp/ostream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace serde::ostream {
template< class CharT, class Traits, typename T>
std::basic_ostream<CharT, Traits>& operator<<(std::basic_ostream<CharT,Traits>& os, const T& x) {
if constexpr (type::is_struct_v<T>) {
return os << serde::deserialize<serde::literal>(x).to_string();
return os << serde::deserialize<serde::string_converter>(x).to_str();
} else {
return os << x;
}
Expand Down

0 comments on commit 3f2fb25

Please sign in to comment.