Skip to content

Commit d434bfe

Browse files
committed
mapping::Deserializer. Deserialize oatpp::Any.
1 parent a92d624 commit d434bfe

File tree

5 files changed

+77
-30
lines changed

5 files changed

+77
-30
lines changed

src/oatpp-postgresql/mapping/Deserializer.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ Deserializer::Deserializer() {
3939
m_methods.resize(data::mapping::type::ClassId::getClassCount(), nullptr);
4040

4141
setDeserializerMethod(data::mapping::type::__class::String::CLASS_ID, &Deserializer::deserializeString);
42-
setDeserializerMethod(data::mapping::type::__class::Any::CLASS_ID, nullptr);
42+
setDeserializerMethod(data::mapping::type::__class::Any::CLASS_ID, &Deserializer::deserializeAny);
4343

4444
setDeserializerMethod(data::mapping::type::__class::Int8::CLASS_ID, &Deserializer::deserializeInt<oatpp::Int8>);
4545
setDeserializerMethod(data::mapping::type::__class::UInt8::CLASS_ID, &Deserializer::deserializeInt<oatpp::UInt8>);
@@ -84,7 +84,7 @@ oatpp::Void Deserializer::deserialize(const InData& data, const Type* type) cons
8484
auto& method = m_methods[id];
8585

8686
if(method) {
87-
return (*method)(data, type);
87+
return (*method)(this, data, type);
8888
}
8989

9090
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deserialize()]: "
@@ -131,12 +131,24 @@ v_int64 Deserializer::deInt(const InData& data) {
131131
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deInt()]: Error. Unknown OID.");
132132
}
133133

134-
oatpp::Void Deserializer::deserializeString(const InData& data, const Type* type) {
134+
oatpp::Void Deserializer::deserializeString(const Deserializer* _this, const InData& data, const Type* type) {
135+
(void) _this;
135136
(void) type;
136137
switch(data.oid) {
137138
case TEXTOID: return oatpp::String(data.data, data.size, true);
138139
}
139140
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deserializeString()]: Error. Unknown OID.");
140141
}
141142

143+
oatpp::Void Deserializer::deserializeAny(const Deserializer* _this, const InData& data, const Type* type) {
144+
(void) type;
145+
const Type* valueType = _this->m_typeMapper.getOidType(data.oid);
146+
if(valueType == nullptr) {
147+
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deserializeAny()]: Error. Unknown OID.");
148+
}
149+
auto value = _this->deserialize(data, valueType);
150+
auto anyHandle = std::make_shared<data::mapping::type::AnyHandle>(value.getPtr(), value.valueType);
151+
return oatpp::Void(anyHandle, Any::Class::getType());
152+
}
153+
142154
}}}

src/oatpp-postgresql/mapping/Deserializer.hpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#ifndef oatpp_postgresql_mapping_Deserializer_hpp
2626
#define oatpp_postgresql_mapping_Deserializer_hpp
2727

28+
#include "TypeMapper.hpp"
2829
#include "oatpp/core/Types.hpp"
2930

3031
#include <libpq-fe.h>
@@ -41,14 +42,15 @@ class Deserializer {
4142
v_buff_size size;
4243
};
4344
public:
44-
typedef oatpp::Void (*DeserializerMethod)(const InData& data, const Type* type);
45+
typedef oatpp::Void (*DeserializerMethod)(const Deserializer*, const InData&, const Type*);
4546
private:
4647
static v_int16 deInt2(const InData& data);
4748
static v_int32 deInt4(const InData& data);
4849
static v_int64 deInt8(const InData& data);
4950
static v_int64 deInt(const InData& data);
5051
private:
5152
std::vector<DeserializerMethod> m_methods;
53+
TypeMapper m_typeMapper;
5254
public:
5355

