gvvaughan / lyaml

LibYAML binding for Lua.
gvvaughan.github.io/lyaml
Other
209 stars 34 forks source link

Unexpected type behavior when importing a YAML file #42

Closed fmitha closed 3 years ago

fmitha commented 3 years ago

Consider the following YAML file, say y.yaml

2017.11:
2017.11.02:

Then consider the following loading code:

local function loadyaml(config)
   local posix = require "posix"
   local lyaml = require "lyaml"
   local s
   if posix.stat(config) ~= nil then
      local f = io.open(config)
      f:close()
      return lyaml.load (s)
   end
end

x = loadyaml("y.yaml")
for k,v in pairs(x) do
  print(type(k),type(v))
  print(k,v)
end

The result is

number  table
2017.11 table: 0x5620e93dd8a0
string  table
2017.11.02      table: 0x5620e93dd8a0

I know that Lua likes to type things as numbers which can be considered to be numbers under certain circumstances, but is this really appropriate behavior here?

If one simply assigns these values, Lua doesn't type them differently.

foo = "2017.11" date1 = "2017.11" print(type(date1)) string date2 = "2017.11.02" print(type(date2)) string

fmitha commented 3 years ago

Actually, it looks like this is expected behavior for YAML, thinking about it further. Python also does basically the same thing (it gives float). There doesn't seem to be a delete option, so closing.