Skip to content

Commit ef1beba

Browse files
authored
Merge pull request #24 from queryverse/new-mime-type
Add support for "application/vnd.dataresource+json" MIME type
2 parents 46b8611 + c1f7780 commit ef1beba

3 files changed

Lines changed: 22 additions & 4 deletions

File tree

NEWS.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
# QueryOperators.jl v0.7.0 Release Notes
2+
* Add support for "application/vnd.dataresource+json" MIME showing
3+
14
# QueryOperators.jl v0.6.0 Release Notes
25
* Add unique command
36

src/enumerable/show.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
function printsequence(io::IO, source::Enumerable)
22
T = eltype(source)
33
rows = Base.IteratorSize(source) == Base.HasLength() ? length(source) : "?"
4-
4+
55
print(io, "$(rows)-element query result")
66

77
max_element_to_show = 10
@@ -18,7 +18,7 @@ function printsequence(io::IO, source::Enumerable)
1818
else
1919
extra_rows = length(source) - max_element_to_show
2020
print(io, "$extra_rows more $(extra_rows==1 ? "element" : "elements")")
21-
end
21+
end
2222
break
2323
else
2424
print(io, " ")
@@ -43,9 +43,21 @@ function Base.show(io::IO, ::MIME"text/html", source::Enumerable)
4343
TableShowUtils.printHTMLtable(io, source)
4444
else
4545
error("Cannot write this Enumerable as text/html.")
46-
end
46+
end
4747
end
4848

4949
function Base.Multimedia.showable(::MIME"text/html", source::Enumerable)
5050
return eltype(source) <: NamedTuple
5151
end
52+
53+
function Base.show(io::IO, ::MIME"application/vnd.dataresource+json", source::Enumerable)
54+
if eltype(source) <: NamedTuple
55+
TableShowUtils.printdataresource(io, source)
56+
else
57+
error("Cannot write this Enumerable as 'application/vnd.dataresource+json'.")
58+
end
59+
end
60+
61+
function Base.Multimedia.showable(::MIME"application/vnd.dataresource+json", source::Enumerable)
62+
return eltype(source) <: NamedTuple
63+
end

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ end
6666

6767
# ensure that the default value must be of the same type
6868
errored = false
69-
try
69+
try
7070
QueryOperators.@default_if_empty(source_1, "string")
7171
catch
7272
errored = true
@@ -147,6 +147,9 @@ a │ b │ c
147147
@test sprint((stream,data)->show(stream, "text/html", data), ntups) ==
148148
"<table><thead><tr><th>a</th><th>b</th><th>c</th></tr></thead><tbody><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>4</td><td>5</td><td>6</td></tr></tbody></table>"
149149

150+
@test sprint((stream,data)->show(stream, "application/vnd.dataresource+json", data), ntups) ==
151+
"{\"schema\":{\"fields\":[{\"name\":\"a\",\"type\":\"integer\"},{\"name\":\"b\",\"type\":\"integer\"},{\"name\":\"c\",\"type\":\"integer\"}]},\"data\":[{\"a\":1,\"b\":2,\"c\":3},{\"a\":4,\"b\":5,\"c\":6}]}"
152+
150153
end
151154

152155
include("test_enumerable_unique.jl")

0 commit comments

Comments
 (0)