5456
Deserializer();
@@ -59,10 +61,13 @@ class Deserializer {
5961

6062
public:
6163

62-
static oatpp::Void deserializeString(const InData& data, const Type* type);
64+
static oatpp::Void deserializeString(const Deserializer* _this, const InData& data, const Type* type);
65+
66+
static oatpp::Void deserializeAny(const Deserializer* _this, const InData& data, const Type* type);
6367

6468
template<class IntWrapper>
65-
static oatpp::Void deserializeInt(const InData& data, const Type* type) {
69+
static oatpp::Void deserializeInt(const Deserializer* _this, const InData& data, const Type* type) {
70+
(void) _this;
6671
(void) type;
6772
auto value = deInt(data);
6873
return IntWrapper((typename IntWrapper::UnderlyingType) value);

src/oatpp-postgresql/mapping/TypeMapper.cpp

Lines changed: 49 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,51 @@ namespace oatpp { namespace postgresql { namespace mapping {
3030

3131
TypeMapper::TypeMapper() {
3232

33-
m_oids.resize(data::mapping::type::ClassId::getClassCount(), 0);
33+
{
34+
m_oids.resize(data::mapping::type::ClassId::getClassCount(), 0);
3435

35-
setTypeOid(data::mapping::type::__class::String::CLASS_ID, TEXTOID);
36-
setTypeOid(data::mapping::type::__class::Any::CLASS_ID, 0);
36+
setTypeOid(data::mapping::type::__class::String::CLASS_ID, TEXTOID);
37+
setTypeOid(data::mapping::type::__class::Any::CLASS_ID, 0);
3738

38-
setTypeOid(data::mapping::type::__class::Int8::CLASS_ID, INT2OID);
39-
setTypeOid(data::mapping::type::__class::UInt8::CLASS_ID, INT2OID);
39+
setTypeOid(data::mapping::type::__class::Int8::CLASS_ID, INT2OID);
40+
setTypeOid(data::mapping::type::__class::UInt8::CLASS_ID, INT2OID);
4041

41-
setTypeOid(data::mapping::type::__class::Int16::CLASS_ID, INT2OID);
42-
setTypeOid(data::mapping::type::__class::UInt16::CLASS_ID, INT4OID);
42+
setTypeOid(data::mapping::type::__class::Int16::CLASS_ID, INT2OID);
43+
setTypeOid(data::mapping::type::__class::UInt16::CLASS_ID, INT4OID);
4344

44-
setTypeOid(data::mapping::type::__class::Int32::CLASS_ID, INT4OID);
45-
setTypeOid(data::mapping::type::__class::UInt32::CLASS_ID, INT8OID);
45+
setTypeOid(data::mapping::type::__class::Int32::CLASS_ID, INT4OID);
46+
setTypeOid(data::mapping::type::__class::UInt32::CLASS_ID, INT8OID);
4647

47-
setTypeOid(data::mapping::type::__class::Int64::CLASS_ID, INT8OID);
48-
setTypeOid(data::mapping::type::__class::UInt64::CLASS_ID, 0);
48+
setTypeOid(data::mapping::type::__class::Int64::CLASS_ID, INT8OID);
49+
setTypeOid(data::mapping::type::__class::UInt64::CLASS_ID, 0);
4950

50-
setTypeOid(data::mapping::type::__class::Float32::CLASS_ID, FLOAT4OID);
51-
setTypeOid(data::mapping::type::__class::Float64::CLASS_ID, FLOAT8OID);
52-
setTypeOid(data::mapping::type::__class::Boolean::CLASS_ID, BOOLOID);
51+
setTypeOid(data::mapping::type::__class::Float32::CLASS_ID, FLOAT4OID);
52+
setTypeOid(data::mapping::type::__class::Float64::CLASS_ID, FLOAT8OID);
53+
setTypeOid(data::mapping::type::__class::Boolean::CLASS_ID, BOOLOID);
5354

54-
setTypeOid(data::mapping::type::__class::AbstractObject::CLASS_ID, 0);
55-
setTypeOid(data::mapping::type::__class::AbstractEnum::CLASS_ID, 0);
55+
setTypeOid(data::mapping::type::__class::AbstractObject::CLASS_ID, 0);
56+
setTypeOid(data::mapping::type::__class::AbstractEnum::CLASS_ID, 0);
5657

57-
setTypeOid(data::mapping::type::__class::AbstractVector::CLASS_ID, 0);
58-
setTypeOid(data::mapping::type::__class::AbstractList::CLASS_ID, 0);
59-
setTypeOid(data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, 0);
58+
setTypeOid(data::mapping::type::__class::AbstractVector::CLASS_ID, 0);
59+
setTypeOid(data::mapping::type::__class::AbstractList::CLASS_ID, 0);
60+
setTypeOid(data::mapping::type::__class::AbstractUnorderedSet::CLASS_ID, 0);
6061

61-
setTypeOid(data::mapping::type::__class::AbstractPairList::CLASS_ID, 0);
62-
setTypeOid(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, 0);
62+
setTypeOid(data::mapping::type::__class::AbstractPairList::CLASS_ID, 0);
63+
setTypeOid(data::mapping::type::__class::AbstractUnorderedMap::CLASS_ID, 0);
64+
}
65+
66+
{
67+
setOidType(TEXTOID, oatpp::String::Class::getType());
68+
69+
setOidType(INT2OID, oatpp::Int16::Class::getType());
70+
setOidType(INT4OID, oatpp::Int32::Class::getType());
71+
setOidType(INT8OID, oatpp::Int64::Class::getType());
72+
73+
setOidType(FLOAT4OID, oatpp::Float32::Class::getType());
74+
setOidType(FLOAT8OID, oatpp::Float64::Class::getType());
75+
76+
setOidType(BOOLOID, oatpp::Boolean::Class::getType());
77+
}
6378

6479
}
6580

@@ -72,12 +87,24 @@ void TypeMapper::setTypeOid(const data::mapping::type::ClassId& classId, Oid oid
7287
}
7388
}
7489

75-
Oid TypeMapper::getTypeOid(data::mapping::type::Type* type) const {
90+
void TypeMapper::setOidType(Oid oid, const data::mapping::type::Type* type) {
91+
m_types[oid] = type;
92+
}
93+
94+
Oid TypeMapper::getTypeOid(const data::mapping::type::Type* type) const {
7695
const v_uint32 id = type->classId.id;
7796
if(id < m_oids.size()) {
7897
return m_oids[id];
7998
}
8099
throw std::runtime_error("[oatpp::postgresql::mapping::TypeMapper::getTypeOid()]: Error. Unknown classId");
81100
}
82101

102+
const data::mapping::type::Type* TypeMapper::getOidType(Oid oid) const {
103+
auto it = m_types.find(oid);
104+
if(it != m_types.end()) {
105+
return it->second;
106+
}
107+
return nullptr;
108+
}
109+
83110
}}}

src/oatpp-postgresql/mapping/TypeMapper.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,16 @@ namespace oatpp { namespace postgresql { namespace mapping {
3434
class TypeMapper {
3535
private:
3636
std::vector<Oid> m_oids;
37+
std::unordered_map<Oid, const data::mapping::type::Type*> m_types;
3738
public:
3839

3940
TypeMapper();
4041

4142
void setTypeOid(const data::mapping::type::ClassId& classId, Oid oid);
43+
void setOidType(Oid oid, const data::mapping::type::Type* type);
4244

43-
Oid getTypeOid(data::mapping::type::Type* type) const;
45+
Oid getTypeOid(const data::mapping::type::Type* type) const;
46+
const data::mapping::type::Type* getOidType(Oid oid) const;
4447

4548
};
4649

test/oatpp-postgresql/tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class Test : public oatpp::test::UnitTest {
107107
OATPP_LOGD(TAG, "OK=%d, count=%d", res->isSuccess(), res->count());
108108

109109
{
110-
oatpp::Vector<oatpp::Object<Ints>> resObjType;
110+
oatpp::Vector<oatpp::Fields<oatpp::Any>> resObjType;
111111
oatpp::Void polymorph(resObjType.valueType);
112112

113113
res->fetch(polymorph, res->count());

0 commit comments

Comments
 (0)