lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


The last days I spent some time with the YAML Lua module found at https://github.com/arcapos/luayaml.

I more or less completely reworked the module.  It seems to work just fine now.

Documentation can be found at https://lua.msys.ch/lua-module-reference.html#_yaml

The most interesting extension is the introduction of Lua specific YAML labels:

!Lua/function assigns a Lua chunk to a value, thus the table returned by the parser can contain Lua functions:

a_function: !Lua/function
  a = 40 + 2
  return 'The answer is .. ' a

When you parse this YAML using the yaml.parse() function, the table returned will contain an index "a_function", which is a function:

local t = yaml.parse([[
a_function: !Lua/function
  a = 40 + 2
  return 'The answer is .. ' a
]]

print(t.a_function())

!Lua/call directly calls a Lua chunk and assigns to the value whatever that function returns:

local t = yaml.parse([[
a_value: !Lua/call return 'Hello, world!
]]

print(t.a_value) -- prints Hello, world!

The two parsing functions yaml.parse() and yaml.parsefile() can be handed an optional second argument, which is an environment that serves as the global environment (_ENV) for the Lua code in both use cases.

My own use case for this module is a flexible report generator that uses YAML for report definition.

- mb