Skip to content

Commit

Permalink
TOML: Return Dates objects by default, even for explicit Parser
Browse files Browse the repository at this point in the history
The doc-comment here strongly suggests this was intended to be a public
interface (although not the most frequently used one), so this tries a
bit harder to avoid breaking the Dates-related behavior here.

This removes `DTParser`, which effectively behaved the way the existing
`Parser()` behaved before JuliaLang#54755

As a side effect, this hides the `Base.TOML.Parser` type out of the way
the public API.
  • Loading branch information
topolarity committed Jul 3, 2024
1 parent 1193997 commit 05af472
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 17 deletions.
23 changes: 8 additions & 15 deletions stdlib/TOML/src/TOML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,8 @@ explicitly create a `Parser` but instead one directly use use
will however reuse some internal data structures which can be beneficial for
performance if a larger number of small files are parsed.
"""
const Parser = Internals.Parser

"""
DTParser()
Constructor for a TOML `Parser` which returns date and time objects from Dates.
"""
function DTParser(args...; kwargs...)
parser = Parser(args...; kwargs...)
function Parser(args...; kwargs...)
parser = Internals.Parser(args...; kwargs...)
parser.Dates = Dates
return parser
end
Expand All @@ -60,7 +53,7 @@ See also [`TOML.tryparsefile`](@ref).
"""
parsefile(f::AbstractString) =
Internals.parse(DTParser(readstring(f); filepath=abspath(f)))
parsefile(p::Parser, f::AbstractString) =
parsefile(p::Internals.Parser, f::AbstractString) =
Internals.parse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))

"""
Expand All @@ -74,7 +67,7 @@ See also [`TOML.parsefile`](@ref).
"""
tryparsefile(f::AbstractString) =
Internals.tryparse(DTParser(readstring(f); filepath=abspath(f)))
tryparsefile(p::Parser, f::AbstractString) =
tryparsefile(p::Internals.Parser, f::AbstractString) =
Internals.tryparse(Internals.reinit!(p, readstring(f); filepath=abspath(f)))

"""
Expand All @@ -88,10 +81,10 @@ See also [`TOML.tryparse`](@ref).
"""
parse(str::AbstractString) =
Internals.parse(DTParser(String(str)))
parse(p::Parser, str::AbstractString) =
parse(p::Internals.Parser, str::AbstractString) =
Internals.parse(Internals.reinit!(p, String(str)))
parse(io::IO) = parse(read(io, String))
parse(p::Parser, io::IO) = parse(p, read(io, String))
parse(p::Internals.Parser, io::IO) = parse(p, read(io, String))

"""
tryparse(x::Union{AbstractString, IO})
Expand All @@ -104,10 +97,10 @@ See also [`TOML.parse`](@ref).
"""
tryparse(str::AbstractString) =
Internals.tryparse(DTParser(String(str)))
tryparse(p::Parser, str::AbstractString) =
tryparse(p::Internals.Parser, str::AbstractString) =
Internals.tryparse(Internals.reinit!(p, String(str)))
tryparse(io::IO) = tryparse(read(io, String))
tryparse(p::Parser, io::IO) = tryparse(p, read(io, String))
tryparse(p::Internals.Parser, io::IO) = tryparse(p, read(io, String))

"""
ParserError
Expand Down
17 changes: 15 additions & 2 deletions stdlib/TOML/test/values.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,29 @@ using Test
using TOML
using TOML: Internals

# Construct an explicit Parser to test the "cached" version of parsing
const test_parser = TOML.Parser()

function testval(s, v)
f = "foo = $s"
# First, test with the standard entrypoint
parsed = TOML.parse(f)["foo"]
return isequal(v, parsed) && typeof(v) == typeof(parsed)
(!isequal(v, parsed) || typeof(v) != typeof(parsed)) && return false
# Next, test with the "cached" (explicit Parser) entrypoint
parsed = TOML.parse(test_parser, f)["foo"]
(!isequal(v, parsed) || typeof(v) != typeof(parsed)) && return false
return true
end

function failval(s, v)
f = "foo = $s"
# First, test with the standard entrypoint
err = TOML.tryparse(f);
return err isa TOML.Internals.ParserError && err.type == v
(!isa(err, TOML.Internals.ParserError) || err.type != v) && return false
# Next, test with the "cached" (explicit Parser) entrypoint
err = TOML.tryparse(test_parser, f);
(!isa(err, TOML.Internals.ParserError) || err.type != v) && return false
return true
end

@testset "Numbers" begin
Expand Down

0 comments on commit 05af472

Please sign in to comment.