mirror of
https://gitee.com/zyjblog/oatpp.git
synced 2025-01-05 17:42:23 +08:00
Merge pull request #245 from oatpp/fix_enum_initialization
Fix enum initialization
This commit is contained in:
commit
3f3c3b1e47
@ -6,7 +6,7 @@ Please read carefully to prepare for migration.
|
||||
Feel free to ask questions - [Chat on Gitter](https://gitter.im/oatpp-framework/Lobby)
|
||||
|
||||
Contents:
|
||||
- [No more explicit ObjectWrapper](#no-more-explicit-objectwrapper)
|
||||
- [No More Explicit ObjectWrapper](#no-more-explicit-objectwrapper)
|
||||
- [Object-Mapping Simplified Primitives](#object-mapping-simplified-primitives)
|
||||
- [Object-Mapping std Collections](#object-mapping-std-collections)
|
||||
- [Type oatpp::Any](#type-oatppany)
|
||||
@ -14,9 +14,9 @@ Contents:
|
||||
- [DTO - Hashcode & Equals](#dto-hashcode-and-equals)
|
||||
- [DTO - Fields Annotation](#dto-fields-annotation)
|
||||
|
||||
## No more explicit ObjectWrapper
|
||||
## No More Explicit ObjectWrapper
|
||||
|
||||
Do not explicitly write `ObjectWrapper`
|
||||
Do not explicitly write `ObjectWrapper` for collections.
|
||||
|
||||
DTO:
|
||||
|
||||
@ -41,6 +41,27 @@ ENDPOINT("POST", "body-dto", postWithBody,
|
||||
}
|
||||
```
|
||||
|
||||
### Except for Cases
|
||||
|
||||
For `Object` in some cases, you still have to explicitly specify `ObjectWrapper` type.
|
||||
|
||||
Examples:
|
||||
|
||||
When declaring a variable or a function return type:
|
||||
|
||||
```cpp
|
||||
/* function parameters and return types */
|
||||
MyDto::ObjectWrapper foo(const MyDto::ObjectWrapper& parameter) {
|
||||
|
||||
/* declaring a variable */
|
||||
MyDto::ObjectWrapper myDto = MyDto::createShared();
|
||||
|
||||
auto myDto = MyDto::createShared(); // better.
|
||||
|
||||
return myDto;
|
||||
}
|
||||
```
|
||||
|
||||
## Object-Mapping Simplified Primitives
|
||||
|
||||
No more `<primitive>->getValue()`.
|
||||
|
@ -119,8 +119,6 @@ enum class NAME : ORDINAL_TYPE { \
|
||||
) \
|
||||
}; \
|
||||
\
|
||||
namespace { \
|
||||
\
|
||||
class Z__OATPP_ENUM_META_##NAME : public oatpp::data::mapping::type::EnumMeta<NAME> { \
|
||||
private: \
|
||||
\
|
||||
@ -141,9 +139,7 @@ public: \
|
||||
\
|
||||
}; \
|
||||
\
|
||||
bool Z__OATPP_ENUM_META_INITIALIZER_##NAME = Z__OATPP_ENUM_META_##NAME::initializer(); \
|
||||
\
|
||||
}
|
||||
static bool Z__OATPP_ENUM_META_INITIALIZER_##NAME = Z__OATPP_ENUM_META_##NAME::initializer();
|
||||
|
||||
// Chooser
|
||||
|
||||
|
@ -25,6 +25,7 @@
|
||||
#ifndef oatpp_data_mapping_type_Enum_hpp
|
||||
#define oatpp_data_mapping_type_Enum_hpp
|
||||
|
||||
#include "./Any.hpp"
|
||||
#include "./Primitive.hpp"
|
||||
#include "oatpp/core/data/share/MemoryLabel.hpp"
|
||||
|
||||
@ -88,6 +89,7 @@ namespace __class {
|
||||
virtual type::Void toInterpretation(const type::Void& enumValue, EnumInterpreterError& error) const = 0;
|
||||
virtual type::Void fromInterpretation(const type::Void& interValue, EnumInterpreterError& error) const = 0;
|
||||
virtual type::Type* getInterpretationType() const = 0;
|
||||
virtual std::vector<type::Any> getInterpretedEnum() const = 0;
|
||||
|
||||
};
|
||||
|
||||
@ -378,9 +380,7 @@ Void EnumInterpreterAsString<T, notnull>::fromInterpretation(const Void& interVa
|
||||
}
|
||||
|
||||
try {
|
||||
const auto &entry = EnumObjectWrapper<T, EnumInterpreterAsString<T, notnull>>::getEntryByName(
|
||||
interValue.staticCast<String>()
|
||||
);
|
||||
const auto &entry = EnumOW::getEntryByName(interValue.staticCast<String>());
|
||||
return EnumOW(entry.value);
|
||||
} catch (const std::runtime_error& e) { // TODO - add a specific error for this.
|
||||
error = EnumInterpreterError::ENTRY_NOT_FOUND;
|
||||
@ -479,6 +479,24 @@ namespace __class {
|
||||
return Interpreter::getInterpretationType();
|
||||
}
|
||||
|
||||
std::vector<type::Any> getInterpretedEnum() const override {
|
||||
|
||||
typedef type::EnumObjectWrapper<T, Interpreter> EnumOW;
|
||||
|
||||
std::vector<type::Any> result({});
|
||||
|
||||
for(const auto& e : EnumOW::getEntries()) {
|
||||
type::EnumInterpreterError error = type::EnumInterpreterError::OK;
|
||||
result.push_back(type::Any(toInterpretation(EnumOW(e.value), error)));
|
||||
if(error != type::EnumInterpreterError::OK) {
|
||||
throw std::runtime_error("[oatpp::data::mapping::type::__class::Enum<T, Interpreter>::getInterpretedEnum()]: Unknown error.");
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
private:
|
||||
|
@ -153,6 +153,12 @@ void EnumTest::onRun() {
|
||||
OATPP_ASSERT(pd1->notNull == false);
|
||||
OATPP_ASSERT(pd2->notNull == true);
|
||||
|
||||
{
|
||||
auto interEnum = pd1->getInterpretedEnum();
|
||||
OATPP_ASSERT(interEnum.size() == 3);
|
||||
OATPP_ASSERT(interEnum[0].getStoredType() == oatpp::String::Class::getType());
|
||||
}
|
||||
|
||||
OATPP_LOGI(TAG, "OK");
|
||||
}
|
||||
|
||||
|
@ -303,6 +303,9 @@ void FullTest::onRun() {
|
||||
}
|
||||
|
||||
{ // test Enum as String
|
||||
|
||||
OATPP_ASSERT(oatpp::Enum<app::AllowedPathParams>::getEntries().size() == 2);
|
||||
|
||||
oatpp::Enum<app::AllowedPathParams> v = app::AllowedPathParams::HELLO;
|
||||
auto response = client->getHeaderEnumAsString(v);
|
||||
OATPP_ASSERT(response->getStatusCode() == 200);
|
||||
|
Loading…
Reference in New Issue
Block a user