Skip to content

Commit 2b00752

Browse files
author
Peter Amstutz
committed
Turn idMap into mapSubject and mapPredicate.
1 parent af7a419 commit 2b00752

File tree

4 files changed

+45
-27
lines changed

4 files changed

+45
-27
lines changed

schema_salad/jsonld_context.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def process_type(t, g, context, defaultBase, namespaces, defaultPrefix):
8686
predicate = "%s:%s" % (defaultPrefix, recordname)
8787

8888
if context.get(recordname, predicate) != predicate:
89-
raise Exception("Predicate collision on '%s', '%s' != '%s'" % (recordname, context[t["name"]], predicate))
89+
raise Exception("Predicate collision on '%s', '%s' != '%s'" % (recordname, context[recordname], predicate))
9090

9191
if not recordname:
9292
raise Exception()

schema_salad/makedoc.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ def extendsfrom(item, ex):
286286
else:
287287
doc = ""
288288

289-
if self.title is None:
289+
if self.title is None and f["doc"]:
290290
self.title = f["doc"][0:f["doc"].index("\n")]
291291
if self.title.startswith('# '):
292292
self.title = self.title[2:]

schema_salad/ref_resolver.py

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,8 @@ def add_context(self, newcontext, baseuri=""):
171171
self.identity_links = set()
172172
self.standalone = set()
173173
self.nolinkcheck = set()
174-
self.idmap = set()
174+
self.idmap = {}
175+
self.mapPredicate = {}
175176
self.vocab = {}
176177
self.rvocab = {}
177178

@@ -194,8 +195,11 @@ def add_context(self, newcontext, baseuri=""):
194195
if isinstance(self.ctx[c], dict) and self.ctx[c].get("noLinkCheck"):
195196
self.nolinkcheck.add(c)
196197

197-
if isinstance(self.ctx[c], dict) and self.ctx[c].get("idMap"):
198-
self.idmap.add(c)
198+
if isinstance(self.ctx[c], dict) and self.ctx[c].get("mapSubject"):
199+
self.idmap[c] = self.ctx[c]["mapSubject"]
200+
201+
if isinstance(self.ctx[c], dict) and self.ctx[c].get("mapPredicate"):
202+
self.mapPredicate[c] = self.ctx[c]["mapPredicate"]
199203

200204
if isinstance(self.ctx[c], dict) and "@id" in self.ctx[c]:
201205
self.vocab[c] = self.ctx[c]["@id"]
@@ -338,22 +342,33 @@ def resolve_all(self, document, base_url, file_base=None):
338342
if idmapField in document and isinstance(document[idmapField], dict):
339343
ls = []
340344
for k,v in document[idmapField].items():
341-
v["id"] = k
345+
if not isinstance(v, dict):
346+
if idmapField in loader.mapPredicate:
347+
v = {loader.mapPredicate[idmapField]: v}
348+
else:
349+
raise validate.ValidationException("mapSubject '%s' value '%s' is not a dict and does not have a mapPredicate", k, v)
350+
v[loader.idmap[idmapField]] = k
342351
ls.append(v)
343352
document[idmapField] = ls
344353

345-
for identifer in loader.identity_links:
354+
for identifer in loader.identifiers:
346355
if identifer in document:
347356
if isinstance(document[identifer], basestring):
348357
document[identifer] = loader.expand_url(document[identifer], base_url, scoped=True)
349358
if document[identifer] not in loader.idx or isinstance(loader.idx[document[identifer]], basestring):
350359
loader.idx[document[identifer]] = document
351360
base_url = document[identifer]
352-
elif isinstance(document[identifer], list):
353-
for n, v in enumerate(document[identifer]):
354-
document[identifer][n] = loader.expand_url(document[identifer][n], base_url, scoped=True)
355-
if document[identifer][n] not in loader.idx:
356-
loader.idx[document[identifer][n]] = document[identifer][n]
361+
else:
362+
raise validate.ValidationException("identifier field '%s' must be a string" % (document[identifer]))
363+
364+
for identifer in loader.identity_links:
365+
if identifer in document and isinstance(document[identifer], list):
366+
for n, v in enumerate(document[identifer]):
367+
document[identifer][n] = loader.expand_url(document[identifer][n], base_url, scoped=True)
368+
if document[identifer][n] not in loader.idx:
369+
loader.idx[document[identifer][n]] = document[identifer][n]
370+
#else:
371+
# raise validate.ValidationException("identity field '%s' must be a string" % (document[identifer]))
357372

358373
for d in document:
359374
d2 = loader.expand_url(d, "", scoped=False, vocab_term=True)

tests/test_examples.py

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,41 +66,44 @@ def test_jsonld_ctx(self):
6666
'http://example.com/foo#bar': 'asym'
6767
})
6868

69+
maxDiff = None
6970

7071
def test_idmap(self):
7172
ldr = schema_salad.ref_resolver.Loader({})
7273
ldr.add_context({
7374
"inputs": {
7475
"@id": "http://example.com/inputs",
75-
"idMap": True
76+
"mapSubject": "id",
77+
"mapPredicate": "a"
7678
},
7779
"outputs": {
78-
"@id": "http://example.com/outputs",
79-
"idMap": True
80+
"@type": "@id",
81+
"identity": True,
8082
},
8183
"id": "@id"})
8284

8385
ra, _ = ldr.resolve_all({
86+
"id": "stuff",
8487
"inputs": {
85-
"zip": {"a": 1},
86-
"zing": {"a": 2}
87-
},
88-
"outputs": {
89-
"out": {"b": 3},
88+
"zip": 1,
89+
"zing": 2
9090
},
91+
"outputs": ["out"],
9192
"other": {
9293
'n': 9
9394
}
9495
}, "http://example2.com/")
9596

9697
self.assertEqual(ra,
97-
{'inputs': [{'a': 2, 'id': 'http://example2.com/#zing'},
98-
{'a': 1, 'id': 'http://example2.com/#zip'}],
99-
'outputs': [{'b': 3, 'id': 'http://example2.com/#out'}],
100-
'other': {
101-
'n': 9
102-
}
103-
}
98+
{
99+
"id": "http://example2.com/#stuff",
100+
'inputs': [{'a': 2, 'id': 'http://example2.com/#stuff/zing'},
101+
{'a': 1, 'id': 'http://example2.com/#stuff/zip'}],
102+
'outputs': ['http://example2.com/#stuff/out'],
103+
'other': {
104+
'n': 9
105+
}
106+
}
104107
)
105108

106109
def test_examples(self):

0 commit comments

Comments
 (0)