Skip to content

Commit

Permalink
Support export stream creation using JSONPath-like syntax (#36)
Browse files Browse the repository at this point in the history
This PR extends the `Database::createExportStream` method to support basic path selection, for example `http://192.168.13.10/color.brightness`.

Array item selection isn't included, but could be added in a similar way for updates as discussed in #18.
  • Loading branch information
mikee47 authored Sep 15, 2024
1 parent 5226c95 commit 0b89302
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 7 deletions.
2 changes: 2 additions & 0 deletions samples/Basic_Config/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ Web client
----------

The sample starts a basic HTTP server which can be visited to view the database content in JSON format.
On the linux host emulator, for example, enter *http://192.168.13.10* to view the entire database.
To view a specific object use JSONPath notation, for example *http://192.168.13.10/color.brightness*.

An HTTP POST request can also be used to update the database contents.
This must have `Content-Type: application/json` encoding.
Expand Down
5 changes: 2 additions & 3 deletions samples/Basic_Config/app/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
#include <basic-config.h>
#include <ConfigDB/Json/Format.h>
#include <ConfigDB/Network/HttpImportResource.h>
#include <Data/CStringArray.h>
#include <Data/Format/Json.h>

#ifdef ENABLE_MALLOC_COUNT
Expand Down Expand Up @@ -200,14 +199,14 @@ void printStoreStats(ConfigDB::Database& db, bool detailed)

void onFile(HttpRequest& request, HttpResponse& response)
{
Serial << toString(request.method) << " REQ" << endl;
Serial << toString(request.method) << " \"" << request.uri.getRelativePath() << '"' << endl;

if(request.method != HTTP_GET) {
response.code = HTTP_STATUS_BAD_REQUEST;
return;
}

auto stream = database.createExportStream(ConfigDB::Json::format);
auto stream = database.createExportStream(ConfigDB::Json::format, request.uri.getRelativePath());
response.sendDataStream(stream.release(), MIME_JSON);
}

Expand Down
41 changes: 41 additions & 0 deletions src/Database.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include <Platform/System.h>
#include <Data/Stream/FileStream.h>
#include <Data/Buffer/PrintBuffer.h>
#include <Data/CStringArray.h>

namespace ConfigDB
{
Expand Down Expand Up @@ -282,6 +283,46 @@ bool Database::save(Store& store) const
return result;
}

std::unique_ptr<ExportStream> Database::createExportStream(const Format& format, const String& path)
{
CStringArray csa;
{
String tmp = path;
tmp.replace('.', '\0');
csa = std::move(tmp);
}
auto it = csa.begin();
if(!it) {
return format.createExportStream(*this);
}

int storeIndex = typeinfo.findStore(*it, strlen(*it));
if(storeIndex >= 0) {
++it;
} else {
storeIndex = 0;
}

unsigned offset{0};
auto prop = &typeinfo.stores[storeIndex];
for(; it; ++it) {
int i = prop->findObject(*it, strlen(*it));
if(i < 0) {
return nullptr;
}
offset += prop->offset;
prop = &prop->getObject(i);
}

auto store = openStore(storeIndex);
if(!store) {
return nullptr;
}

Object obj(*store, *prop, offset);
return format.createExportStream(store, obj);
}

bool Database::exportToFile(const Format& format, const String& filename)
{
FileStream stream;
Expand Down
7 changes: 3 additions & 4 deletions src/include/ConfigDB/Database.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,10 @@ class Database

/**
* @brief Create a read-only stream for serializing the database
* @param format
* @param path JSONPath-like expression to restrict output to specific store or object
*/
std::unique_ptr<ExportStream> createExportStream(const Format& format)
{
return format.createExportStream(*this);
}
std::unique_ptr<ExportStream> createExportStream(const Format& format, const String& path = nullptr);

/**
* @brief Serialize the database to a stream
Expand Down

0 comments on commit 0b89302

Please sign in to comment.