Fix route parameters

This commit is contained in:
G.H.O.S.T 2024-12-31 03:03:38 +01:00
parent ef549ce2f5
commit 428ac2f229
Signed by: G.H.O.S.T
GPG Key ID: 3BD93EABD1407B82
2 changed files with 32 additions and 64 deletions

View File

@ -1,17 +1,19 @@
#ifndef code__seafire__routing__route_parameters_hxx_ #ifndef code__seafire__routing__route_parameter_hxx_
#define code__seafire__routing__route_parameters_hxx_ #define code__seafire__routing__route_parameter_hxx_
#include <code/seafire/server/parameters.hxx> #include <code/seafire/server/parameters.hxx>
#include <code/seafire/server/request.hxx> #include <code/seafire/server/request.hxx>
#include <code/seafire/routing/route-parameters.hxx> #include <code/seafire/routing/route-parameters.hxx>
#include <optional>
namespace code::seafire::routing namespace code::seafire::routing
{ {
template< template<
server::parameter_name Name, server::parameter_name_t Name,
typename ParameterType = server::string_parameter typename ParameterType = server::string_parameter_t
> >
class route_parameter_t class route_parameter_t
{ {
@ -21,29 +23,46 @@ namespace code::seafire::routing
static static
std::string const& std::string const&
name(); name()
{
static std::string const name{Name};
return name;
}
route_parameter_t(std::optional<value_type>); route_parameter_t(std::optional<value_type> value)
: _value{std::move(value)}
{}
std::optional<value_type> const& std::optional<value_type> const&
value() const; value() const
{
return _value;
}
operator std::optional<value_type> const&() const; operator std::optional<value_type> const&() const
{
return value();
}
std::optional<value_type> const* std::optional<value_type> const*
operator->() const; operator->() const
{
return &value();
}
static static
route_parameter_t<Name, ParameterType> route_parameter_t<Name, ParameterType>
fetch(server::request_t&); fetch(server::request_t& req)
{
auto v = req.extensions().use<route_parameters_t>().get(name());
return parameter_type::try_parse(v);
}
private: private:
std::optional<value_type> value_; std::optional<value_type> _value;
}; };
} // namespace code::seafire::routing } // namespace code::seafire::routing
#include <code/seafire/routing/route-parameters.txx>
#endif #endif

View File

@ -1,51 +0,0 @@
namespace code::seafire::routing
{
template<server::parameter_name Name, typename ParameterType>
std::string const&
route_parameter_t<Name, ParameterType>::
name()
{
static std::string const name{Name};
return name;
}
template<server::parameter_name Name, typename ParameterType>
route_parameter_t<Name, ParameterType>::
route_parameter_t(std::optional<value_type> value)
: value_{std::move(value)}
{}
template<server::parameter_name Name, typename ParameterType>
std::optional<route_parameter_t<Name, ParameterType>::value_type> const&
route_parameter_t<Name, ParameterType>::
value() const
{
return value;
}
template<server::parameter_name Name, typename ParameterType>
route_parameter_t<Name, ParameterType>::
operator std::optional<value_type> const&() const
{
return value();
}
template<server::parameter_name Name, typename ParameterType>
std::optional<route_parameter_t<Name, ParameterType>::value_type> const*
route_parameter_t<Name, ParameterType>::
operator->() const
{
return &value();
}
template<server::parameter_name Name, typename ParameterType>
route_parameter_t<Name, ParameterType>
route_parameter_t<Name, ParameterType>::
fetch(server::request_t&)
{
auto v = req.extensions().use<route_parameters_t>().get(name());
return parameter_type::try_parse(v);
}
} // namespace code::seafire::routing