Skip to content

Commit d606713

Browse files
committed
uuid. Use oatpp::encoding::Hex.
1 parent 7a6f9c7 commit d606713

File tree

5 files changed

+91
-57
lines changed

5 files changed

+91
-57
lines changed

src/oatpp-postgresql/Types.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ namespace oatpp { namespace postgresql {
3232
/**
3333
* Uuid as oatpp primitive type.
3434
*/
35-
typedef oatpp::data::mapping::type::Primitive<mapping::type::Uuid, mapping::type::__class::Uuid> Uuid;
35+
typedef oatpp::data::mapping::type::Primitive<mapping::type::UuidObject, mapping::type::__class::Uuid> Uuid;
3636

3737
}}
3838

src/oatpp-postgresql/mapping/Deserializer.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -156,10 +156,6 @@ oatpp::Void Deserializer::deserializeString(const Deserializer* _this, const InD
156156
switch(data.oid) {
157157
case TEXTOID:
158158
case VARCHAROID: return oatpp::String(data.data, data.size, true);
159-
case UUIDOID: {
160-
postgresql::mapping::type::Uuid uuid((p_char8)data.data);
161-
return uuid.toString();
162-
}
163159
}
164160

165161
throw std::runtime_error("[oatpp::postgresql::mapping::Deserializer::deserializeString()]: Error. Unknown OID.");

src/oatpp-postgresql/mapping/type/Uuid.cpp

Lines changed: 43 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -23,52 +23,69 @@
2323
***************************************************************************/
2424

2525
#include "Uuid.hpp"
26+
#include "oatpp/encoding/Hex.hpp"
2627
#include "oatpp/core/data/stream/BufferStream.hpp"
2728

2829
namespace oatpp { namespace postgresql { namespace mapping { namespace type {
2930

30-
namespace __class {
31-
const oatpp::ClassId Uuid::CLASS_ID("oatpp::postgresql::Uuid");
31+
UuidObject::UuidObject(v_char8 data[DATA_SIZE]) {
32+
std::memcpy(m_data, data, DATA_SIZE);
3233
}
3334

34-
const char* const Uuid::ALPHABET = "0123456789abcdef";
35-
36-
Uuid::Uuid(v_char8 data[DATA_SIZE]) {
37-
std::memcpy(m_data, data, DATA_SIZE);
35+
UuidObject::UuidObject(const oatpp::String& text) {
36+
data::stream::BufferOutputStream stream(16);
37+
encoding::Hex::decode(&stream, text->getData(), text->getSize(), true);
38+
if(stream.getCurrentPosition() != 16) {
39+
throw std::runtime_error("[oatpp::postgresql::mapping::type::UuidObject::UuidObject()]: Error. Invalid string.");
40+
}
41+
std::memcpy(m_data, stream.getData(), DATA_SIZE);
3842
}
3943

40-
const p_char8 Uuid::getData() const {
44+
const p_char8 UuidObject::getData() const {
4145
return (const p_char8) m_data;
4246
}
4347

44-
v_buff_size Uuid::getSize() const {
48+
v_buff_size UuidObject::getSize() const {
4549
return DATA_SIZE;
4650
}
4751

48-
oatpp::String Uuid::toString() const {
49-
oatpp::String result(DATA_SIZE * 2 + 4);
50-
p_char8 rdata = result->getData();
51-
v_int32 shift = 0;
52-
for(v_buff_size i = 0; i < DATA_SIZE; i ++) {
53-
if(i == 4 || i == 6 || i == 8 || i == 10) {
54-
rdata[shift + i * 2] = '-';
55-
++ shift;
56-
}
57-
auto a = m_data[i];
58-
v_char8 b1 = 0x0F & (a >> 4);
59-
v_char8 b2 = 0x0F & (a);
60-
rdata[shift + i * 2 ] = ALPHABET[b1];
61-
rdata[shift + i * 2 + 1] = ALPHABET[b2];
62-
}
63-
return result;
52+
oatpp::String UuidObject::toString() const {
53+
auto alphabet = encoding::Hex::ALPHABET_LOWER;
54+
data::stream::BufferOutputStream stream(36);
55+
encoding::Hex::encode(&stream, &m_data[0], 4, alphabet);
56+
stream.writeCharSimple('-');
57+
encoding::Hex::encode(&stream, &m_data[4], 2, alphabet);
58+
stream.writeCharSimple('-');
59+
encoding::Hex::encode(&stream, &m_data[6], 2, alphabet);
60+
stream.writeCharSimple('-');
61+
encoding::Hex::encode(&stream, &m_data[8], 2, alphabet);
62+
stream.writeCharSimple('-');
63+
encoding::Hex::encode(&stream, &m_data[10], 6, alphabet);
64+
return stream.toString();
6465
}
6566

66-
bool Uuid::operator==(const Uuid &other) const {
67+
bool UuidObject::operator==(const UuidObject &other) const {
6768
return std::memcmp(m_data, other.m_data, DATA_SIZE) == 0;
6869
}
6970

70-
bool Uuid::operator!=(const Uuid &other) const {
71+
bool UuidObject::operator!=(const UuidObject &other) const {
7172
return !operator==(other);
7273
}
7374

75+
namespace __class {
76+
77+
const oatpp::ClassId Uuid::CLASS_ID("oatpp::postgresql::Uuid");
78+
79+
oatpp::Type* Uuid::getType() {
80+
static Type type(
81+
CLASS_ID, nullptr, nullptr, nullptr, nullptr,
82+
{
83+
{"postgresql", new Inter()}
84+
}
85+
);
86+
return &type;
87+
}
88+
89+
}
90+
7491
}}}}

src/oatpp-postgresql/mapping/type/Uuid.hpp

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -30,29 +30,15 @@
3030
namespace oatpp { namespace postgresql { namespace mapping { namespace type {
3131

3232
namespace __class {
33-
34-
class Uuid {
35-
public:
36-
37-
static const oatpp::ClassId CLASS_ID;
38-
39-
static oatpp::Type* getType() {
40-
static oatpp::Type type(CLASS_ID, nullptr);
41-
return &type;
42-
}
43-
44-
};
45-
33+
class Uuid;
4634
}
4735

48-
class Uuid {
36+
class UuidObject {
4937
public:
5038
/**
5139
* Size of UUID data in bytes.
5240
*/
5341
static constexpr v_buff_size DATA_SIZE = 16;
54-
private:
55-
static const char* const ALPHABET;
5642
private:
5743
v_char8 m_data[DATA_SIZE];
5844
public:
@@ -61,7 +47,13 @@ class Uuid {
6147
* Constructor.
6248
* @param data
6349
*/
64-
Uuid(v_char8 data[DATA_SIZE]);
50+
UuidObject(v_char8 data[DATA_SIZE]);
51+
52+
/**
53+
* Constructor.
54+
* @param text
55+
*/
56+
UuidObject(const oatpp::String& text);
6557

6658
/**
6759
* Get raw data of ObjectId.
@@ -81,11 +73,40 @@ class Uuid {
8173
*/
8274
oatpp::String toString() const;
8375

84-
bool operator==(const Uuid &other) const;
85-
bool operator!=(const Uuid &other) const;
76+
bool operator==(const UuidObject &other) const;
77+
bool operator!=(const UuidObject &other) const;
8678

8779
};
8880

81+
typedef oatpp::data::mapping::type::Primitive<UuidObject, __class::Uuid> Uuid;
82+
83+
namespace __class {
84+
85+
class Uuid {
86+
public:
87+
88+
class Inter : public oatpp::Type::Interpretation<type::Uuid, oatpp::String> {
89+
public:
90+
91+
oatpp::String interpret(const type::Uuid& value) const override {
92+
return value->toString();
93+
}
94+
95+
type::Uuid reproduce(const oatpp::String& value) const override {
96+
return std::make_shared<UuidObject>(value);
97+
}
98+
99+
};
100+
101+
public:
102+
103+
static const oatpp::ClassId CLASS_ID;
104+
static oatpp::Type* getType();
105+
106+
};
107+
108+
}
109+
89110
}}}}
90111

91112
#endif // oatpp_postgresql_mapping_type_Uuid_hpp

test/oatpp-postgresql/tests.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class MyClient : public oatpp::orm::DbClient {
7373
PARAM(oatpp::String, email),
7474
PREPARE(true))
7575

76-
QUERY(selectUsers, "SELECT userId FROM EXAMPLE_USER", PREPARE(true))
76+
QUERY(selectUsers, "SELECT * FROM EXAMPLE_USER", PREPARE(true))
7777

7878
QUERY(insertStrs,
7979
"INSERT INTO test_strs "
@@ -146,12 +146,11 @@ class Test : public oatpp::test::UnitTest {
146146

147147
{
148148
v_char8 data1[16] = "012345670123456";
149-
v_char8 data2[16] = "012345670123457";
150149
oatpp::postgresql::Uuid uuid1(data1);
151-
oatpp::postgresql::Uuid uuid2(data2);
150+
oatpp::postgresql::Uuid uuid2(oatpp::String("8e6d32f7-aca2-4601-b89a-f2c55b010962"));
152151

153-
auto text = uuid1->toString();
154-
OATPP_LOGD(TAG, "uuid='%s'", text->c_str());
152+
OATPP_LOGD(TAG, "uuid1='%s'", uuid1->toString()->c_str());
153+
OATPP_LOGD(TAG, "uuid2='%s'", uuid2->toString()->c_str());
155154

156155
if(uuid1 == uuid2) {
157156
OATPP_LOGD(TAG, "eq");
@@ -195,8 +194,8 @@ class Test : public oatpp::test::UnitTest {
195194
{
196195

197196
//auto res = client.createUser("admin1", "AdMiN", "admin1@admin.com");
198-
//auto res = client.selectUsers();
199-
auto res = client.insertMultipleUsers();
197+
auto res = client.selectUsers();
198+
//auto res = client.insertMultipleUsers();
200199

201200
if(res->isSuccess()) {
202201
OATPP_LOGD(TAG, "OK, count=%d", res->getCount());
@@ -209,6 +208,7 @@ class Test : public oatpp::test::UnitTest {
209208

210209
oatpp::parser::json::mapping::ObjectMapper om;
211210
om.getSerializer()->getConfig()->useBeautifier = true;
211+
om.getSerializer()->getConfig()->enableInterpretations = {"postgresql"};
212212

213213
auto str = om.writeToString(dataset);
214214

0 commit comments

Comments
 (0)