diff options
-rw-r--r-- | common/domainadaptor.cpp | 23 | ||||
-rw-r--r-- | common/domainadaptor.h | 95 | ||||
-rw-r--r-- | dummyresource/domainadaptor.cpp | 91 | ||||
-rw-r--r-- | dummyresource/domainadaptor.h | 6 | ||||
-rw-r--r-- | dummyresource/facade.cpp | 2 | ||||
-rw-r--r-- | dummyresource/facade.h | 2 | ||||
-rw-r--r-- | tests/domainadaptortest.cpp | 96 |
7 files changed, 101 insertions, 214 deletions
diff --git a/common/domainadaptor.cpp b/common/domainadaptor.cpp index 5e6f062..aa8c3d9 100644 --- a/common/domainadaptor.cpp +++ b/common/domainadaptor.cpp | |||
@@ -20,6 +20,15 @@ | |||
20 | #include "domainadaptor.h" | 20 | #include "domainadaptor.h" |
21 | 21 | ||
22 | template <> | 22 | template <> |
23 | flatbuffers::uoffset_t extractProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
24 | { | ||
25 | if (property.isValid()) { | ||
26 | return fbb.CreateString(property.toString().toStdString()).o; | ||
27 | } | ||
28 | return 0; | ||
29 | } | ||
30 | |||
31 | template <> | ||
23 | QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>() | 32 | QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > initializeReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event>() |
24 | { | 33 | { |
25 | auto propertyMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); | 34 | auto propertyMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); |
@@ -38,3 +47,17 @@ QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > | |||
38 | return propertyMapper; | 47 | return propertyMapper; |
39 | } | 48 | } |
40 | 49 | ||
50 | template <> | ||
51 | QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>() | ||
52 | { | ||
53 | auto propertyMapper = QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> >::create(); | ||
54 | propertyMapper->addMapping("summary", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> { | ||
55 | auto offset = extractProperty<QString>(value, fbb); | ||
56 | return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_summary(offset); }; | ||
57 | }); | ||
58 | propertyMapper->addMapping("uid", [](const QVariant &value, flatbuffers::FlatBufferBuilder &fbb) -> std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> { | ||
59 | auto offset = extractProperty<QString>(value, fbb); | ||
60 | return [offset](Akonadi2::ApplicationDomain::Buffer::EventBuilder &builder) { builder.add_uid(offset); }; | ||
61 | }); | ||
62 | return propertyMapper; | ||
63 | } | ||
diff --git a/common/domainadaptor.h b/common/domainadaptor.h index 5d9574b..e356692 100644 --- a/common/domainadaptor.h +++ b/common/domainadaptor.h | |||
@@ -78,6 +78,39 @@ private: | |||
78 | }; | 78 | }; |
79 | 79 | ||
80 | /** | 80 | /** |
81 | * Defines how to convert qt primitives to flatbuffer ones | ||
82 | * TODO: rename to createProperty or so? | ||
83 | */ | ||
84 | template <class T> | ||
85 | flatbuffers::uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb); | ||
86 | |||
87 | /** | ||
88 | * Create a buffer from a domain object using the provided mappings | ||
89 | */ | ||
90 | template <class Builder, class Buffer> | ||
91 | flatbuffers::Offset<Buffer> createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper) | ||
92 | { | ||
93 | //First create a primitives such as strings using the mappings | ||
94 | QList<std::function<void(Builder &)> > propertiesToAddToResource; | ||
95 | for (const auto &property : domainObject.changedProperties()) { | ||
96 | qWarning() << "copying property " << property; | ||
97 | const auto value = domainObject.getProperty(property); | ||
98 | if (mapper.hasMapping(property)) { | ||
99 | mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb); | ||
100 | } else { | ||
101 | qWarning() << "no mapping for property available " << property; | ||
102 | } | ||
103 | } | ||
104 | |||
105 | //Then create all porperties using the above generated builderCalls | ||
106 | Builder builder(fbb); | ||
107 | for (auto propertyBuilder : propertiesToAddToResource) { | ||
108 | propertyBuilder(builder); | ||
109 | } | ||
110 | return builder.Finish(); | ||
111 | } | ||
112 | |||
113 | /** | ||
81 | * A generic adaptor implementation that uses a property mapper to read/write values. | 114 | * A generic adaptor implementation that uses a property mapper to read/write values. |
82 | * | 115 | * |
83 | * TODO: this is the read-only part. Create a write only equivalent | 116 | * TODO: this is the read-only part. Create a write only equivalent |
@@ -95,11 +128,6 @@ public: | |||
95 | //TODO remove | 128 | //TODO remove |
96 | void setProperty(const QByteArray &key, const QVariant &value) | 129 | void setProperty(const QByteArray &key, const QVariant &value) |
97 | { | 130 | { |
98 | // if (mResourceMapper && mResourceMapper->hasMapping(key)) { | ||
99 | // // mResourceMapper->setProperty(key, value, mResourceBuffer); | ||
100 | // } else { | ||
101 | // // mLocalMapper.; | ||
102 | // } | ||
103 | } | 131 | } |
104 | 132 | ||
105 | virtual QVariant getProperty(const QByteArray &key) const | 133 | virtual QVariant getProperty(const QByteArray &key) const |
@@ -128,49 +156,6 @@ public: | |||
128 | }; | 156 | }; |
129 | 157 | ||
130 | /** | 158 | /** |
131 | * A generic adaptor implementation that uses a property mapper to read/write values. | ||
132 | */ | ||
133 | template <class LocalBuilder, class ResourceBuilder> | ||
134 | class GenericWriteBufferAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor | ||
135 | { | ||
136 | public: | ||
137 | GenericWriteBufferAdaptor(const BufferAdaptor &buffer) | ||
138 | : BufferAdaptor() | ||
139 | { | ||
140 | for(const auto &property : buffer.availableProperties()) { | ||
141 | setProperty(property, buffer.getProperty(property)); | ||
142 | } | ||
143 | } | ||
144 | |||
145 | void setProperty(const QByteArray &key, const QVariant &value) | ||
146 | { | ||
147 | // if (mResourceMapper && mResourceMapper->hasMapping(key)) { | ||
148 | // // mResourceMapper->setProperty(key, value, mResourceBuffer); | ||
149 | // } else { | ||
150 | // // mLocalMapper.; | ||
151 | // } | ||
152 | } | ||
153 | |||
154 | //TODO remove | ||
155 | virtual QVariant getProperty(const QByteArray &key) const | ||
156 | { | ||
157 | Q_ASSERT(false); | ||
158 | } | ||
159 | |||
160 | virtual QList<QByteArray> availableProperties() const | ||
161 | { | ||
162 | Q_ASSERT(false); | ||
163 | QList<QByteArray> props; | ||
164 | return props; | ||
165 | } | ||
166 | |||
167 | // LocalBuffer const *mLocalBuffer; | ||
168 | // ResourceBuffer const *mResourceBuffer; | ||
169 | QSharedPointer<WritePropertyMapper<LocalBuilder> > mLocalMapper; | ||
170 | QSharedPointer<WritePropertyMapper<ResourceBuilder> > mResourceMapper; | ||
171 | }; | ||
172 | |||
173 | /** | ||
174 | * Initializes the local property mapper. | 159 | * Initializes the local property mapper. |
175 | * | 160 | * |
176 | * Provide an implementation for each application domain type. | 161 | * Provide an implementation for each application domain type. |
@@ -178,16 +163,24 @@ public: | |||
178 | template <class T> | 163 | template <class T> |
179 | QSharedPointer<ReadPropertyMapper<T> > initializeReadPropertyMapper(); | 164 | QSharedPointer<ReadPropertyMapper<T> > initializeReadPropertyMapper(); |
180 | 165 | ||
166 | template <class T> | ||
167 | QSharedPointer<WritePropertyMapper<T> > initializeWritePropertyMapper(); | ||
168 | |||
181 | /** | 169 | /** |
182 | * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. | 170 | * The factory should define how to go from an entitybuffer (local + resource buffer), to a domain type adapter. |
183 | * It defines how values are split accross local and resource buffer. | 171 | * It defines how values are split accross local and resource buffer. |
184 | * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. | 172 | * This is required by the facade the read the value, and by the pipeline preprocessors to access the domain values in a generic way. |
185 | */ | 173 | */ |
186 | template<typename DomainType, typename LocalBuffer, typename ResourceBuffer> | 174 | template<typename DomainType, typename LocalBuffer, typename ResourceBuffer, typename LocalBuilder, typename ResourceBuilder> |
187 | class DomainTypeAdaptorFactory | 175 | class DomainTypeAdaptorFactory |
188 | { | 176 | { |
189 | public: | 177 | public: |
190 | DomainTypeAdaptorFactory() : mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()) {}; | 178 | DomainTypeAdaptorFactory() : |
179 | mLocalMapper(initializeReadPropertyMapper<LocalBuffer>()), | ||
180 | mResourceMapper(QSharedPointer<ReadPropertyMapper<ResourceBuffer> >::create()), | ||
181 | mLocalWriteMapper(initializeWritePropertyMapper<LocalBuilder>()), | ||
182 | mResourceWriteMapper(QSharedPointer<WritePropertyMapper<ResourceBuilder> >::create()) | ||
183 | {}; | ||
191 | virtual ~DomainTypeAdaptorFactory() {}; | 184 | virtual ~DomainTypeAdaptorFactory() {}; |
192 | 185 | ||
193 | /** | 186 | /** |
@@ -214,6 +207,8 @@ public: | |||
214 | protected: | 207 | protected: |
215 | QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper; | 208 | QSharedPointer<ReadPropertyMapper<LocalBuffer> > mLocalMapper; |
216 | QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper; | 209 | QSharedPointer<ReadPropertyMapper<ResourceBuffer> > mResourceMapper; |
210 | QSharedPointer<WritePropertyMapper<LocalBuilder> > mLocalWriteMapper; | ||
211 | QSharedPointer<WritePropertyMapper<ResourceBuilder> > mResourceWriteMapper; | ||
217 | }; | 212 | }; |
218 | 213 | ||
219 | 214 | ||
diff --git a/dummyresource/domainadaptor.cpp b/dummyresource/domainadaptor.cpp index 00af3fe..fa00bbc 100644 --- a/dummyresource/domainadaptor.cpp +++ b/dummyresource/domainadaptor.cpp | |||
@@ -9,49 +9,12 @@ | |||
9 | #include "entity_generated.h" | 9 | #include "entity_generated.h" |
10 | #include "metadata_generated.h" | 10 | #include "metadata_generated.h" |
11 | #include "domainadaptor.h" | 11 | #include "domainadaptor.h" |
12 | #include "log.h" | ||
12 | #include <common/entitybuffer.h> | 13 | #include <common/entitybuffer.h> |
13 | 14 | ||
14 | using namespace DummyCalendar; | 15 | using namespace DummyCalendar; |
15 | using namespace flatbuffers; | 16 | using namespace flatbuffers; |
16 | 17 | ||
17 | /** | ||
18 | * Defines how to convert qt primitives to flatbuffer ones | ||
19 | * TODO: rename to createProperty or so? | ||
20 | */ | ||
21 | template <class T> | ||
22 | uoffset_t extractProperty(const QVariant &, flatbuffers::FlatBufferBuilder &fbb); | ||
23 | |||
24 | template <> | ||
25 | uoffset_t extractProperty<QString>(const QVariant &property, flatbuffers::FlatBufferBuilder &fbb) | ||
26 | { | ||
27 | if (property.isValid()) { | ||
28 | return fbb.CreateString(property.toString().toStdString()).o; | ||
29 | } | ||
30 | return 0; | ||
31 | } | ||
32 | |||
33 | /** | ||
34 | * Create a buffer from a domain object using the provided mappings | ||
35 | */ | ||
36 | template <class Builder> | ||
37 | void createBufferPart(const Akonadi2::ApplicationDomain::ApplicationDomainType &domainObject, flatbuffers::FlatBufferBuilder &fbb, const WritePropertyMapper<Builder> &mapper) | ||
38 | { | ||
39 | //First create a primitives such as strings using the mappings | ||
40 | QList<std::function<void(Builder &)> > propertiesToAddToResource; | ||
41 | for (const auto &property : domainObject.changedProperties()) { | ||
42 | const auto value = domainObject.getProperty(property); | ||
43 | if (mapper.hasMapping(property)) { | ||
44 | mapper.setProperty(property, domainObject.getProperty(property), propertiesToAddToResource, fbb); | ||
45 | } | ||
46 | } | ||
47 | |||
48 | //Then create all porperties using the above generated builderCalls | ||
49 | Builder builder(fbb); | ||
50 | for (auto propertyBuilder : propertiesToAddToResource) { | ||
51 | propertyBuilder(builder); | ||
52 | } | ||
53 | builder.Finish(); | ||
54 | } | ||
55 | 18 | ||
56 | 19 | ||
57 | 20 | ||
@@ -59,7 +22,6 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() | |||
59 | : DomainTypeAdaptorFactory() | 22 | : DomainTypeAdaptorFactory() |
60 | { | 23 | { |
61 | //TODO turn this into initializeReadPropertyMapper as well? | 24 | //TODO turn this into initializeReadPropertyMapper as well? |
62 | mResourceMapper = QSharedPointer<ReadPropertyMapper<DummyEvent> >::create(); | ||
63 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { | 25 | mResourceMapper->addMapping("summary", [](DummyEvent const *buffer) -> QVariant { |
64 | if (buffer->summary()) { | 26 | if (buffer->summary()) { |
65 | return QString::fromStdString(buffer->summary()->c_str()); | 27 | return QString::fromStdString(buffer->summary()->c_str()); |
@@ -76,51 +38,24 @@ DummyEventAdaptorFactory::DummyEventAdaptorFactory() | |||
76 | 38 | ||
77 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) | 39 | void DummyEventAdaptorFactory::createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb) |
78 | { | 40 | { |
79 | // flatbuffers::FlatBufferBuilder resFbb; | ||
80 | // flatbuffers::FlatBufferBuilder localFbb; | ||
81 | |||
82 | // QList<std::function<void(DummyEventBuilder &)> > propertiesToAddToResource; | ||
83 | // QList<std::function<void(Akonadi2::ApplicationDomain::Buffer::EventBuilder &)> > propertiesToAddToLocal; | ||
84 | // for (const auto &property : event.changedProperties()) { | ||
85 | // const auto value = event.getProperty(property); | ||
86 | // if (mResourceWriteMapper && mResourceWriteMapper->hasMapping(property)) { | ||
87 | // mResourceWriteMapper->setProperty(property, value, propertiesToAddToResource, resFbb); | ||
88 | // } if (mLocalWriteMapper && mLocalWriteMapper->hasMapping(property)) { | ||
89 | // mLocalWriteMapper->setProperty(property, value, propertiesToAddToLocal, localFbb); | ||
90 | // } | ||
91 | // } | ||
92 | |||
93 | // DummyEventBuilder resBuilder(resFbb); | ||
94 | // for (auto propertyBuilder : propertiesToAddToResource) { | ||
95 | // propertyBuilder(resBuilder); | ||
96 | // } | ||
97 | // resBuilder.Finish(); | ||
98 | |||
99 | // DummyEventBuilder localBuilder(localFbb); | ||
100 | // for (auto propertyBuilder : propertiesToAddToResource) { | ||
101 | // propertyBuilder(localBuilder); | ||
102 | // } | ||
103 | // localBuilder.Finish(); | ||
104 | |||
105 | // TODO: how does a resource specify what goes to a local buffer and what it stores separately? | ||
106 | // flatbuffers::FlatBufferBuilder eventFbb; | ||
107 | // { | ||
108 | // auto summary = extractProperty<QString>(event.getProperty("summary"), fbb); | ||
109 | // DummyCalendar::DummyEventBuilder eventBuilder(eventFbb); | ||
110 | // eventBuilder.add_summary(summary); | ||
111 | // auto eventLocation = eventBuilder.Finish(); | ||
112 | // DummyCalendar::FinishDummyEventBuffer(eventFbb, eventLocation); | ||
113 | // } | ||
114 | |||
115 | //TODO we should only copy values into the local buffer that haven't already been copied by the resource buffer | ||
116 | flatbuffers::FlatBufferBuilder localFbb; | 41 | flatbuffers::FlatBufferBuilder localFbb; |
117 | if (mLocalWriteMapper) { | 42 | if (mLocalWriteMapper) { |
118 | createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(event, localFbb, *mLocalWriteMapper); | 43 | auto pos = createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::Event>(event, localFbb, *mLocalWriteMapper); |
44 | Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(localFbb, pos); | ||
45 | flatbuffers::Verifier verifier(localFbb.GetBufferPointer(), localFbb.GetSize()); | ||
46 | if (!verifier.VerifyBuffer<Akonadi2::ApplicationDomain::Buffer::Event>()) { | ||
47 | Warning() << "Created invalid local buffer"; | ||
48 | } | ||
119 | } | 49 | } |
120 | 50 | ||
121 | flatbuffers::FlatBufferBuilder resFbb; | 51 | flatbuffers::FlatBufferBuilder resFbb; |
122 | if (mResourceWriteMapper) { | 52 | if (mResourceWriteMapper) { |
123 | createBufferPart<DummyEventBuilder>(event, resFbb, *mResourceWriteMapper); | 53 | auto pos = createBufferPart<DummyEventBuilder, DummyEvent>(event, resFbb, *mResourceWriteMapper); |
54 | DummyCalendar::FinishDummyEventBuffer(resFbb, pos); | ||
55 | flatbuffers::Verifier verifier(resFbb.GetBufferPointer(), resFbb.GetSize()); | ||
56 | if (!verifier.VerifyBuffer<DummyEvent>()) { | ||
57 | Warning() << "Created invalid resource buffer"; | ||
58 | } | ||
124 | } | 59 | } |
125 | 60 | ||
126 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); | 61 | Akonadi2::EntityBuffer::assembleEntityBuffer(fbb, 0, 0, resFbb.GetBufferPointer(), resFbb.GetSize(), localFbb.GetBufferPointer(), localFbb.GetSize()); |
diff --git a/dummyresource/domainadaptor.h b/dummyresource/domainadaptor.h index 39028c9..9d351e7 100644 --- a/dummyresource/domainadaptor.h +++ b/dummyresource/domainadaptor.h | |||
@@ -5,14 +5,10 @@ | |||
5 | #include "dummycalendar_generated.h" | 5 | #include "dummycalendar_generated.h" |
6 | #include "entity_generated.h" | 6 | #include "entity_generated.h" |
7 | 7 | ||
8 | class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent> | 8 | class DummyEventAdaptorFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder> |
9 | { | 9 | { |
10 | public: | 10 | public: |
11 | DummyEventAdaptorFactory(); | 11 | DummyEventAdaptorFactory(); |
12 | virtual ~DummyEventAdaptorFactory() {}; | 12 | virtual ~DummyEventAdaptorFactory() {}; |
13 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); | 13 | virtual void createBuffer(const Akonadi2::ApplicationDomain::Event &event, flatbuffers::FlatBufferBuilder &fbb); |
14 | |||
15 | private: | ||
16 | QSharedPointer<WritePropertyMapper<DummyCalendar::DummyEventBuilder> > mResourceWriteMapper; | ||
17 | QSharedPointer<WritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder> > mLocalWriteMapper; | ||
18 | }; | 14 | }; |
diff --git a/dummyresource/facade.cpp b/dummyresource/facade.cpp index af8c187..1477fcf 100644 --- a/dummyresource/facade.cpp +++ b/dummyresource/facade.cpp | |||
@@ -122,7 +122,7 @@ void DummyResourceFacade::readValue(QSharedPointer<Akonadi2::Storage> storage, c | |||
122 | const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); | 122 | const auto localBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::ApplicationDomain::Buffer::Event>(buffer.entity().local()); |
123 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); | 123 | const auto metadataBuffer = Akonadi2::EntityBuffer::readBuffer<Akonadi2::Metadata>(buffer.entity().metadata()); |
124 | 124 | ||
125 | if (!resourceBuffer || !metadataBuffer) { | 125 | if ((!resourceBuffer && !localBuffer) || !metadataBuffer) { |
126 | qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); | 126 | qWarning() << "invalid buffer " << QByteArray::fromRawData(static_cast<char*>(keyValue), keySize); |
127 | return true; | 127 | return true; |
128 | } | 128 | } |
diff --git a/dummyresource/facade.h b/dummyresource/facade.h index 7bef2cc..37ed81d 100644 --- a/dummyresource/facade.h +++ b/dummyresource/facade.h | |||
@@ -41,5 +41,5 @@ public: | |||
41 | 41 | ||
42 | private: | 42 | private: |
43 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); | 43 | void readValue(QSharedPointer<Akonadi2::Storage> storage, const QByteArray &key, const std::function<void(const Akonadi2::ApplicationDomain::Event::Ptr &)> &resultCallback, std::function<bool(const std::string &key, DummyCalendar::DummyEvent const *buffer, Akonadi2::ApplicationDomain::Buffer::Event const *local)>); |
44 | QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent> > mFactory; | 44 | QSharedPointer<DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, DummyCalendar::DummyEvent, Akonadi2::ApplicationDomain::Buffer::EventBuilder, DummyCalendar::DummyEventBuilder> > mFactory; |
45 | }; | 45 | }; |
diff --git a/tests/domainadaptortest.cpp b/tests/domainadaptortest.cpp index 74c0d62..1e285dc 100644 --- a/tests/domainadaptortest.cpp +++ b/tests/domainadaptortest.cpp | |||
@@ -13,89 +13,12 @@ | |||
13 | #include "metadata_generated.h" | 13 | #include "metadata_generated.h" |
14 | #include "entity_generated.h" | 14 | #include "entity_generated.h" |
15 | 15 | ||
16 | class TestEventAdaptor : public Akonadi2::ApplicationDomain::BufferAdaptor | 16 | class TestFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, Akonadi2::ApplicationDomain::Buffer::Event, Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::EventBuilder> |
17 | { | ||
18 | public: | ||
19 | TestEventAdaptor() | ||
20 | : Akonadi2::ApplicationDomain::BufferAdaptor() | ||
21 | { | ||
22 | } | ||
23 | |||
24 | void setProperty(const QByteArray &key, const QVariant &value) | ||
25 | { | ||
26 | // if (mResourceMapper->mWriteAccessors.contains(key)) { | ||
27 | // // mResourceMapper.setProperty(key, value, mResourceBuffer); | ||
28 | // } else { | ||
29 | // // mLocalMapper.; | ||
30 | // } | ||
31 | } | ||
32 | |||
33 | virtual QVariant getProperty(const QByteArray &key) const | ||
34 | { | ||
35 | if (mResourceBuffer && mResourceMapper->hasMapping(key)) { | ||
36 | return mResourceMapper->getProperty(key, mResourceBuffer); | ||
37 | } else if (mLocalBuffer) { | ||
38 | return mLocalMapper->getProperty(key, mLocalBuffer); | ||
39 | } | ||
40 | return QVariant(); | ||
41 | } | ||
42 | |||
43 | Akonadi2::ApplicationDomain::Buffer::Event const *mLocalBuffer; | ||
44 | Akonadi2::ApplicationDomain::Buffer::Event const *mResourceBuffer; | ||
45 | |||
46 | QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > mLocalMapper; | ||
47 | QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> > mResourceMapper; | ||
48 | }; | ||
49 | |||
50 | class TestFactory : public DomainTypeAdaptorFactory<Akonadi2::ApplicationDomain::Event, Akonadi2::ApplicationDomain::Buffer::Event, Akonadi2::ApplicationDomain::Buffer::Event> | ||
51 | { | 17 | { |
52 | public: | 18 | public: |
53 | TestFactory() | 19 | TestFactory() |
54 | { | 20 | { |
55 | mResourceMapper = QSharedPointer<ReadPropertyMapper<Akonadi2::ApplicationDomain::Buffer::Event> >::create(); | 21 | mResourceWriteMapper = initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(); |
56 | mResourceMapper->addMapping("summary", [](Akonadi2::ApplicationDomain::Buffer::Event const *buffer) -> QVariant { | ||
57 | if (buffer->summary()) { | ||
58 | return QString::fromStdString(buffer->summary()->c_str()); | ||
59 | } | ||
60 | return QVariant(); | ||
61 | }); | ||
62 | } | ||
63 | |||
64 | virtual QSharedPointer<Akonadi2::ApplicationDomain::BufferAdaptor> createAdaptor(const Akonadi2::Entity &entity) | ||
65 | { | ||
66 | Akonadi2::ApplicationDomain::Buffer::Event const *resourceBuffer = 0; | ||
67 | if (auto resourceData = entity.resource()) { | ||
68 | flatbuffers::Verifier verifyer(resourceData->Data(), resourceData->size()); | ||
69 | if (Akonadi2::ApplicationDomain::Buffer::VerifyEventBuffer(verifyer)) { | ||
70 | resourceBuffer = Akonadi2::ApplicationDomain::Buffer::GetEvent(resourceData->Data()); | ||
71 | if (resourceBuffer->summary()) { | ||
72 | qDebug() << QString::fromStdString(std::string(resourceBuffer->summary()->c_str())); | ||
73 | } | ||
74 | } | ||
75 | } | ||
76 | |||
77 | // Akonadi2::Metadata const *metadataBuffer = 0; | ||
78 | // if (auto metadataData = entity.metadata()) { | ||
79 | // flatbuffers::Verifier verifyer(metadataData->Data(), metadataData->size()); | ||
80 | // if (Akonadi2::VerifyMetadataBuffer(verifyer)) { | ||
81 | // metadataBuffer = Akonadi2::GetMetadata(metadataData); | ||
82 | // } | ||
83 | // } | ||
84 | |||
85 | Akonadi2::ApplicationDomain::Buffer::Event const *localBuffer = 0; | ||
86 | if (auto localData = entity.local()) { | ||
87 | flatbuffers::Verifier verifyer(localData->Data(), localData->size()); | ||
88 | if (Akonadi2::ApplicationDomain::Buffer::VerifyEventBuffer(verifyer)) { | ||
89 | localBuffer = Akonadi2::ApplicationDomain::Buffer::GetEvent(localData); | ||
90 | } | ||
91 | } | ||
92 | |||
93 | auto adaptor = QSharedPointer<TestEventAdaptor>::create(); | ||
94 | adaptor->mLocalBuffer = localBuffer; | ||
95 | adaptor->mResourceBuffer = resourceBuffer; | ||
96 | adaptor->mResourceMapper = mResourceMapper; | ||
97 | adaptor->mLocalMapper = mLocalMapper; | ||
98 | return adaptor; | ||
99 | } | 22 | } |
100 | }; | 23 | }; |
101 | 24 | ||
@@ -111,6 +34,21 @@ private Q_SLOTS: | |||
111 | { | 34 | { |
112 | } | 35 | } |
113 | 36 | ||
37 | void testCreateBufferPart() | ||
38 | { | ||
39 | auto writeMapper = initializeWritePropertyMapper<Akonadi2::ApplicationDomain::Buffer::EventBuilder>(); | ||
40 | |||
41 | Akonadi2::ApplicationDomain::Event event; | ||
42 | event.setProperty("summary", "foo"); | ||
43 | |||
44 | flatbuffers::FlatBufferBuilder fbb; | ||
45 | auto pos = createBufferPart<Akonadi2::ApplicationDomain::Buffer::EventBuilder, Akonadi2::ApplicationDomain::Buffer::Event>(event, fbb, *writeMapper); | ||
46 | Akonadi2::ApplicationDomain::Buffer::FinishEventBuffer(fbb, pos); | ||
47 | |||
48 | flatbuffers::Verifier verifier(fbb.GetBufferPointer(), fbb.GetSize()); | ||
49 | QVERIFY(verifier.VerifyBuffer<Akonadi2::ApplicationDomain::Buffer::Event>()); | ||
50 | } | ||
51 | |||
114 | void testAdaptor() | 52 | void testAdaptor() |
115 | { | 53 | { |
116 | //Create entity buffer | 54 | //Create entity buffer |