]> source.dussan.org Git - rspamd.git/commitdiff
[Feature] Lua_selectors: Add sort and uniq transform functions
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 24 Jun 2019 12:07:51 +0000 (13:07 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Mon, 24 Jun 2019 12:07:51 +0000 (13:07 +0100)
lualib/lua_selectors.lua

index b678ef17997ca2209211329bd9209307116c491b..15777fc111285dd0575f2d22cba3017db4b01ab9 100644 (file)
@@ -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'] = {