Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,3 @@ vcpkg_installed/
# test output & cache
Testing/
.cache/
*out*
35 changes: 6 additions & 29 deletions include/ro-crate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
#include <string>
#include <map>
#include <vector>
#include <algorithm>

namespace rocrate {

Expand Down Expand Up @@ -254,15 +253,7 @@ namespace rocrate {
EntityRegister getEntities() const;

/**
* Adds a context entry to the RO-Crate's root metadata entity, without a prefix.
*
* @param value The context value associated with the key.
* @throw std::invalid_argument if the value is empty.
*/
void addContext(const std::string& value);

/**
* Adds a context entry to the RO-Crate's root metadata entity, with a prefix.
* Adds a context entry to the RO-Crate's root metadata entity.
*
* @param key The context key to add.
* @param value The context value associated with the key.
Expand Down Expand Up @@ -321,9 +312,6 @@ namespace rocrate {
);

EntityRegister entities_;
std::vector<std::string> remoteContext_ = {
"https://w3id.org/ro/crate/1.3/context",
};
std::map<std::string, std::string> context_;
};

Expand Down Expand Up @@ -396,6 +384,7 @@ namespace rocrate {
}

inline void ROCrate::addContext(const std::string& key, const std::string& value) {
// Validate the context
if (key.empty()) {
throw std::invalid_argument("Context key cannot be empty.");
}
Expand All @@ -418,22 +407,14 @@ namespace rocrate {
inline void ROCrate::writeOut(const std::string& path) {
// Create the JSON representation of the RO-Crate
nlohmann::json outCrate = {
{"@context", "https://w3id.org/ro/crate/1.3/context"},
{"@graph", nlohmann::json::array()}
};

// Add the context entries to the JSON representation
if (!remoteContext_.empty() || !context_.empty()) {
// Remote contexts: bare string if only one, array if multiple
if (remoteContext_.size() == 1 && context_.empty()) {
outCrate["@context"] = remoteContext_[0];
} else {
outCrate["@context"] = nlohmann::json::array();
for (const auto& remote : remoteContext_) {
outCrate["@context"].push_back(remote);
}
}

// Local contexts
if (!context_.empty()) {
outCrate["@context"] = nlohmann::json::array();
outCrate["@context"].push_back("https://w3id.org/ro/crate/1.3/context");
for (const auto& [key, value] : context_) {
outCrate["@context"].push_back({{key, value}});
}
Expand Down Expand Up @@ -478,10 +459,6 @@ namespace rocrate {
for (const auto& [key, value] : contextEntry.items()) {
addContext(key, value.get<std::string>());
}
} else if (contextEntry.is_string()) {
addContext(contextEntry.get<std::string>());
} else {
throw std::runtime_error("Invalid context entry in RO-Crate JSON: " + contextEntry.dump());
}
}
}
Expand Down
25 changes: 0 additions & 25 deletions tests/fixtures/add-context.json

This file was deleted.

9 changes: 5 additions & 4 deletions tests/unit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ TEST_CASE("Duplicate context doesn't throw", "[unit]") {

}

TEST_CASE("Add context throws if context value is empty", "[unit]") {
TEST_CASE("Add context throws if context is empty", "[unit]") {
rocrate::ROCrate crate;
REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument);
}
Expand Down Expand Up @@ -244,10 +244,11 @@ TEST_CASE("Add context with and without prefix (key) yields correct JSON-LD outp
REQUIRE_NOTHROW(crate2.readIn(outputPath));
crate2.writeOut(outputPath + ".roundtrip.json");

REQUIRE_RO_CRATE_FILE_EQUAL_BY_ID(
std::string(TEST_SOURCE_DIR) + "/tests/fixtures/add-context.json",
outputPath + ".roundtrip.json"
REQUIRE_THROWS_AS(
crate.addContext("", "https://example.com/context.jsonld"),
std::invalid_argument
);
REQUIRE_THROWS_AS(crate.addContext("example", ""), std::invalid_argument);
}

TEST_CASE("getContext returns the correct combined context", "[unit]") {
Expand Down
Loading