[Tarantool-patches] [PATCH] lua: throw correct error on calls to sequence methods with dot

Dmitriy Koltsov dkoltsov at picodata.io
Thu May 21 15:38:47 MSK 2020


Make error on box.sequence.<sequence_ex>.<method> call
similar to space error.
Ex: 'Use sequence:next(...) instead of sequence.next(...)'.
Closes #4754
---
 
Github: https://github.com/VifleY/tarantool/tree/dkoltsov-fix-gh4754
Issue: https://github.com/tarantool/tarantool/issues/4754
 
 src/box/lua/schema.lua     | 19 +++++++++++++++++++
 test/box/sequence.result   | 30 ++++++++++++++++++++++++++++++
 test/box/sequence.test.lua | 12 ++++++++++++
 3 files changed, 61 insertions(+)
diff --git a/src/box/lua/schema.lua b/src/box/lua/schema.lua
index 85fcca562..5fc3128c4 100644
--- a/src/box/lua/schema.lua
+++ b/src/box/lua/schema.lua
@@ -1809,14 +1809,25 @@ function box.schema.space.bless(space)
     end
 end
 
+-- Helper function to check sequnce:method() usage
+local function check_sequence_arg(seq, method)
+    if type(seq) ~= 'table' or seq.id == nil then
+        local fmt = 'Use sequence:%s(...) instead of sequence.%s(...)'
+        error(string.format(fmt, method, method))
+    end
+end
+
 local sequence_mt = {}
 sequence_mt.__index = sequence_mt
 
 sequence_mt.next = function(self)
+    check_sequence_arg(self, 'next')
     return internal.sequence.next(self.id)
 end
 
 sequence_mt.current = function(self)
+    check_sequence_arg(self, 'current')
+
     local ai64 = buffer.reg1.ai64
     local rc = builtin.box_sequence_current(self.id, ai64)
     if rc < 0 then
@@ -1826,18 +1837,26 @@ sequence_mt.current = function(self)
 end
 
 sequence_mt.set = function(self, value)
+    check_sequence_arg(self, 'set')
+
     return internal.sequence.set(self.id, value)
 end
 
 sequence_mt.reset = function(self)
+    check_sequence_arg(self, 'reset')
+
     return internal.sequence.reset(self.id)
 end
 
 sequence_mt.alter = function(self, opts)
+    check_sequence_arg(self, 'alter')
+
     box.schema.sequence.alter(self.id, opts)
 end
 
 sequence_mt.drop = function(self)
+    check_sequence_arg(self, 'drop')
+
     box.schema.sequence.drop(self.id)
 end
 
diff --git a/test/box/sequence.result b/test/box/sequence.result
index eb1d80d1b..17cdd8021 100644
--- a/test/box/sequence.result
+++ b/test/box/sequence.result
@@ -2379,3 +2379,33 @@ sq:current()
 sq:drop()
 ---
 ...
+--
+-- gh-4754: throw error similar to space.method() error
+-- Ex: 'Use sequence:next(...) instead of sequence.next(...)'
+--
+sq = box.schema.sequence.create('test')
+---
+...
+sq.next()
+---
+- error: 'builtin/box/schema.lua:1816: Use sequence:next(...) instead of sequence.next(...)'
+...
+sq.current()
+---
+- error: 'builtin/box/schema.lua:1816: Use sequence:current(...) instead of sequence.current(...)'
+...
+sq.set()
+---
+- error: 'builtin/box/schema.lua:1816: Use sequence:set(...) instead of sequence.set(...)'
+...
+sq.reset()
+---
+- error: 'builtin/box/schema.lua:1816: Use sequence:reset(...) instead of sequence.reset(...)'
+...
+sq.drop()
+---
+- error: 'builtin/box/schema.lua:1816: Use sequence:drop(...) instead of sequence.drop(...)'
+...
+sq:drop()
+---
+...
\ No newline at end of file
diff --git a/test/box/sequence.test.lua b/test/box/sequence.test.lua
index a49565b8a..9ea61daef 100644
--- a/test/box/sequence.test.lua
+++ b/test/box/sequence.test.lua
@@ -812,3 +812,15 @@ sq:current()
 sq:reset()
 sq:current()
 sq:drop()
+
+--
+-- gh-4754: throw error similar to space.method() error
+-- Ex: 'Use sequence:next(...) instead of sequence.next(...)'
+--
+sq = box.schema.sequence.create('test')
+sq.next()
+sq.current()
+sq.set()
+sq.reset()
+sq.drop()
+sq:drop()
\ No newline at end of file
-- 
2.17.1
 
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://lists.tarantool.org/pipermail/tarantool-patches/attachments/20200521/f0d5c70e/attachment.html>


More information about the Tarantool-patches mailing list