commit 7a0b994: [Feature] Lua_selectors: Add sort and uniq transform functions

Vsevolod Stakhov vsevolod at highsecure.ru
Mon Jun 24 12:14:05 UTC 2019


Author: Vsevolod Stakhov
Date: 2019-06-24 13:07:51 +0100
URL: https://github.com/rspamd/rspamd/commit/7a0b99449428fc397d7c9f7d28cb1de34c3ecee7 (HEAD -> master)

[Feature] Lua_selectors: Add sort and uniq transform functions

---
 lualib/lua_selectors.lua | 26 ++++++++++++++++++++++++++
 1 file changed, 26 insertions(+)

diff --git a/lualib/lua_selectors.lua b/lualib/lua_selectors.lua
index b678ef179..15777fc11 100644
--- a/lualib/lua_selectors.lua
+++ b/lualib/lua_selectors.lua
@@ -423,6 +423,32 @@ local transform_function = {
     ['description'] = 'Joins strings into a single string using separator in the argument',
     ['args_schema'] = {ts.string:is_optional()}
   },
+  -- Sort strings
+  ['sort'] = {
+    ['types'] = {
+      ['list'] = true
+    },
+    ['process'] = function(inp, t, _)
+      table.sort(inp)
+      return inp, t
+    end,
+    ['description'] = 'Sort strings lexicographically',
+  },
+  -- Return unique elements based on hashing (can work without sorting)
+  ['uniq'] = {
+    ['types'] = {
+      ['list'] = true
+    },
+    ['process'] = function(inp, t, _)
+      local tmp = {}
+      for _,val in ipairs(inp) do
+        tmp[val] = true
+      end
+
+      return fun.map(function(k, _) return k end, tmp), t
+    end,
+    ['description'] = 'Returns a list of unique elements (using a hash table)',
+  },
   -- Create a digest from string or a list of strings
   ['digest'] = {
     ['types'] = {


More information about the Commits mailing list