ApiController. Integrate oatpp::Enum with ApiController.

This commit is contained in:
lganzzzo 2020-05-09 05:07:21 +03:00
parent 48c1fcce05
commit a124a5c510
5 changed files with 154 additions and 88 deletions

View File

@ -74,7 +74,7 @@ if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing HEADER parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
const auto& NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto& NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid HEADER parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
@ -86,7 +86,7 @@ if(!__param_str_val_##NAME){ \
oatpp::String("Missing HEADER parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
const auto& NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto& NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid HEADER parameter '") + \
@ -117,7 +117,7 @@ if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing PATH parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
const auto& NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto& NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid PATH parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
@ -129,7 +129,7 @@ if(!__param_str_val_##NAME){ \
oatpp::String("Missing PATH parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
const auto NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid PATH parameter '") + \
@ -166,7 +166,7 @@ if(!__param_str_val_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Missing QUERY parameter '" #NAME "'"); \
} \
bool __param_validation_check_##NAME; \
const auto& NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto& NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, "Invalid QUERY parameter '" #NAME "'. Expected type is '" #TYPE "'"); \
}
@ -178,7 +178,7 @@ if(!__param_str_val_##NAME){ \
oatpp::String("Missing QUERY parameter '") + QUALIFIER + "'"); \
} \
bool __param_validation_check_##NAME; \
const auto& NAME = parseParameterFromString<TYPE>(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
const auto& NAME = ApiController::TypeInterpretation<TYPE>::fromString(#TYPE, __param_str_val_##NAME, __param_validation_check_##NAME); \
if(!__param_validation_check_##NAME){ \
return ApiController::handleError(Status::CODE_400, \
oatpp::String("Invalid QUERY parameter '") + \

View File

@ -133,6 +133,8 @@ protected:
template<class T, bool notnull>
class EnumInterpreterAsString {
public:
typedef String UnderlyingTypeObjectWrapper;
public:
template <bool N>
using InterpreterType = EnumInterpreterAsString<T, N>;
@ -146,6 +148,10 @@ public:
template<class T, bool notnull>
class EnumInterpreterAsInteger {
private:
typedef typename std::underlying_type<T>::type EnumUnderlyingType;
public:
typedef typename ObjectWrapperByUnderlyingType<EnumUnderlyingType>::ObjectWrapper UnderlyingTypeObjectWrapper;
public:
template <bool N>
using InterpreterType = EnumInterpreterAsInteger<T, N>;

View File

@ -450,91 +450,140 @@ public:
public:
template<typename T>
T parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) text;
success = false;
OATPP_LOGE("[oatpp::web::server::api::ApiController::parseParameterFromString()]",
"Error. No conversion from '%s' to '%s' is defined.", "oatpp::String", typeName->getData());
throw std::runtime_error("[oatpp::web::server::api::ApiController::parseParameterFromString()]: Error. "
"No conversion from 'oatpp::String' to '" + typeName->std_str() + "' is defined. "
"Please define type conversion.");
}
struct TypeInterpretation {
static T fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) text;
success = false;
OATPP_LOGE("[oatpp::web::server::api::ApiController::TypeInterpretation::fromString()]",
"Error. No conversion from '%s' to '%s' is defined.", "oatpp::String", typeName->getData());
throw std::runtime_error("[oatpp::web::server::api::ApiController::TypeInterpretation::fromString()]: Error. "
"No conversion from 'oatpp::String' to '" + typeName->std_str() + "' is defined. "
"Please define type conversion.");
}
};
template<>
struct TypeInterpretation <oatpp::String> {
static oatpp::String fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
success = true;
return text;
}
};
template<>
struct TypeInterpretation <oatpp::Int8> {
static oatpp::Int8 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::UInt8> {
static oatpp::UInt8 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Int16> {
static oatpp::Int16 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::UInt16> {
static oatpp::UInt16 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Int32> {
static oatpp::Int32 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::UInt32> {
static oatpp::UInt32 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Int64> {
static oatpp::Int64 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt64(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::UInt64> {
static oatpp::UInt64 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt64(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Float32> {
static oatpp::Float32 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToFloat32(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Float64> {
static oatpp::Float64 fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToFloat64(text, success);
}
};
template<>
struct TypeInterpretation <oatpp::Boolean> {
static oatpp::Boolean fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToBool(text, success);
}
};
template<class T, class I>
struct TypeInterpretation <data::mapping::type::EnumObjectWrapper<T, I>> {
typedef data::mapping::type::EnumObjectWrapper<T, I> EnumOW;
typedef typename I::UnderlyingTypeObjectWrapper UTOW;
static EnumOW fromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
const auto& parsedValue = ApiController::TypeInterpretation<UTOW>::fromString(typeName, text, success);
if(success) {
data::mapping::type::EnumInterpreterError error = data::mapping::type::EnumInterpreterError::OK;
const auto& result = I::fromInterpretation(parsedValue, error);
if(error == data::mapping::type::EnumInterpreterError::OK) {
return result.template staticCast<EnumOW>();
}
success = false;
}
return nullptr;
}
};
};
template<>
inline oatpp::String ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
success = true;
return text;
}
template<>
inline oatpp::Int8 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
template<>
inline oatpp::UInt8 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
template<>
inline oatpp::Int16 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
template<>
inline oatpp::UInt16 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
template<>
inline oatpp::Int32 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt32(text, success);
}
template<>
inline oatpp::UInt32 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt32(text, success);
}
template<>
inline oatpp::Int64 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToInt64(text, success);
}
template<>
inline oatpp::UInt64 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToUInt64(text, success);
}
template<>
inline oatpp::Float32 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToFloat32(text, success);
}
template<>
inline oatpp::Float64 ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToFloat64(text, success);
}
template<>
inline oatpp::Boolean ApiController::parseParameterFromString(const oatpp::String& typeName, const oatpp::String& text, bool& success) {
(void) typeName;
return utils::conversion::strToBool(text, success);
}
}}}}
#endif /* oatpp_web_server_rest_Controller_hpp */

View File

@ -282,6 +282,12 @@ public:
return createResponse(Status::CODE_200, "OK");
}
ENDPOINT("GET", "enum/{enum}", testEnum,
PATH(oatpp::Enum<AllowedPathParams>::AsInteger, enumValue, "enum"))
{
return createResponse(Status::CODE_200, "OK");
}
};

View File

@ -42,6 +42,11 @@ class TestDto : public oatpp::data::mapping::type::Object {
};
ENUM(AllowedPathParams, v_int32,
VALUE(HELLO, 100, "hello"),
VALUE(WORLD, 200, "world")
)
#include OATPP_CODEGEN_END(DTO)
}}}}