Веб-сайт самохостера Lotigara

summaryrefslogtreecommitdiff
path: root/source/base/StarAssets.cpp
diff options
context:
space:
mode:
authorKae <80987908+Novaenia@users.noreply.github.com>2024-03-09 09:54:23 +1100
committerGitHub <noreply@github.com>2024-03-09 09:54:23 +1100
commita6a1cc7f42d8ee55531ad87be70eafd46737166b (patch)
tree08d5ab229e1d66d262d01073eb2bf7a5dbb3f7c3 /source/base/StarAssets.cpp
parent4c2e75b585ef366c6744b5e4e303c0fddff1fefd (diff)
parentc5e568e47c95ac8210a72effb2e3a24ebd90af1b (diff)
Merge pull request #29 from WasabiRaptor/json-patching
improvement to iterating through patch arrays
Diffstat (limited to 'source/base/StarAssets.cpp')
-rw-r--r--source/base/StarAssets.cpp60
1 files changed, 38 insertions, 22 deletions
diff --git a/source/base/StarAssets.cpp b/source/base/StarAssets.cpp
index 422ea22..0b21f5e 100644
--- a/source/base/StarAssets.cpp
+++ b/source/base/StarAssets.cpp
@@ -678,6 +678,38 @@ ByteArray Assets::read(String const& path) const {
throw AssetException(strf("No such asset '{}'", path));
}
+Json Assets::checkPatchArray(String const& path, AssetSourcePtr const& source, Json const result, JsonArray const patchData, Maybe<Json> const external) const {
+ auto externalRef = external.value();
+ auto newResult = result;
+ for (auto const patch : patchData) {
+ switch(patch.type()){
+ case Json::Type::Array: // if the patch is an array, go down recursively until we get objects
+ try {
+ newResult = checkPatchArray(path, source, newResult, patch.toArray(), externalRef);
+ } catch (JsonPatchTestFail const& e) {
+ Logger::debug("Patch test failure from file {} in source: '{}' at '{}'. Caused by: {}", path, source->metadata().value("name", ""), m_assetSourcePaths.getLeft(source), e.what());
+ } catch (JsonPatchException const& e) {
+ Logger::error("Could not apply patch from file {} in source: '{}' at '{}'. Caused by: {}", path, source->metadata().value("name", ""), m_assetSourcePaths.getLeft(source), e.what());
+ }
+ break;
+ case Json::Type::Object: // if its an object, check for operations, or for if an external file is needed for patches to reference
+ newResult = JsonPatching::applyOperation(newResult, patch, externalRef);
+ break;
+ case Json::Type::String:
+ try {
+ externalRef = json(patch.toString());
+ } catch (...) {
+ throw JsonPatchTestFail(strf("Unable to load reference asset: {}", patch.toString()));
+ }
+ break;
+ default:
+ throw JsonPatchException(strf("Patch data is wrong type: {}", Json::typeName(patch.type())));
+ break;
+ }
+ }
+ return newResult;
+}
+
Json Assets::readJson(String const& path) const {
ByteArray streamData = read(path);
try {
@@ -688,30 +720,13 @@ Json Assets::readJson(String const& path) const {
if (patchJson.isType(Json::Type::Array)) {
auto patchData = patchJson.toArray();
try {
- if (patchData.size()) {
- if (patchData.at(0).type() == Json::Type::Array) {
- for (auto const& patch : patchData) {
- try {
- result = jsonPatch(result, patch.toArray());
- } catch (JsonPatchTestFail const& e) {
- Logger::debug("Patch test failure from file {} in source: {}. Caused by: {}", pair.first, m_assetSourcePaths.getLeft(pair.second), e.what());
- }
- }
- } else if (patchData.at(0).type() == Json::Type::Object) {
- try {
- result = jsonPatch(result, patchData);
- } catch (JsonPatchTestFail const& e) {
- Logger::debug("Patch test failure from file {} in source: {}. Caused by: {}", pair.first, m_assetSourcePaths.getLeft(pair.second), e.what());
- }
- } else {
- throw JsonPatchException(strf("Patch data is wrong type: {}", Json::typeName(patchData.at(0).type())));
- }
- }
+ result = checkPatchArray(pair.first, pair.second, result, patchData, {});
+ } catch (JsonPatchTestFail const& e) {
+ Logger::debug("Patch test failure from file {} in source: '{}' at '{}'. Caused by: {}", pair.first, pair.second->metadata().value("name", ""), m_assetSourcePaths.getLeft(pair.second), e.what());
} catch (JsonPatchException const& e) {
- Logger::error("Could not apply patch from file {} in source: {}. Caused by: {}", pair.first, m_assetSourcePaths.getLeft(pair.second), e.what());
+ Logger::error("Could not apply patch from file {} in source: '{}' at '{}'. Caused by: {}", pair.first, pair.second->metadata().value("name", ""), m_assetSourcePaths.getLeft(pair.second), e.what());
}
- }
- else if (patchJson.isType(Json::Type::Object)) { //Kae: Do a good ol' json merge instead if the .patch file is a Json object
+ } else if (patchJson.isType(Json::Type::Object)) { //Kae: Do a good ol' json merge instead if the .patch file is a Json object
auto patchData = patchJson.toObject();
result = jsonMerge(result, patchData);
}
@@ -722,6 +737,7 @@ Json Assets::readJson(String const& path) const {
}
}
+
bool Assets::doLoad(AssetId const& id) const {
try {
// loadAsset automatically manages the queue and freshens the asset