peposso / lua-tinyyaml

a tiny yaml (subset) parser for pure lua

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Cannot load empty file

fbosio opened this issue · comments

We're making some tests with the busted module and an empty yaml file is used in it, but the test raises an error.

How to reproduce this issue

Just run

local yaml = require "tinyyaml"
local t = yaml.parse("")

Expected result

t should be an empty table.

Obtained error

lua: ./tinyyaml.lua:684: bad argument #1 to 'sfind' (string expected, got nil)

Proposed fix

Add

if #lines == 0 then return {} end 

below line 684 of tinyyaml.lua.

Edit

The above doesn't take into account the case of a non-empty file that has comments only. A better fix would be

if sfind(lines[1] or "", '^%%YAML') then tremove(lines, 1) end

in line 704, i.e., add or "" next to if sfind(lines[1] (suggested by @gabrielbosio).