From 0b845b1eac5af5aa00694d1c6eed93bae37a28b3 Mon Sep 17 00:00:00 2001 From: Fons van der Plas Date: Mon, 12 Dec 2022 21:35:55 +0100 Subject: [PATCH] TOML: print: handle mixed vector of dicts and non-dicts (#47876) (cherry picked from commit 4ff62883130802a44a5b4b3aea85c2aa0d6f98cf) --- stdlib/TOML/src/print.jl | 5 ++++- stdlib/TOML/test/print.jl | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/stdlib/TOML/src/print.jl b/stdlib/TOML/src/print.jl index c9709cd7e4283..74efdfc97a05d 100644 --- a/stdlib/TOML/src/print.jl +++ b/stdlib/TOML/src/print.jl @@ -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, diff --git a/stdlib/TOML/test/print.jl b/stdlib/TOML/test/print.jl index 9479a14ca8796..bbfce3b7d7474 100644 --- a/stdlib/TOML/test/print.jl +++ b/stdlib/TOML/test/print.jl @@ -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