Skip to content

Commit

Permalink
Make checking for sequence more stringent.
Browse files Browse the repository at this point in the history
  • Loading branch information
iwanders committed Nov 19, 2020
1 parent 74f3b66 commit 1ad358d
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion lib/lyaml/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,24 @@ local dumper_mt = {
elseif itsa == 'string' or itsa == 'boolean' or itsa == 'number' then
return self:dump_scalar(node)
elseif itsa == 'table' then
if #node > 0 then
-- Something is only a sequence if its keys start at 1
-- and are consecutive integers without any jumps.
local prior_key = 0
local is_pure_sequence = true
local i, v = next(node, nil)
while i and is_pure_sequence do
if type(i) ~= "number" or (prior_key + 1 ~= i) then
is_pure_sequence = false -- breaks the loop
else
prior_key = i
i, v = next(node, prior_key)
end
end
if is_pure_sequence then
-- Only sequentially numbered integer keys starting from 1.
return self:dump_sequence(node)
else
-- Table contains non sequential integer keys or mixed keys.
return self:dump_mapping(node)
end
else -- unsupported Lua type
Expand Down

0 comments on commit 1ad358d

Please sign in to comment.