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

Improve iteration for multiple documents in one file. #120

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
18 changes: 11 additions & 7 deletions src/YAML.jl
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,6 @@ end

YAMLDocIterator(input::IO, more_constructors::_constructor=nothing, multi_constructors::Dict = Dict(); dicttype::_dicttype=Dict{Any, Any}, constructorType::Function = SafeConstructor) = YAMLDocIterator(input, constructorType(_patch_constructors(more_constructors, dicttype), multi_constructors))

# It's unknown how many documents will be found. By doing this,
# functions like `collect` do not try to query the length of the
# iterator.
Base.IteratorSize(::YAMLDocIterator) = Base.SizeUnknown()

# Iteration protocol.
function iterate(it::YAMLDocIterator, _ = nothing)
Expand All @@ -131,6 +127,14 @@ function iterate(it::YAMLDocIterator, _ = nothing)
return doc, nothing
end

# It's unknown how many documents will be found. By doing this,
# functions like `collect` do not try to query the length of the
# iterator.
Base.IteratorSize(::Type{YAMLDocIterator}) = Base.SizeUnknown()
contradict marked this conversation as resolved.
Show resolved Hide resolved
# Documents can be trees of elements or just single values, so don't promise
# any particular type.
Base.IteratorEltype(::Type{YAMLDocIterator}) = Base.EltypeUnknown()

"""
load_all(x::Union{AbstractString, IO}) -> YAMLDocIterator

Expand Down Expand Up @@ -161,8 +165,8 @@ load_file(filename::AbstractString, args...; kwargs...) =
Parse the YAML file `filename`, and return corresponding YAML documents.
"""
load_all_file(filename::AbstractString, args...; kwargs...) =
open(filename, "r") do input
load_all(input, args...; kwargs...)
open(filename, "r") do f
io = IOBuffer(read(f))
load_all(io, args...; kwargs...)
end

end # module
40 changes: 37 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -239,25 +239,59 @@ const encodings = [
@test YAML.load(IOBuffer(data)) == "test"
end

@testset "multi_doc_bom" begin
iterable = YAML.load_all("""
const multidoc_contents = """
\ufeff---\r
test: 1
\ufeff---
test: 2

\ufeff---
test: 3
""")
\ufeff---
42
"""

@testset "multi_doc_bom" begin
iterable = YAML.load_all(multidoc_contents)
(val, state) = iterate(iterable)
@test isequal(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test isequal(val, 42)
@test iterate(iterable, state) === nothing
end

@testset "multi_doc_file" begin
fname = tempname() # cleanup=true, file will be deleted on process exit
open(fname, "w") do f
write(f, multidoc_contents)
end
iterable = YAML.load_all_file(fname)
(val, state) = iterate(iterable)
@test isequal(val, Dict("test" => 1))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 2))
(val, state) = iterate(iterable, state)
@test isequal(val, Dict("test" => 3))
(val, state) = iterate(iterable, state)
@test isequal(val, 42)
@test iterate(iterable, state) === nothing
end

@testset "multi_doc_iteration_protocol" begin
fname = tempname() # cleanup=true, file will be deleted on process exit
open(fname, "w") do f
write(f, multidoc_contents)
end
iterable = YAML.load_all_file(fname)
@test Base.IteratorSize(YAML.YAMLDocIterator) == Base.SizeUnknown()
@test Base.IteratorEltype(YAML.YAMLDocIterator) == Base.EltypeUnknown()
@test length(collect(iterable)) == 4
end

# test that an OrderedDict is written in the correct order
using OrderedCollections, DataStructures
@test strip(YAML.yaml(OrderedDict(:c => 3, :b => 2, :a => 1))) == join(["c: 3", "b: 2", "a: 1"], "\n")
Expand Down
Loading