Skip to content
Merged
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
42 changes: 31 additions & 11 deletions src/constructor.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,13 @@ function flatten_mapping(node::MappingNode)
end


function construct_mapping(dicttype::Union{Type,Function}, constructor::Constructor, node::MappingNode; strict_unique_keys::Bool=false)
function construct_mapping(dicttype::Union{Type,Function}, constructor::Constructor, node::Node; strict_unique_keys::Bool=false)
if !(node isa MappingNode)
throw(ConstructorError(nothing, nothing,
"expected a mapping node, but found $(typeof(node))",
node.start_mark))
end

# If the mapping was constructed with a merge tag, skip the later
# test for duplicates, since it is a key part of the merging
# functionality that selected values can be changed.
Expand Down Expand Up @@ -246,14 +252,28 @@ const bool_values = Dict(
"off" => false )


function construct_yaml_bool(constructor::Constructor, node::Node)
# Convert a scalar node's text with `parse_text`. Text that it rejects, such as `!!int abc`,
# does not fit the node's tag, so it raises a ConstructorError at the node.
function construct_parsed_scalar(parse_text::Function, constructor::Constructor, node::Node)
value = construct_scalar(constructor, node)
bool_values[lowercase(value)]
try
return parse_text(value)
catch e
e isa InterruptException && rethrow()
throw(ConstructorError(nothing, nothing,
"could not convert $(repr(value)) to $(node.tag)", node.start_mark))
end
end


function construct_yaml_int(constructor::Constructor, node::Node)
value = string(construct_scalar(constructor, node))
construct_yaml_bool(constructor::Constructor, node::Node) =
construct_parsed_scalar(value -> bool_values[lowercase(value)], constructor, node)


construct_yaml_int(constructor::Constructor, node::Node) =
construct_parsed_scalar(parse_yaml_int, constructor, node)

function parse_yaml_int(value::AbstractString)
value = lowercase(replace(value, "_" => ""))

if in(':', value)
Expand Down Expand Up @@ -294,8 +314,10 @@ function parse_int(value::AbstractString, base::Int)
end


function construct_yaml_float(constructor::Constructor, node::Node)
value = string(construct_scalar(constructor, node))
construct_yaml_float(constructor::Constructor, node::Node) =
construct_parsed_scalar(parse_yaml_float, constructor, node)

function parse_yaml_float(value::AbstractString)
value = lowercase(replace(value, "_" => ""))

if in(':', value)
Expand Down Expand Up @@ -460,10 +482,8 @@ function construct_undefined(constructor::Constructor, node::Node)
end


function construct_yaml_binary(constructor::Constructor, node::Node)
value = replace(string(construct_scalar(constructor, node)), "\n" => "")
base64decode(value)
end
construct_yaml_binary(constructor::Constructor, node::Node) =
construct_parsed_scalar(value -> base64decode(replace(value, "\n" => "")), constructor, node)

const default_yaml_constructors = Dict{Union{String, Nothing}, Function}(
"tag:yaml.org,2002:null" => construct_yaml_null,
Expand Down
17 changes: 13 additions & 4 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -495,11 +495,20 @@ end
@testset "explicit float tags reject non-decimal text" begin
# only a decimal that rounds to zero or infinity takes the arbitrary-precision path
for input in ("!!float 0b101", "!!float 0b1e400", "!!float 1@5", "!!float 1@400", "!!float 0x1p2000")
@test_throws ArgumentError YAML.load(input)
@test_throws YAML.ConstructorError YAML.load(input)
end
@test YAML.load("!!float 1e400") == Inf
end

@testset "values that do not fit their explicit tag" begin
bad = String([0xd8]) # not valid UTF-8
for input in ("!!map x", "!!map [1]", "a: !!map b", "!!bool y", "!!bool " * bad, "!!int abc",
"!!int " * bad, "!!float abc", "!!float 1" * bad, "!!binary 0")
@test_throws YAML.ConstructorError YAML.load(input)
end
@test_throws YAML.ConstructorError YAML.load("!!map x"; dicttype = Dict{String,Any})
end

# issue 114 - gracefully handle extra commas in flow collections
@testset "issue114" begin
@test YAML.load("[3,4,]") == [3,4]
Expand Down Expand Up @@ -869,8 +878,8 @@ end
@test YAML.load_file(path) === expected
end
end
@test_throws ArgumentError YAML.load("!!int 0b102")
@test_throws ArgumentError YAML.load("!!int 0b")
@test_throws YAML.ConstructorError YAML.load("!!int 0b102")
@test_throws YAML.ConstructorError YAML.load("!!int 0b")
@test YAML.load("0b102") == "0b102"
@test YAML.load("0b") == "0b"
@test YAML.load("0b" * bitstring(typemax(Int))) === typemax(Int)
Expand All @@ -894,7 +903,7 @@ end
@test YAML.load(YAML.yaml(Dict("k" => x))) == Dict("k" => x)
end
@test YAML.load("!!int 09" * "0"^25) == 9 * big(10)^25
@test_throws ArgumentError YAML.load("!!int 1 2")
@test_throws YAML.ConstructorError YAML.load("!!int 1 2")
end

@testset "octal integer construction" begin
Expand Down
Loading