Skip to content

Commit

Permalink
feat(daemon): add reflect-cpp helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
LordTermor committed May 24, 2024
1 parent 99c3e6d commit c952ce2
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
21 changes: 21 additions & 0 deletions daemon/utilities/drogon/Helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
#pragma once

#include "drogon/utils/FunctionTraits.h"
#include "utilities/reflect/PathParser.h"

#include "json/value.h"
#include <drogon/HttpRequest.h>
#include <drogon/HttpResponse.h>
#include <drogon/HttpTypes.h>
#include <rfl/SnakeCaseToCamelCase.hpp>
#include <rfl/json/read.hpp>
#include <rfl/json/write.hpp>

namespace bxt::drogon_helpers {
inline ::drogon::HttpResponsePtr make_error_response(
Expand Down Expand Up @@ -39,4 +44,20 @@ inline ::drogon::HttpResponsePtr

return result;
}

// Use reflect-cpp to make json response
template<typename T>
inline ::drogon::HttpResponsePtr make_json_response(const T& value) {
auto result = ::drogon::HttpResponse::newHttpResponse();
result->setBody(rfl::json::write<rfl::SnakeCaseToCamelCase>(value));
result->setStatusCode(drogon::k200OK);

return result;
}

template<typename T> inline auto get_request_json(drogon::HttpRequestPtr req) {
return rfl::json::read<T, rfl::SnakeCaseToCamelCase>(
std::string(req->body()));
}

} // namespace bxt::drogon_helpers
32 changes: 32 additions & 0 deletions daemon/utilities/reflect/PathParser.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/* === This file is part of bxt ===
*
* SPDX-FileCopyrightText: 2024 Artem Grinev <[email protected]>
* SPDX-License-Identifier: AGPL-3.0-or-later
*
*/
#pragma once
#include <filesystem>
#include <rfl/parsing/CustomParser.hpp>
#include <string>

struct Path {
std::string path;

static Path from_class(const std::filesystem::path& _p) noexcept {
return Path {_p.string()};
}
std::filesystem::path to_class() const {
return std::filesystem::path {path};
}
};
namespace rfl::parsing {

template<class ReaderType, class WriterType, class ProcessorsType>
struct Parser<ReaderType, WriterType, std::filesystem::path, ProcessorsType>
: public CustomParser<ReaderType,
WriterType,
ProcessorsType,
std::filesystem::path,
Path> {};

} // namespace rfl::parsing

0 comments on commit c952ce2

Please sign in to comment.