Skip to content

Commit

Permalink
Improve JSON-LD context parsing (#61)
Browse files Browse the repository at this point in the history
Following w3c/json-ld-syntax#329 and
w3c/json-ld-syntax#329, this PR updates the
processing of JSON-LD contexts to handle when there are dictionaries.
  • Loading branch information
cthoyt authored Jul 28, 2023
1 parent b5e3598 commit e72efd9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
8 changes: 7 additions & 1 deletion src/curies/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,13 @@ def from_jsonld(cls, data: LocationOr[Dict[str, Any]]) -> "Converter":
>>> converter = Converter.from_jsonld(url)
>>> "rdf" in converter.prefix_map
"""
return cls.from_prefix_map(_prepare(data)["@context"])
prefix_map = {}
for key, value in _prepare(data)["@context"].items():
if isinstance(value, str):
prefix_map[key] = value
elif isinstance(value, dict) and value.get("@prefix") is True:
prefix_map[key] = value["@id"]
return cls.from_prefix_map(prefix_map)

@classmethod
def from_jsonld_github(
Expand Down
18 changes: 18 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,24 @@ def test_bioregistry(self):
self.assertIn("chebi", c.prefix_map)
self.assertNotIn("CHEBI", c.prefix_map)

def test_jsonld(self):
"""Test parsing JSON-LD context."""
context = {
"@context": {
"hello": "https://example.org/hello:",
"CHEBI": {
"@prefix": True,
"@id": "http://purl.obolibrary.org/CHEBI_",
},
"nope": {
"nope": "nope",
},
},
}
converter = Converter.from_jsonld(context)
self.assertIn("hello", converter.prefix_map)
self.assertIn("CHEBI", converter.prefix_map)

def test_from_github(self):
"""Test getting a JSON-LD map from GitHub."""
with self.assertRaises(ValueError):
Expand Down

0 comments on commit e72efd9

Please sign in to comment.