Skip to content

Commit

Permalink
fix: support boolean value for service info parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
Bycob committed Jul 5, 2023
1 parent 71cb66a commit 737724d
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 9 deletions.
39 changes: 31 additions & 8 deletions src/http/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,14 @@
#include <vector>
#include <iostream>

#include <boost/lexical_cast.hpp>

#include "oatpp/web/server/api/ApiController.hpp"
#include "oatpp/parser/json/mapping/ObjectMapper.hpp"
#include "oatpp/core/macro/codegen.hpp"
#include "oatpp/core/macro/component.hpp"

#include "apidata.h"
#include "oatppjsonapi.h"
#include "utils/utils.hpp"
#include "dto/info.hpp"
#include "dto/service_predict.hpp"
#include "dto/service_create.hpp"
Expand Down Expand Up @@ -77,8 +76,16 @@ class DedeController : public oatpp::web::server::api::ApiController

oatpp::String qs_status = queryParams.get("status");
bool status = false;
if (qs_status)
status = boost::lexical_cast<bool>(*qs_status);
try
{
if (qs_status)
status = dd::dd_utils::parse_bool(*qs_status);
}
catch (boost::bad_lexical_cast &)
{
return _oja->response_bad_request_400(
"status must be a boolean value");
}

auto hit = _oja->_mlservices.begin();
while (hit != _oja->_mlservices.end())
Expand All @@ -101,13 +108,29 @@ class DedeController : public oatpp::web::server::api::ApiController
{
oatpp::String qs_status = queryParams.get("status");
bool status = true;
if (qs_status)
status = boost::lexical_cast<bool>(*qs_status);
try
{
if (qs_status)
status = dd::dd_utils::parse_bool(*qs_status);
}
catch (boost::bad_lexical_cast &)
{
return _oja->response_bad_request_400(
"status must be a boolean value");
}

oatpp::String qs_labels = queryParams.get("labels");
bool labels = false;
if (qs_labels)
labels = boost::lexical_cast<bool>(*qs_labels);
try
{
if (qs_labels)
labels = dd::dd_utils::parse_bool(*qs_labels);
}
catch (boost::bad_lexical_cast &)
{
return _oja->response_bad_request_400(
"labels must be a boolean value");
}

auto janswer = _oja->service_status(service_name, status, labels);
return _oja->jdoc_to_response(janswer);
Expand Down
21 changes: 21 additions & 0 deletions src/utils/utils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
#include <vector>
#include <algorithm>

#include <boost/lexical_cast.hpp>

namespace dd
{
namespace dd_utils
Expand Down Expand Up @@ -102,6 +104,25 @@ namespace dd
return count == 1;
}

/** boost::lexical_cast<bool> but accept "true" and "false" */
inline bool parse_bool(const std::string &str)
{
try
{
return boost::lexical_cast<bool>(str);
}
catch (boost::bad_lexical_cast &)
{
if (str == "true")
return true;
else if (str == "false")
return false;
else
throw;
}
return false;
}

#ifdef WIN32
inline int my_hardware_concurrency()
{
Expand Down
12 changes: 11 additions & 1 deletion tests/ut-oatpp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
ASSERT_EQ(201, d["status"]["code"].GetInt());

// service info
response = client->get_service_with_labels(serv.c_str(), "1");
response = client->get_service_with_labels(serv.c_str(), "true");
message = response->readBodyToString();
ASSERT_TRUE(message != nullptr);
std::cout << "jstr=" << *message << std::endl;
Expand All @@ -102,6 +102,16 @@ void test_services(std::shared_ptr<DedeApiTestClient> client)
ASSERT_TRUE(d["body"].HasMember("labels"));
ASSERT_EQ(d["body"]["labels"].Size(), 0);

// test other boolean values
response = client->get_service_with_labels(serv.c_str(), "1");
ASSERT_EQ(response->getStatusCode(), 200);
response = client->get_service_with_labels(serv.c_str(), "false");
ASSERT_EQ(response->getStatusCode(), 200);
response = client->get_service_with_labels(serv.c_str(), "0");
ASSERT_EQ(response->getStatusCode(), 200);
response = client->get_service_with_labels(serv.c_str(), "flase");
ASSERT_EQ(response->getStatusCode(), 400);

// info call
response = client->get_info();
message = response->readBodyToString();
Expand Down

0 comments on commit 737724d

Please sign in to comment.