Add the getOptionalParameter method (#1037)

This commit is contained in:
An Tao 2021-09-30 08:50:00 +08:00 committed by GitHub
parent 22f810a71b
commit 3eb5bcd1dd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 166 additions and 25 deletions

View File

@ -52,11 +52,11 @@ int main()
[](const HttpRequestPtr &req,
std::function<void(const HttpResponsePtr &)> &&callback) {
auto resp = HttpResponse::newHttpResponse();
std::string name = req->getParameter("user");
if (name == "")
auto name = req->getOptionalParameter<std::string>("user");
if (!name)
resp->setBody("Please tell me your name");
else
resp->setBody("Hello, " + name + "!");
resp->setBody("Hello, " + name.value() + "!");
callback(resp);
},
{Get});

View File

@ -21,6 +21,7 @@
#include <drogon/DrClassMap.h>
#include <drogon/DrObject.h>
#include <drogon/utils/FunctionTraits.h>
#include <drogon/utils/Utilities.h>
#include <drogon/HttpRequest.h>
#include <deque>
#include <memory>
@ -159,27 +160,8 @@ class HttpBinder : public HttpBinderBase
std::string handlerName_;
template <typename T>
struct CanConvertFromStringStream
{
private:
using yes = std::true_type;
using no = std::false_type;
template <typename U>
static auto test(U *p, std::stringstream &&ss)
-> decltype((ss >> *p), yes());
template <typename>
static no test(...);
public:
static constexpr bool value =
std::is_same<decltype(test<T>(nullptr, std::stringstream())),
yes>::value;
};
template <typename T>
typename std::enable_if<CanConvertFromStringStream<T>::value, void>::type
typename std::enable_if<internal::CanConvertFromStringStream<T>::value,
void>::type
getHandlerArgumentValue(T &value, std::string &&p)
{
if (!p.empty())
@ -190,7 +172,8 @@ class HttpBinder : public HttpBinderBase
}
template <typename T>
typename std::enable_if<!(CanConvertFromStringStream<T>::value), void>::type
typename std::enable_if<!(internal::CanConvertFromStringStream<T>::value),
void>::type
getHandlerArgumentValue(T &value, std::string &&p)
{
}

View File

@ -16,6 +16,8 @@
#include <drogon/exports.h>
#include <drogon/utils/string_view.h>
#include <drogon/utils/optional.h>
#include <drogon/utils/Utilities.h>
#include <drogon/DrClassMap.h>
#include <drogon/HttpTypes.h>
#include <drogon/Session.h>
@ -277,6 +279,38 @@ class DROGON_EXPORT HttpRequest
/// Get a parameter identified by the @param key
virtual const std::string &getParameter(const std::string &key) const = 0;
/**
* @brief Get the optional parameter identified by the @param key. if the
* parameter doesn't exist, or the original parameter can't be converted to
* a T type object, an empty optional object is returned.
*
* @tparam T
* @param key
* @return optional<T>
*/
template <typename T>
optional<T> getOptionalParameter(const std::string &key)
{
auto &params = getParameters();
auto it = params.find(key);
if (it != params.end())
{
try
{
return optional<T>(drogon::utils::fromString<T>(it->second));
}
catch (const std::exception &e)
{
LOG_ERROR << e.what();
return optional<T>{};
}
}
else
{
return optional<T>{};
}
}
/// Return the remote IP address and port
virtual const trantor::InetAddress &peerAddr() const = 0;
const trantor::InetAddress &getPeerAddr() const

View File

@ -24,6 +24,8 @@
#include <vector>
#include <set>
#include <limits>
#include <sstream>
#include <algorithm>
#ifdef _WIN32
#include <time.h>
char *strptime(const char *s, const char *f, struct tm *tm);
@ -31,6 +33,28 @@ time_t timegm(struct tm *tm);
#endif
namespace drogon
{
namespace internal
{
template <typename T>
struct CanConvertFromStringStream
{
private:
using yes = std::true_type;
using no = std::false_type;
template <typename U>
static auto test(U *p, std::stringstream &&ss)
-> decltype((ss >> *p), yes());
template <typename>
static no test(...);
public:
static constexpr bool value =
std::is_same<decltype(test<T>(nullptr, std::stringstream())),
yes>::value;
};
} // namespace internal
namespace utils
{
/// Determine if the string is an integer
@ -282,5 +306,105 @@ DROGON_EXPORT void replaceAll(std::string &s,
*/
DROGON_EXPORT bool secureRandomBytes(void *ptr, size_t size);
template <typename T>
typename std::enable_if<internal::CanConvertFromStringStream<T>::value, T>::type
fromString(const std::string &p) noexcept(false)
{
T value{};
if (!p.empty())
{
std::stringstream ss(p);
ss >> value;
}
return value;
}
template <typename T>
typename std::enable_if<!(internal::CanConvertFromStringStream<T>::value),
T>::type
fromString(const std::string &) noexcept(false)
{
throw std::runtime_error("Bad type conversion");
}
template <>
inline std::string fromString<std::string>(const std::string &p) noexcept(false)
{
return p;
}
template <>
inline int fromString<int>(const std::string &p) noexcept(false)
{
return std::stoi(p);
}
template <>
inline long fromString<long>(const std::string &p) noexcept(false)
{
return std::stol(p);
}
template <>
inline long long fromString<long long>(const std::string &p) noexcept(false)
{
return std::stoll(p);
}
template <>
inline unsigned long fromString<unsigned long>(const std::string &p) noexcept(
false)
{
return std::stoul(p);
}
template <>
inline unsigned long long fromString<unsigned long long>(
const std::string &p) noexcept(false)
{
return std::stoull(p);
}
template <>
inline float fromString<float>(const std::string &p) noexcept(false)
{
return std::stof(p);
}
template <>
inline double fromString<double>(const std::string &p) noexcept(false)
{
return std::stod(p);
}
template <>
inline long double fromString<long double>(const std::string &p) noexcept(false)
{
return std::stold(p);
}
template <>
inline bool fromString<bool>(const std::string &p) noexcept(false)
{
if (p == "1")
{
return true;
}
if (p == "0")
{
return false;
}
std::string l{p};
std::transform(p.begin(), p.end(), l.begin(), tolower);
if (l == "true")
{
return true;
}
else if (l == "false")
{
return false;
}
throw std::runtime_error("Can't convert from string '" + p + "' to bool");
}
} // namespace utils
} // namespace drogon