From 9b74e57ad3fb694741fdcf5e27a046fbe99a40f7 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 8 Jul 2019 13:01:08 +0100 Subject: [Feature] Libucl: Allow to sort keys in ucl objects --- contrib/libucl/lua_ucl.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) (limited to 'contrib/libucl/lua_ucl.c') diff --git a/contrib/libucl/lua_ucl.c b/contrib/libucl/lua_ucl.c index b97387d9e..049b1d08c 100644 --- a/contrib/libucl/lua_ucl.c +++ b/contrib/libucl/lua_ucl.c @@ -1368,6 +1368,7 @@ lua_ucl_to_format (lua_State *L) { ucl_object_t *obj; int format = UCL_EMIT_JSON; + bool sort = false; if (lua_gettop (L) > 1) { if (lua_type (L, 2) == LUA_TNUMBER) { @@ -1397,10 +1398,22 @@ lua_ucl_to_format (lua_State *L) format = UCL_EMIT_MSGPACK; } } + + if (lua_isboolean (L, 3)) { + sort = lua_toboolean (L, 3); + } } obj = ucl_object_lua_import (L, 1); + if (obj != NULL) { + + if (sort) { + if (ucl_object_type (obj) == UCL_OBJECT) { + ucl_object_sort_keys (obj, UCL_SORT_KEYS_RECURSIVE); + } + } + lua_ucl_to_string (L, obj, format); ucl_object_unref (obj); } -- cgit v1.2.3