Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow printing some TOML dictionaries inline by marking them with an IdSet #53233

Merged
merged 2 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 3 additions & 2 deletions stdlib/TOML/src/TOML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -110,10 +110,11 @@ const ParserError = Internals.ParserError


"""
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sorted=false, by=identity)
print([to_toml::Function], io::IO [=stdout], data::AbstractDict; sorted=false, by=identity, inline_tables::Base.IdSet{<:AbstractDict})

Write `data` as TOML syntax to the stream `io`. If the keyword argument `sorted` is set to `true`,
sort tables according to the function given by the keyword argument `by`.
sort tables according to the function given by the keyword argument `by`. If the keyword argument
`inline_tables` is given, it should be a set of tables that should be printed "inline".

The following data types are supported: `AbstractDict`, `AbstractVector`, `AbstractString`, `Integer`, `AbstractFloat`, `Bool`,
`Dates.DateTime`, `Dates.Time`, `Dates.Date`. Note that the integers and floats
Expand Down
27 changes: 18 additions & 9 deletions stdlib/TOML/src/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,11 +141,18 @@ function print_table(f::MbyFunc, io::IO, a::AbstractDict,
indent::Int = 0,
first_block::Bool = true,
sorted::Bool = false,
inline_tables::Base.IdSet,
by::Function = identity,
)

if a in inline_tables
@invokelatest print_inline_table(f, io, a)
return
end

akeys = keys(a)
if sorted
akeys = sort!(collect(akeys); by=by)
akeys = sort!(collect(akeys); by)
end

# First print non-tabular entries
Expand All @@ -154,7 +161,9 @@ function print_table(f::MbyFunc, io::IO, a::AbstractDict,
if !isa(value, TOMLValue)
value = to_toml_value(f, value)
end
is_tabular(value) && continue
if is_tabular(value) && !(value in inline_tables)
continue
end

Base.print(io, ' '^4max(0,indent-1))
printkey(io, [String(key)])
Expand All @@ -169,7 +178,7 @@ function print_table(f::MbyFunc, io::IO, a::AbstractDict,
if !isa(value, TOMLValue)
value = to_toml_value(f, value)
end
if is_table(value)
if is_table(value) && !(value in inline_tables)
push!(ks, String(key))
_values = @invokelatest values(value)
header = isempty(value) || !all(is_tabular(v) for v in _values)::Bool
Expand All @@ -183,7 +192,7 @@ function print_table(f::MbyFunc, io::IO, a::AbstractDict,
Base.print(io,"]\n")
end
# Use runtime dispatch here since the type of value seems not to be enforced other than as AbstractDict
@invokelatest print_table(f, io, value, ks; indent = indent + header, first_block = header, sorted=sorted, by=by)
@invokelatest print_table(f, io, value, ks; indent = indent + header, first_block = header, sorted, by, inline_tables)
pop!(ks)
elseif @invokelatest(is_array_of_tables(value))
# print array of tables
Expand All @@ -197,7 +206,7 @@ function print_table(f::MbyFunc, io::IO, a::AbstractDict,
Base.print(io,"]]\n")
# TODO, nicer error here
!isa(v, AbstractDict) && error("array should contain only tables")
@invokelatest print_table(f, io, v, ks; indent = indent + 1, sorted=sorted, by=by)
@invokelatest print_table(f, io, v, ks; indent = indent + 1, sorted, by, inline_tables)
end
pop!(ks)
end
Expand All @@ -209,7 +218,7 @@ end
# API #
#######

print(f::MbyFunc, io::IO, a::AbstractDict; sorted::Bool=false, by=identity) = print_table(f, io, a; sorted=sorted, by=by)
print(f::MbyFunc, a::AbstractDict; sorted::Bool=false, by=identity) = print(f, stdout, a; sorted=sorted, by=by)
print(io::IO, a::AbstractDict; sorted::Bool=false, by=identity) = print_table(nothing, io, a; sorted=sorted, by=by)
print(a::AbstractDict; sorted::Bool=false, by=identity) = print(nothing, stdout, a; sorted=sorted, by=by)
print(f::MbyFunc, io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::Base.IdSet{<:AbstractDict}=Base.IdSet{Dict{String}}()) = print_table(f, io, a; sorted, by, inline_tables)
print(f::MbyFunc, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::Base.IdSet{<:AbstractDict}=Base.IdSet{Dict{String}}()) = print(f, stdout, a; sorted, by, inline_tables)
print(io::IO, a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::Base.IdSet{<:AbstractDict}=Base.IdSet{Dict{String}}()) = print_table(nothing, io, a; sorted, by, inline_tables)
print( a::AbstractDict; sorted::Bool=false, by=identity, inline_tables::Base.IdSet{<:AbstractDict}=Base.IdSet{Dict{String}}()) = print(nothing, stdout, a; sorted, by, inline_tables)
16 changes: 16 additions & 0 deletions stdlib/TOML/test/print.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,3 +140,19 @@ d = "hello"
a = 2
b = 9.9
"""


inline_dict = Dict("a" => [1,2], "b" => Dict("a" => "b"), "c" => "foo")
d = Dict(
"x" => "y",
"y" => inline_dict,
"z" => [1,2,3],
)
inline_tables = Base.IdSet{Dict}()
push!(inline_tables, inline_dict)
@test toml_str(d; sorted=true, inline_tables) ==
"""
x = "y"
y = {c = "foo", b = {a = "b"}, a = [1, 2]}
z = [1, 2, 3]
"""