Fix bugs in HTTP view examples (#246)

This commit is contained in:
An Tao 2019-09-16 10:10:23 +08:00 committed by GitHub
parent 456d003482
commit 9855fc9b4f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 23 additions and 14 deletions

View File

@ -51,7 +51,8 @@ target_include_directories(
${PROJECT_NAME}
PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/lib/inc>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/orm_lib/inc>
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>)
$<BUILD_INTERFACE:${PROJECT_BINARY_DIR}>
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/trantor>)
# set the include list for dynamic view compiling
list(APPEND INCLUDE_DIRS_FOR_DYNAMIC_VIEW

View File

@ -1,7 +1,7 @@
<!DOCTYPE html>
<html>
<%c++
auto para=@@.get<std::map<std::string,std::string>>("parameters");
auto para=@@.get<std::unordered_map<std::string,std::string>>("parameters");
%>
<head>
<meta charset="UTF-8">

View File

@ -24,7 +24,7 @@ void ApiTest::get(const HttpRequestPtr &req,
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["p1"] = std::to_string(p1);
para["p2"] = p2;
data.insert("parameters", para);
@ -41,7 +41,7 @@ void ApiTest::your_method_name(
LOG_WARN << req->matchedPathPatternData();
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["p1"] = std::to_string(p1);
para["p2"] = std::to_string(p2);
para["p3"] = HttpViewData::htmlTranslate(string_view(

View File

@ -20,7 +20,7 @@ class A : public DrObjectBase
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["int p1"] = std::to_string(p1);
para["string p2"] = p2;
para["string p3"] = p3;
@ -40,7 +40,7 @@ class A : public DrObjectBase
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["int p1"] = std::to_string(p1);
para["string p2"] = p2;
para["string p3"] = p3;
@ -61,7 +61,7 @@ class B : public DrObjectBase
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["p1"] = std::to_string(p1);
para["p2"] = std::to_string(p2);
data.insert("parameters", para);
@ -91,7 +91,7 @@ class Test : public HttpController<Test>
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["p1"] = std::to_string(p1);
para["p2"] = std::to_string(p2);
data.insert("parameters", para);
@ -105,7 +105,7 @@ class Test : public HttpController<Test>
{
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["p1"] = std::to_string(p1);
para["p2"] = std::to_string(p2);
data.insert("parameters", para);
@ -146,7 +146,7 @@ int main()
float b) {
HttpViewData data;
data.insert("title", std::string("ApiTest::get"));
std::map<std::string, std::string> para;
std::unordered_map<std::string, std::string> para;
para["a"] = std::to_string(a);
para["b"] = std::to_string(b);
data.insert("parameters", para);

View File

@ -39,13 +39,13 @@ class HttpViewData
auto it = _viewData.find(key);
if (it != _viewData.end())
{
try
if (typeid(T) == it->second.type())
{
return *(any_cast<T>(&(it->second)));
}
catch (std::exception &e)
else
{
LOG_ERROR << e.what();
LOG_ERROR << "Bad type";
}
}
return nullVal;

View File

@ -15,6 +15,7 @@
#pragma once
#include <drogon/utils/any.h>
#include <trantor/utils/Logger.h>
#include <map>
#include <memory>
#include <mutex>
@ -45,7 +46,14 @@ class Session
auto it = _sessionMap.find(key);
if (it != _sessionMap.end())
{
return *(any_cast<T>(&(it->second)));
if (typeid(T) == it->second.type())
{
return *(any_cast<T>(&(it->second)));
}
else
{
LOG_ERROR << "Bad type";
}
}
return nullVal;
};