Skip to content

Commit c693db2

Browse files
committed
Upstream creating a Resolver with a specific root resource.
Maybe there's some way to collapse this but for now, saves downstream having to assign some default base URI for the (possibly-not-internally-identified) resource.
1 parent 87bc1a0 commit c693db2

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed

referencing/_core.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,19 @@ def resolver(self, base_uri: URI = "") -> Resolver[D]:
371371
"""
372372
return Resolver(base_uri=base_uri, registry=self)
373373

374+
def resolver_with_root(self, resource: Resource[D]) -> Resolver[D]:
375+
"""
376+
Return a `Resolver` with a specific root resource.
377+
378+
The resource must have an internal identifier (e.g. for a JSON Schema
379+
resource in recent versions, an ``$id`` keyword).
380+
"""
381+
uri = resource.id() or ""
382+
return Resolver(
383+
base_uri=uri,
384+
registry=self.with_resource(uri, resource),
385+
)
386+
374387

375388
@frozen
376389
class Resolved(Generic[D]):

referencing/tests/test_core.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,23 @@ def retrieve(uri):
425425
registry = Registry(retrieve=retrieve).with_resource("urn:root", root)
426426
assert registry.crawl()["urn:child"] == child
427427

428+
def test_resolver(self):
429+
one = Resource.opaque(contents={})
430+
registry = Registry({"http://example.com": one})
431+
resolver = registry.resolver(base_uri="http://example.com")
432+
assert resolver.lookup("#").contents == {}
433+
434+
def test_resolver_with_root_identified(self):
435+
root = ID_AND_CHILDREN.create_resource({"ID": "http://example.com"})
436+
resolver = Registry().resolver_with_root(root)
437+
assert resolver.lookup("http://example.com").contents == root.contents
438+
assert resolver.lookup("#").contents == root.contents
439+
440+
def test_resolver_with_root_unidentified(self):
441+
root = Resource.opaque(contents={})
442+
resolver = Registry().resolver_with_root(root)
443+
assert resolver.lookup("#").contents == root.contents
444+
428445
def test_repr(self):
429446
one = Resource.opaque(contents={})
430447
two = ID_AND_CHILDREN.create_resource({"foo": "bar"})

0 commit comments

Comments
 (0)