Skip to content

Commit 8072bf5

Browse files
author
Peter Amstutz
committed
"type" DSL wip
1 parent 33ef215 commit 8072bf5

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

schema_salad/metaschema/metaschema.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ $graph:
139139
the item is transformed to a JSON object with the key assigned to the
140140
field specified by `mapSubject` and the value assigned to the field
141141
specified by `mapPredicate`.
142+
- name: typeDSL
143+
type: ["null", boolean]
144+
doc: |
145+
Field must be expanded based on the the Schema Salad type DSL.
142146
143147
- name: SpecializeDef
144148
type: record

schema_salad/ref_resolver.py

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
import pprint
1616
from StringIO import StringIO
1717
from .aslist import aslist
18+
from .flatten import flatten
1819
import rdflib
1920
from rdflib.namespace import RDF, RDFS, OWL
2021
from rdflib.plugins.parsers.notation3 import BadSyntax
@@ -103,6 +104,7 @@ def __init__(self, ctx, schemagraph=None, foreign_properties=None,
103104
self.rvocab = {} # type: Dict[unicode, Any]
104105
self.idmap = None # type: Dict[unicode, Any]
105106
self.mapPredicate = None # type: Dict[unicode, Any]
107+
self.type_dsl_fields = None # type: Set[str]
106108

107109
self.add_context(ctx)
108110

@@ -337,6 +339,32 @@ def _resolve_idmap(self, document, loader):
337339
ls.append(v)
338340
document[idmapField] = ls
339341

342+
def _type_dsl(self, t):
343+
r = t
344+
if "[]" in t:
345+
r = {"type": "array",
346+
"items": t.replace("[]", "").replace("?", "")}
347+
if t.endswith("?"):
348+
r = ["null", r]
349+
return r
350+
351+
def _resolve_type_dsl(self, document, loader):
352+
for d in loader.type_dsl_fields:
353+
if d in document:
354+
if isinstance(document[d], basestring):
355+
document[d] = self._type_dsl(document[d])
356+
elif isinstance(document[d], list):
357+
document[d] = [self._type_dsl(t) for t in document[d]]
358+
if isinstance(document[d], list):
359+
document[d] = flatten(document[d])
360+
seen = set()
361+
uniq = []
362+
for item in document[d]:
363+
if item not in seen:
364+
uniq.append(item)
365+
seen.add(item)
366+
document[d] = uniq
367+
340368
def _resolve_identifier(self, document, loader, base_url):
341369
# Expand identifier field (usually 'id') to resolve scope
342370
for identifer in loader.identifiers:
@@ -439,10 +467,11 @@ def resolve_all(self, document, base_url, file_base=None):
439467

440468
if isinstance(document, dict):
441469

470+
self._normalize_fields(document, loader)
442471
self._resolve_idmap(document, loader)
472+
self._type_dsl(document, loader)
443473
base_url = self._resolve_identifier(document, loader, base_url)
444474
self._resolve_identity(document, loader, base_url)
445-
self._normalize_fields(document, loader)
446475
self._resolve_uris(document, loader, base_url)
447476

448477
try:

0 commit comments

Comments
 (0)