Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "DataSets"
uuid = "c9661210-8a83-48f0-b833-72e62abce419"
authors = ["Chris Foster <[email protected]> and contributors"]
version = "0.2.12"
version = "0.2.13"

[deps]
AbstractTrees = "1520ce14-60c1-5f80-bbc7-55ef81b5835c"
Expand Down
15 changes: 13 additions & 2 deletions src/DataSets.jl
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,21 @@ function Base.show(io::IO, d::DataSet)
end

function Base.show(io::IO, ::MIME"text/plain", d::DataSet)
TOML.print(io, d.conf)
# When we write the DataSet in the show() method above, we just dump the
# underlying dictionary as TOML. However, not all Julia values can be
# serialized, so we do a best-effort sanitization of the dictionary to
# ensure it is serializable.
try
TOML.print(io, d.conf) do _
"<unserializable>"
end
catch e
@debug "Failed to serialize DataSet to TOML" exception = (e, catch_backtrace())
print(io, "\n... <unserializable>")
print(io, "\nSet JULIA_DEBUG=DataSets to see the error")
end
end


#-------------------------------------------------------------------------------
"""
Subtypes of `AbstractDataProject` have the interface
Expand Down
53 changes: 53 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ using DataSets
using Test
using UUIDs
using ResourceContexts
using TOML

using DataSets: FileSystemRoot

Expand Down Expand Up @@ -45,6 +46,58 @@ end

ds = dataset(proj, "a_text_file")
@test ds.uuid == UUID("b498f769-a7f6-4f67-8d74-40b770398f26")

# Exercise the show() methods
let s = sprint(show, ds)
@test occursin("a_text_file", s)
@test occursin("b498f769-a7f6-4f67-8d74-40b770398f26", s)
end
let s = sprint(show, "text/plain", ds)
parsed = TOML.parse(s)
@test parsed isa Dict
@test parsed["name"] == "a_text_file"
@test parsed["uuid"] == "b498f769-a7f6-4f67-8d74-40b770398f26"
end

# Test show() methods when there are values that are not serializable
# into TOML in the Dict
config["datasets"][1]["foo"] = nothing
config["datasets"][1]["bar"] = [1, 2, nothing, Dict("x" => nothing, "y" => "y")]
config["datasets"][1]["baz"] = Dict(
"x" => nothing, "y" => "y",
)
proj = DataSets.load_project(config)
ds = dataset(proj, "a_text_file")
let s = sprint(show, "text/plain", ds)
# It looks like that in Julia <= 1.8.0, the TOML.print(f, ...) variant
# for arbitrary types does not actually work, since it's missing the fallback
# implementation and has other bugs, depending on the Julia version.
#
# So the `show()`-ed TOML will not parse again. But since we have a try-catch anyway,
# we don't care too much, so we just run a simplified test in that case.
if VERSION >= v"1.9.0"
parsed = TOML.parse(s)
@test parsed isa Dict
@test parsed["name"] == "a_text_file"
@test parsed["uuid"] == "b498f769-a7f6-4f67-8d74-40b770398f26"
@test parsed["foo"] == "<unserializable>"
@test parsed["bar"] == [1, 2, "<unserializable>", Dict("x" => "<unserializable>", "y" => "y")]
@test parsed["baz"] == Dict("x" => "<unserializable>", "y" => "y")
else
@test occursin("<unserializable>", s)
end
end

# Also test bad keys
config["datasets"][1]["keys"] = Dict(
nothing => 123,
1234 => "bad",
)
proj = DataSets.load_project(config)
ds = dataset(proj, "a_text_file")
let s = sprint(show, "text/plain", ds)
endswith(s, "\n... <unserializable>\nSet JULIA_DEBUG=DataSets to see the error")
end
end

@testset "open() for DataSet" begin
Expand Down
Loading