Skip to content

Commit

Permalink
TOML: print: handle mixed vector of dicts and non-dicts (JuliaLang#47876
Browse files Browse the repository at this point in the history
)
  • Loading branch information
fonsp committed Dec 12, 2022
1 parent cd19dc5 commit 4ff6288
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
5 changes: 4 additions & 1 deletion stdlib/TOML/src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,10 @@ end

is_table(value) = isa(value, AbstractDict)
is_array_of_tables(value) = isa(value, AbstractArray) &&
length(value) > 0 && isa(value[1], AbstractDict)
length(value) > 0 && (
isa(value, AbstractArray{<:AbstractDict}) ||
all(v -> isa(v, AbstractDict), value)
)
is_tabular(value) = is_table(value) || is_array_of_tables(value)

function print_table(f::MbyFunc, io::IO, a::AbstractDict,
Expand Down
16 changes: 16 additions & 0 deletions stdlib/TOML/test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,22 @@ loaders = ["gzip", { driver = "csv", args = {delim = "\t"}}]
@test roundtrip(str)


@testset "vec with dicts and non-dicts" begin
# https://github.com/JuliaLang/julia/issues/45340
d = Dict("b" => Any[111, Dict("a" => 222, "d" => 333)])
@test toml_str(d) == "b = [111, {a = 222, d = 333}]\n"

d = Dict("b" => Any[Dict("a" => 222, "d" => 333), 111])
@test toml_str(d) == "b = [{a = 222, d = 333}, 111]\n"

d = Dict("b" => Any[Dict("a" => 222, "d" => 333)])
@test toml_str(d) == """
[[b]]
a = 222
d = 333
"""
end

struct Foo
a::Int64
b::Float64
Expand Down

0 comments on commit 4ff6288

Please sign in to comment.