Tarantool development patches archive
 help / color / mirror / Atom feed
From: Serge Petrenko via Tarantool-patches <tarantool-patches@dev.tarantool.org>
To: vdavydov@tarantool.org, sergos@tarantool.org
Cc: tarantool-patches@dev.tarantool.org
Subject: [Tarantool-patches] [PATCH v2 1/2] lua: introduce table.equals method
Date: Fri, 13 Aug 2021 02:30:31 +0300	[thread overview]
Message-ID: <3a954f7793e6356cefb8cd0151a6434382ec876c.1628810253.git.sergepetrenko@tarantool.org> (raw)
In-Reply-To: <cover.1628810253.git.sergepetrenko@tarantool.org>

Introduce table.equals for comparing tables.
The method respects __eq metamethod, if provided.

Needed-for #5894
---
 src/lua/table.lua           | 26 ++++++++++++++++++++++++++
 test/app-tap/table.test.lua | 31 ++++++++++++++++++++++++++++++-
 2 files changed, 56 insertions(+), 1 deletion(-)

diff --git a/src/lua/table.lua b/src/lua/table.lua
index 8fa9b876a..5f35a30f6 100644
--- a/src/lua/table.lua
+++ b/src/lua/table.lua
@@ -57,6 +57,31 @@ local function table_shallowcopy(orig)
     return copy
 end
 
+--- Compare two lua tables
+-- Supports __eq metamethod for comparing custom tables with metatables
+-- @function equals
+-- @return true when the two tables are equal (false otherwise).
+local function table_equals(a, b)
+    if type(a) ~= 'table' or type(b) ~= 'table' then
+        return a == b
+    end
+    local mt = getmetatable(a)
+    if mt and mt.__eq then
+        return a == b
+    end
+    for k, v in pairs(a) do
+        if not table_equals(v, b[k]) then
+            return false
+        end
+    end
+    for k, _ in pairs(b) do
+        if not a[k] then
+            return false
+        end
+    end
+    return true
+end
+
 -- table library extension
 local table = require('table')
 -- require modifies global "table" module and adds "clear" function to it.
@@ -65,3 +90,4 @@ require('table.clear')
 
 table.copy     = table_shallowcopy
 table.deepcopy = table_deepcopy
+table.equals   = table_equals
diff --git a/test/app-tap/table.test.lua b/test/app-tap/table.test.lua
index 60c095fdf..a3c9aa123 100755
--- a/test/app-tap/table.test.lua
+++ b/test/app-tap/table.test.lua
@@ -8,7 +8,7 @@ yaml.cfg{
     encode_invalid_as_nil  = true,
 }
 local test = require('tap').test('table')
-test:plan(31)
+test:plan(38)
 
 do -- check basic table.copy (deepcopy)
     local example_table = {
@@ -223,4 +223,33 @@ do -- check usage of not __copy metamethod on second level + shallow
     )
 end
 
+do -- check table.equals
+    test:ok(table.equals({}, {}), "table.equals for empty tables")
+    test:is(table.equals({}, {1}), false, "table.equals with one empty table")
+    test:is(table.equals({1}, {}), false, "table.equals with one empty table")
+    local tbl_a = {
+        first = {
+            1,
+            2,
+            {},
+        },
+        second = {
+            a = {
+                {'something'},
+            },
+            b = 'something else',
+        },
+        [3] = 'some value',
+    }
+    local tbl_b = table.deepcopy(tbl_a)
+    local tbl_c = table.copy(tbl_a)
+    test:ok(table.equals(tbl_a, tbl_b), "table.equals for complex tables")
+    test:ok(table.equals(tbl_a, tbl_c),
+            "table.equals for shallow copied tables")
+    tbl_c.second.a = 'other thing'
+    test:ok(table.equals(tbl_a, tbl_c),
+            "table.equals for shallow copied tables after modification")
+    test:is(table.equals(tbl_a, tbl_b), false, "table.equals does a deep check")
+end
+
 os.exit(test:check() == true and 0 or 1)
-- 
2.30.1 (Apple Git-130)


  reply	other threads:[~2021-08-12 23:34 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-12 23:30 [Tarantool-patches] [PATCH v2 0/2] allow upgrading from version 1.6 Serge Petrenko via Tarantool-patches
2021-08-12 23:30 ` Serge Petrenko via Tarantool-patches [this message]
2021-08-13  5:30   ` [Tarantool-patches] [PATCH v2 1/2] lua: introduce table.equals method Oleg Babin via Tarantool-patches
2021-08-13 10:22     ` Serge Petrenko via Tarantool-patches
2021-08-13 20:13       ` Oleg Babin via Tarantool-patches
2021-08-16  7:53         ` Serge Petrenko via Tarantool-patches
2021-08-16 13:03           ` Бабин Олег via Tarantool-patches
2021-08-16 15:36             ` Serge Petrenko via Tarantool-patches
2021-08-16 15:41               ` Бабин Олег via Tarantool-patches
2021-08-16 16:47                 ` Serge Petrenko via Tarantool-patches
2021-08-13 11:41   ` Vladimir Davydov via Tarantool-patches
2021-08-12 23:30 ` [Tarantool-patches] [PATCH v2 2/2] box: allow upgrading from version 1.6 Serge Petrenko via Tarantool-patches
2021-08-13 11:40   ` Vladimir Davydov via Tarantool-patches
2021-08-16 13:18   ` Бабин Олег via Tarantool-patches
2021-08-16 16:32     ` Serge Petrenko via Tarantool-patches
2021-08-16 18:22       ` Бабин Олег via Tarantool-patches
2021-08-14  8:12 ` [Tarantool-patches] [PATCH v2 0/2] " Vitaliia Ioffe via Tarantool-patches
2021-08-17  7:28 ` Kirill Yukhin via Tarantool-patches
2021-08-17  8:05   ` Kirill Yukhin via Tarantool-patches

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3a954f7793e6356cefb8cd0151a6434382ec876c.1628810253.git.sergepetrenko@tarantool.org \
    --to=tarantool-patches@dev.tarantool.org \
    --cc=sergepetrenko@tarantool.org \
    --cc=sergos@tarantool.org \
    --cc=vdavydov@tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH v2 1/2] lua: introduce table.equals method' \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox