Skip to content

Commit 62fe6f9

Browse files
committed
Return Status from import methods
1 parent 135e783 commit 62fe6f9

File tree

12 files changed

+66
-54
lines changed

12 files changed

+66
-54
lines changed

src/Database.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -196,20 +196,20 @@ bool Database::exportToFile(const Format& format, const String& filename)
196196
}
197197

198198
if(stream.getLastError() == FS_OK) {
199-
debug_d("[CFGDB] Database saved '%s' OK", filename.c_str());
199+
debug_d("[CFGDB] Database saved to '%s'", filename.c_str());
200200
return true;
201201
}
202202

203-
debug_e("[CFGDB] Database save '%s' failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
203+
debug_e("[CFGDB] Database save to '%s' failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
204204
return false;
205205
}
206206

207-
bool Database::importFromFile(const Format& format, const String& filename)
207+
Status Database::importFromFile(const Format& format, const String& filename)
208208
{
209209
FileStream stream;
210210
if(!stream.open(filename, File::ReadOnly)) {
211-
debug_w("open('%s') failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
212-
return false;
211+
debug_w("[CFGDB] open '%s' failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
212+
return {Result::fileError, stream.getLastError()};
213213
}
214214

215215
return format.importFromStream(*this, stream);

src/Json/Format.cpp

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -61,28 +61,22 @@ std::unique_ptr<ImportStream> Format::createImportStream(std::shared_ptr<Store>
6161
return std::make_unique<WriteStream>(store, object);
6262
}
6363

64-
bool Format::importFromStream(Object& object, Stream& source) const
64+
Status Format::importFromStream(Object& object, Stream& source) const
6565
{
6666
auto status = WriteStream::parse(object, source);
67-
68-
if(status == JSON::Status::EndOfDocument) {
69-
return true;
67+
if(!status) {
68+
debug_w("JSON load '%s': %s", object.getName().c_str(), toString(status).c_str());
7069
}
71-
72-
debug_w("JSON load '%s': %s", object.getName().c_str(), toString(status).c_str());
73-
return false;
70+
return status;
7471
}
7572

76-
bool Format::importFromStream(Database& database, Stream& source) const
73+
Status Format::importFromStream(Database& database, Stream& source) const
7774
{
7875
auto status = WriteStream::parse(database, source);
79-
80-
if(status == JSON::Status::EndOfDocument) {
81-
return true;
76+
if(!status) {
77+
debug_w("JSON load '%s': %s", database.getName().c_str(), toString(status).c_str());
8278
}
83-
84-
debug_w("JSON load '%s': %s", database.getName().c_str(), toString(status).c_str());
85-
return false;
79+
return status;
8680
}
8781

8882
} // namespace ConfigDB::Json

src/Json/WriteStream.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,18 @@ WriteStream::~WriteStream()
3030
}
3131
}
3232

33-
JSON::Status WriteStream::parse(Database& database, Stream& source)
33+
Status WriteStream::parse(Database& database, Stream& source)
3434
{
35-
return WriteStream(database).parser.parse(source);
35+
WriteStream writer(database);
36+
writer.parser.parse(source);
37+
return writer.status;
3638
}
3739

38-
JSON::Status WriteStream::parse(Object& object, Stream& source)
40+
Status WriteStream::parse(Object& object, Stream& source)
3941
{
40-
return WriteStream(object).parser.parse(source);
42+
WriteStream writer(object);
43+
writer.parser.parse(source);
44+
return writer.status;
4145
}
4246

4347
bool WriteStream::startElement(const JSON::Element& element)
@@ -46,18 +50,21 @@ bool WriteStream::startElement(const JSON::Element& element)
4650
return true;
4751
}
4852

49-
auto notInSchema = [&element]() -> bool {
50-
debug_e("[JSON] '%s' not in schema", element.key);
53+
auto notInSchema = [&]() -> bool {
54+
debug_w("[CFGDB] '%s' not in schema", element.key);
55+
status.result = Result::formatError;
5156
return false;
5257
};
5358

54-
auto arrayExpected = [&element]() -> bool {
55-
debug_w("[JSON] '%s' not an array", element.key);
59+
auto arrayExpected = [&]() -> bool {
60+
debug_w("[CFGDB] '%s' not an array", element.key);
61+
status.result = Result::formatError;
5662
return false;
5763
};
5864

59-
auto badSelector = [&element]() -> bool {
60-
debug_w("[JSON] '%s' bad selector", element.key);
65+
auto badSelector = [&]() -> bool {
66+
debug_w("[CFGDB] '%s' bad selector", element.key);
67+
status.result = Result::formatError;
6168
return false;
6269
};
6370

@@ -66,7 +73,11 @@ bool WriteStream::startElement(const JSON::Element& element)
6673
return notInSchema();
6774
}
6875
const char* value = (element.type == JSON::Element::Type::Null) ? nullptr : element.value;
69-
return prop.setJsonValue(value, element.valueLength);
76+
if(!prop.setJsonValue(value, element.valueLength)) {
77+
status.result = Result::formatError;
78+
return false;
79+
}
80+
return true;
7081
};
7182

7283
if(db && element.level == 1) {
@@ -77,7 +88,7 @@ bool WriteStream::startElement(const JSON::Element& element)
7788
store.reset();
7889
store = db->openStore(root, true);
7990
if(!store || !*store) {
80-
// Fatal: store is locked
91+
status.result = Result::updateConflict;
8192
return false;
8293
}
8394
info[0] = *store;
@@ -97,7 +108,7 @@ bool WriteStream::startElement(const JSON::Element& element)
97108
auto& type = *db->typeinfo.stores[i];
98109
store = db->openStore(type, true);
99110
if(!store || !*store) {
100-
// Fatal: store is locked
111+
status.result = Result::updateConflict;
101112
return false;
102113
}
103114
info[1] = Object(*store);
@@ -247,10 +258,9 @@ size_t WriteStream::write(const uint8_t* data, size_t size)
247258

248259
auto jsonStatus = parser.parse(reinterpret_cast<const char*>(data), size);
249260
switch(jsonStatus) {
261+
case JSON::Status::Ok:
250262
case JSON::Status::EndOfDocument:
251-
break;
252263
case JSON::Status::Cancelled:
253-
status.result = Result::updateConflict;
254264
break;
255265
default:
256266
status.result = Result::formatError;

src/Json/WriteStream.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ class WriteStream : public ImportStream, private JSON::Listener
4848

4949
~WriteStream();
5050

51-
static JSON::Status parse(Database& db, Stream& source);
51+
static Status parse(Database& db, Stream& source);
5252

53-
static JSON::Status parse(Object& object, Stream& source);
53+
static Status parse(Object& object, Stream& source);
5454

5555
bool isValid() const override
5656
{

src/Object.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,23 +271,23 @@ bool Object::exportToFile(const Format& format, const String& filename) const
271271
}
272272

273273
if(stream.getLastError() == FS_OK) {
274-
debug_d("[JSON] Store saved '%s' OK", filename.c_str());
274+
debug_d("[CFGDB] Object saved to '%s'", filename.c_str());
275275
return true;
276276
}
277277

278-
debug_e("[JSON] Store save '%s' failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
278+
debug_e("[CFGDB] Object save to '%s' failed: %s", filename.c_str(), stream.getLastErrorString().c_str());
279279
return false;
280280
}
281281

282-
bool Object::importFromFile(const Format& format, const String& filename)
282+
Status Object::importFromFile(const Format& format, const String& filename)
283283
{
284284
FileStream stream;
285285
if(!stream.open(filename, File::ReadOnly)) {
286286
if(stream.getLastError() == IFS::Error::NotFound) {
287-
return true;
287+
return {};
288288
}
289-
debug_w("open('%s') failed", filename.c_str());
290-
return false;
289+
debug_w("[CFGDB] open '%s' failed", filename.c_str());
290+
return {Result::fileError, stream.getLastError()};
291291
}
292292

293293
return format.importFromStream(*this, stream);

src/include/ConfigDB/Database.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,12 +92,12 @@ class Database
9292

9393
bool exportToFile(const Format& format, const String& filename);
9494

95-
bool importFromStream(const Format& format, Stream& source)
95+
Status importFromStream(const Format& format, Stream& source)
9696
{
9797
return format.importFromStream(*this, source);
9898
}
9999

100-
bool importFromFile(const Format& format, const String& filename);
100+
Status importFromFile(const Format& format, const String& filename);
101101

102102
std::unique_ptr<ImportStream> createImportStream(const Format& format)
103103
{

src/include/ConfigDB/Format.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ struct Status {
6060
return nullptr;
6161
}
6262
}
63+
64+
size_t printTo(Print& p) const
65+
{
66+
return p.print(toString());
67+
}
6368
};
6469

6570
class ImportStream : public ReadWriteStream
@@ -126,14 +131,14 @@ class Format
126131
/**
127132
* @brief De-serialise content from stream into object (RAM)
128133
*/
129-
virtual bool importFromStream(Object& object, Stream& source) const = 0;
134+
virtual Status importFromStream(Object& object, Stream& source) const = 0;
130135

131136
/**
132137
* @brief De-serialise content from stream into database
133138
* Each store is overwritten as it is loadded.
134139
* If a store entry is not represented in the data then it is left untouched.
135140
*/
136-
virtual bool importFromStream(Database& database, Stream& source) const = 0;
141+
virtual Status importFromStream(Database& database, Stream& source) const = 0;
137142

138143
/**
139144
* @brief Get the standard file extension for the reader implementation

src/include/ConfigDB/Json/Format.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ class Format : public ConfigDB::Format
3434
size_t exportToStream(Database& database, Print& output) const override;
3535
std::unique_ptr<ImportStream> createImportStream(Database& db) const override;
3636
std::unique_ptr<ImportStream> createImportStream(std::shared_ptr<Store> store, Object& object) const override;
37-
bool importFromStream(Object& object, Stream& source) const override;
38-
bool importFromStream(Database& database, Stream& source) const override;
37+
Status importFromStream(Object& object, Stream& source) const override;
38+
Status importFromStream(Database& database, Stream& source) const override;
3939

4040
String getFileExtension() const override
4141
{

src/include/ConfigDB/Network/HttpImportResource.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ class HttpImportResource : public HttpResource
7979
case Result::ok:
8080
break;
8181
case Result::formatError:
82+
response.code = HTTP_STATUS_BAD_REQUEST;
83+
break;
8284
case Result::updateConflict:
8385
response.code = HTTP_STATUS_CONFLICT;
8486
break;

src/include/ConfigDB/Object.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,12 +160,12 @@ class Object
160160

161161
bool exportToFile(const Format& format, const String& filename) const;
162162

163-
bool importFromStream(const Format& format, Stream& source)
163+
Status importFromStream(const Format& format, Stream& source)
164164
{
165165
return format.importFromStream(*this, source);
166166
}
167167

168-
bool importFromFile(const Format& format, const String& filename);
168+
Status importFromFile(const Format& format, const String& filename);
169169

170170
const ObjectInfo& typeinfo() const
171171
{

0 commit comments

Comments
 (0)