From 49061141efb2af58ee2f3c2c32790368991b6474 Mon Sep 17 00:00:00 2001 From: Thomas Desveaux Date: Wed, 6 Sep 2017 14:44:42 +0200 Subject: [PATCH 1/3] Fix context.mergeFilters --- src/base/context.lua | 10 ++++++++-- src/base/table.lua | 30 +++++++++++++++++++++++++++++ tests/base/test_context.lua | 38 +++++++++++++++++++++++++++++++++++++ 3 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/base/context.lua b/src/base/context.lua index 4c8e5fb57e..33b3c84a61 100644 --- a/src/base/context.lua +++ b/src/base/context.lua @@ -137,8 +137,14 @@ -- function context.mergeFilters(ctx, src) - for k,v in pairs(src.terms) do - ctx.terms[k] = v + if src.terms ~= nil then + for k, v in pairs(src.terms) do + ctx.terms[k] = table.joinunique(ctx.terms[k] or {}, src.terms[k]) + + if #ctx.terms[k] == 1 then + ctx.terms[k] = ctx.terms[k][1] + end + end end end diff --git a/src/base/table.lua b/src/base/table.lua index 99c257628b..c6b3d0570c 100644 --- a/src/base/table.lua +++ b/src/base/table.lua @@ -581,3 +581,33 @@ end return result end + +-- +-- Adds the values from one array to the end of another without duplicates and +-- returns the result. +-- + function table.joinunique(...) + + local arg = {...} + local elems = { } + local result = { } + + local function insertunique(elem) + if not elems[elem] then + table.insert(result, elem) + elems[elem] = true + end + end + + for _,t in ipairs(arg) do + + if type(t) == "table" then + table.foreachi(t, insertunique) + else + insertunique(t) + end + + end + + return result + end diff --git a/tests/base/test_context.lua b/tests/base/test_context.lua index f04ee7ba6e..6e98ed42a0 100644 --- a/tests/base/test_context.lua +++ b/tests/base/test_context.lua @@ -73,3 +73,41 @@ -- detoken in extended context should result in value set in that environ. test.isequal("text", ext.targetname) end + +-- +-- mergeFilters should behave as expected for tags +-- + + function suite.mergeFilters() + + ctx = { terms = { tags = { "ctxtags" } } } + src = { terms = { tags = { "srctags" } } } + + context.mergeFilters(ctx, src) + + result = { terms = { tags = { "ctxtags", "srctags" } } } + + test.isequal(result, ctx) + end + + function suite.mergeFilters_keeptype() + + ctx = { terms = { kind = "ConsoleApp" } } + src = { terms = { kind = "ConsoleApp" } } + + context.mergeFilters(ctx, src) + + test.isequal("string", type(ctx.terms.kind)) + end + + function suite.mergeFilters_createtable() + + ctx = { terms = { tags = "ctxtags" } } + src = { terms = { tags = "srctags" } } + + context.mergeFilters(ctx, src) + + result = { terms = { tags = { "ctxtags", "srctags" } } } + + test.isequal(result, ctx) + end From 22e36f97b8195d93e258d673f0684754f20767d7 Mon Sep 17 00:00:00 2001 From: thomas desveaux Date: Thu, 12 Apr 2018 23:40:16 +0200 Subject: [PATCH 2/3] Better performance on fix issue #884 --- src/base/context.lua | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/base/context.lua b/src/base/context.lua index 96d483e2d8..fe0e80f1f8 100644 --- a/src/base/context.lua +++ b/src/base/context.lua @@ -137,13 +137,11 @@ -- function context.mergeFilters(ctx, src) - if src.terms ~= nil then - for k, v in pairs(src.terms) do - ctx.terms[k] = table.joinunique(ctx.terms[k] or {}, src.terms[k]) - - if #ctx.terms[k] == 1 then - ctx.terms[k] = ctx.terms[k][1] - end + for k, v in pairs(src.terms) do + if k == "tags" then + ctx.terms[k] = table.join(ctx.terms[k], v) + else + ctx.terms[k] = v end end end From a8ad766612a5b469a924948e6531ffc6186c62a7 Mon Sep 17 00:00:00 2001 From: Thomas Desveaux Date: Fri, 13 Apr 2018 19:06:36 +0200 Subject: [PATCH 3/3] Remove no longer used table.joinunique --- src/base/table.lua | 30 ------------------------------ 1 file changed, 30 deletions(-) diff --git a/src/base/table.lua b/src/base/table.lua index 29415dbf7e..d317586c0a 100644 --- a/src/base/table.lua +++ b/src/base/table.lua @@ -600,33 +600,3 @@ end return result end - --- --- Adds the values from one array to the end of another without duplicates and --- returns the result. --- - function table.joinunique(...) - - local arg = {...} - local elems = { } - local result = { } - - local function insertunique(elem) - if not elems[elem] then - table.insert(result, elem) - elems[elem] = true - end - end - - for _,t in ipairs(arg) do - - if type(t) == "table" then - table.foreachi(t, insertunique) - else - insertunique(t) - end - - end - - return result - end