Skip to content

Commit af7a419

Browse files
author
Peter Amstutz
committed
Support id maps in preprocessing.
1 parent 984ab9c commit af7a419

File tree

3 files changed

+50
-1
lines changed

3 files changed

+50
-1
lines changed

schema_salad/ref_resolver.py

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

@@ -193,6 +194,9 @@ def add_context(self, newcontext, baseuri=""):
193194
if isinstance(self.ctx[c], dict) and self.ctx[c].get("noLinkCheck"):
194195
self.nolinkcheck.add(c)
195196

197+
if isinstance(self.ctx[c], dict) and self.ctx[c].get("idMap"):
198+
self.idmap.add(c)
199+
196200
if isinstance(self.ctx[c], dict) and "@id" in self.ctx[c]:
197201
self.vocab[c] = self.ctx[c]["@id"]
198202
elif isinstance(self.ctx[c], basestring):
@@ -330,6 +334,14 @@ def resolve_all(self, document, base_url, file_base=None):
330334
metadata, _ = loader.resolve_all(metadata, base_url, file_base)
331335

332336
if isinstance(document, dict):
337+
for idmapField in loader.idmap:
338+
if idmapField in document and isinstance(document[idmapField], dict):
339+
ls = []
340+
for k,v in document[idmapField].items():
341+
v["id"] = k
342+
ls.append(v)
343+
document[idmapField] = ls
344+
333345
for identifer in loader.identity_links:
334346
if identifer in document:
335347
if isinstance(document[identifer], basestring):

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
install_requires.append("avro-python3")
3939

4040
setup(name='schema-salad',
41-
version='1.7',
41+
version='1.8',
4242
description='Schema Annotations for Linked Avro Data (SALAD)',
4343
long_description=open(README).read(),
4444
author='Common workflow language working group',

tests/test_examples.py

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

69+
70+
def test_idmap(self):
71+
ldr = schema_salad.ref_resolver.Loader({})
72+
ldr.add_context({
73+
"inputs": {
74+
"@id": "http://example.com/inputs",
75+
"idMap": True
76+
},
77+
"outputs": {
78+
"@id": "http://example.com/outputs",
79+
"idMap": True
80+
},
81+
"id": "@id"})
82+
83+
ra, _ = ldr.resolve_all({
84+
"inputs": {
85+
"zip": {"a": 1},
86+
"zing": {"a": 2}
87+
},
88+
"outputs": {
89+
"out": {"b": 3},
90+
},
91+
"other": {
92+
'n': 9
93+
}
94+
}, "http://example2.com/")
95+
96+
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+
}
104+
)
105+
69106
def test_examples(self):
70107
self.maxDiff = None
71108
for a in ["field_name", "ident_res", "link_res", "vocab_res"]:

0 commit comments

Comments
 (0)