Skip to content

Commit

Permalink
Accept multiple YAML documents as attachments (#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
tetron authored Jun 21, 2019
1 parent 9eb44f8 commit c52f745
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 1 deletion.
9 changes: 8 additions & 1 deletion schema_salad/ref_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,8 @@ def resolve_ref(self,
return resolved_obj, metadata
else:
return resolved_obj, CommentedMap()
elif isinstance(resolved_obj, string_types):
return resolved_obj, CommentedMap()
else:
raise ValueError(u"Expected MutableMapping or MutableSequence, got %s: `%s`"
% (type(resolved_obj), Text(resolved_obj)))
Expand Down Expand Up @@ -1021,7 +1023,12 @@ def fetch(self, url, inject_ids=True): # type: (Text, bool) -> Any
else:
textIO = StringIO(text)
textIO.name = url # type: ignore
result = yaml.round_trip_load(textIO, preserve_quotes=True)
attachments = yaml.round_trip_load_all(textIO, preserve_quotes=True)
result = next(attachments)
i = 1
for a in attachments:
self.idx["%s#attachment-%i" % (url, i)] = a
i += 1
add_lc_filename(result, url)
except yaml.parser.ParserError as e:
raise_from(validate.ValidationException("Syntax error %s" % Text(e)), e)
Expand Down
7 changes: 7 additions & 0 deletions schema_salad/tests/multidoc.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
foo: bar
baz: {$include: "#attachment-1"}
quux: {$include: "#attachment-2"}
--- |
This is the {first attachment}.
--- |-
This is the [second attachment].
8 changes: 8 additions & 0 deletions schema_salad/tests/test_ref_resolver.py
Original file line number Diff line number Diff line change
Expand Up @@ -175,3 +175,11 @@ def test_fetch_inject_id():
r3, _ = l3.resolve_ref(furi3)
assert {"id": "http://example.com", "bar": "baz"} == r3
assert [furi3, "http://example.com"] == sorted(list(k.lower() for k in l3.idx.keys()))

def test_attachments():
l1 = Loader({})
furi1 = file_uri(get_data("schema_salad/tests/multidoc.yml"))
r1, _ = l1.resolve_ref(furi1)
assert {"foo": "bar",
"baz": "This is the {first attachment}.\n",
"quux": "This is the [second attachment]."} == r1

0 comments on commit c52f745

Please sign in to comment.