Skip to content

Commit 0b845b1

Browse files
fonspKristofferC
authored andcommitted
TOML: print: handle mixed vector of dicts and non-dicts (#47876)
(cherry picked from commit 4ff6288)
1 parent 2866e26 commit 0b845b1

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

stdlib/TOML/src/print.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,10 @@ end
122122

123123
is_table(value) = isa(value, AbstractDict)
124124
is_array_of_tables(value) = isa(value, AbstractArray) &&
125-
length(value) > 0 && isa(value[1], AbstractDict)
125+
length(value) > 0 && (
126+
isa(value, AbstractArray{<:AbstractDict}) ||
127+
all(v -> isa(v, AbstractDict), value)
128+
)
126129
is_tabular(value) = is_table(value) || is_array_of_tables(value)
127130

128131
function print_table(f::MbyFunc, io::IO, a::AbstractDict,

stdlib/TOML/test/print.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ loaders = ["gzip", { driver = "csv", args = {delim = "\t"}}]
8080
@test roundtrip(str)
8181

8282

83+
@testset "vec with dicts and non-dicts" begin
84+
# https://github.com/JuliaLang/julia/issues/45340
85+
d = Dict("b" => Any[111, Dict("a" => 222, "d" => 333)])
86+
@test toml_str(d) == "b = [111, {a = 222, d = 333}]\n"
87+
88+
d = Dict("b" => Any[Dict("a" => 222, "d" => 333), 111])
89+
@test toml_str(d) == "b = [{a = 222, d = 333}, 111]\n"
90+
91+
d = Dict("b" => Any[Dict("a" => 222, "d" => 333)])
92+
@test toml_str(d) == """
93+
[[b]]
94+
a = 222
95+
d = 333
96+
"""
97+
end
98+
8399
struct Foo
84100
a::Int64
85101
b::Float64

0 commit comments

Comments
 (0)