gvvaughan / lyaml

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

[feature request] Ordered mapping keys #52

Open agladysh opened 1 year ago

agladysh commented 1 year ago

I would like to order mapping keys in the dump for human readability.

For my use case a flat list is enough.

How about something like this?

yaml.dump(data, { mapping_key_order = { "key1", "key2", ... } }) -- keys not in the list are dumped in the end

and, for finer control:

yaml.dump(data, { mapping_key_order = function(mapping) -- This may be the default if mapping_key_order = true
   local keys = tkeys(mapping)
   table.sort(keys)
   return keys
end }) -- keys not in the list are dumped in the end

If mapping_key_order is not specified, use the current implementation for performance.

agladysh commented 1 year ago

The simplest solution would probably be to allow user to supply alternative pairs() implementation to be used in the dump_mapping here:

https://github.com/gvvaughan/lyaml/blob/master/lib/lyaml/init.lua#L124