Tarantool development patches archive
 help / color / mirror / Atom feed
From: olegrok@tarantool.org
To: tarantool-patches@dev.tarantool.org
Cc: Oleg Babin <babinoleg@mail.ru>
Subject: [Tarantool-patches] [PATCH] lua: implement is_decimal check
Date: Fri, 17 Jan 2020 11:03:01 +0300	[thread overview]
Message-ID: <20200117080301.97597-1-olegrok@tarantool.org> (raw)

From: Oleg Babin <babinoleg@mail.ru>

Some of our users want to have a native method
to check is specified value 'decimal' or not

This patch introduces 'is_decimal' check in 'decimal' module

Closes #4623

@TarantoolBot document
Title: decimal.is_decimal

is_decimal check function returns "true"
if specified value is decimal and "false" otherwise
---
Issue: https://github.com/tarantool/tarantool/issues/4623
Branch: olegrok/4623-is_decimal 
 src/lua/decimal.c         | 28 ++++++++++++++++++++++++++++
 test/app/decimal.result   | 29 +++++++++++++++++++++++++++++
 test/app/decimal.test.lua |  9 +++++++++
 3 files changed, 66 insertions(+)

diff --git a/src/lua/decimal.c b/src/lua/decimal.c
index 8905a0b9d..d3400521d 100644
--- a/src/lua/decimal.c
+++ b/src/lua/decimal.c
@@ -87,6 +87,23 @@ lua_pushdecimal(struct lua_State *L)
 	return res;
 }
 
+/**
+ * Returns true if a value at a given index is a decimal
+ * and false otherwise
+ */
+bool
+lua_isdecimal(struct lua_State *L, int index)
+{
+	if (lua_type(L, index) != LUA_TCDATA)
+		return false;
+
+	uint32_t ctypeid;
+	luaL_checkcdata(L, index, &ctypeid);
+	if (ctypeid != CTID_DECIMAL)
+		return false;
+	return true;
+}
+
 /** Check whether a value at a given index is a decimal. */
 static decimal_t *
 lua_checkdecimal(struct lua_State *L, int index)
@@ -272,6 +289,16 @@ ldecimal_new(struct lua_State *L)
 	return 1;
 }
 
+static int
+ldecimal_isdecimal(struct lua_State *L)
+{
+	if (lua_gettop(L) < 1)
+		luaL_error(L, "usage: decimal.is_decimal(value)");
+	bool is_decimal = lua_isdecimal(L, 1);
+	lua_pushboolean(L, is_decimal);
+	return 1;
+}
+
 static int
 ldecimal_round(struct lua_State *L)
 {
@@ -378,6 +405,7 @@ static const luaL_Reg ldecimal_lib[] = {
 	{"precision", ldecimal_precision},
 	{"abs", ldecimal_abs},
 	{"new", ldecimal_new},
+	{"is_decimal", ldecimal_isdecimal},
 	{NULL, NULL}
 };
 
diff --git a/test/app/decimal.result b/test/app/decimal.result
index 90d0984b0..8c0c3c1e4 100644
--- a/test/app/decimal.result
+++ b/test/app/decimal.result
@@ -625,3 +625,32 @@ a % 72
  | ---
  | - 29.96
  | ...
+
+-- gh-4623 is_decimal function
+decimal_value = decimal.new(1)
+ | ---
+ | ...
+decimal.is_decimal(decimal_value)
+ | ---
+ | - true
+ | ...
+decimal.is_decimal('string')
+ | ---
+ | - false
+ | ...
+decimal.is_decimal(0)
+ | ---
+ | - false
+ | ...
+decimal.is_decimal(-1ULL)
+ | ---
+ | - false
+ | ...
+decimal.is_decimal(box.error.new(box.error.UNKNOWN))
+ | ---
+ | - false
+ | ...
+decimal.is_decimal(ffi.cast('char*', '42.42'))
+ | ---
+ | - false
+ | ...
diff --git a/test/app/decimal.test.lua b/test/app/decimal.test.lua
index 79189a2f5..8e838aa32 100644
--- a/test/app/decimal.test.lua
+++ b/test/app/decimal.test.lua
@@ -177,3 +177,12 @@ a % 100
 a % 173
 a % 72
 720 % a
+
+-- gh-4623 is_decimal function
+decimal_value = decimal.new(1)
+decimal.is_decimal(decimal_value)
+decimal.is_decimal('string')
+decimal.is_decimal(0)
+decimal.is_decimal(-1ULL)
+decimal.is_decimal(box.error.new(box.error.UNKNOWN))
+decimal.is_decimal(ffi.cast('char*', '42.42'))
-- 
2.23.0

             reply	other threads:[~2020-01-17  8:03 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-17  8:03 olegrok [this message]
2020-02-14 14:36 ` Serge Petrenko
2020-02-17 11:35 ` Kirill Yukhin

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=20200117080301.97597-1-olegrok@tarantool.org \
    --to=olegrok@tarantool.org \
    --cc=babinoleg@mail.ru \
    --cc=tarantool-patches@dev.tarantool.org \
    --subject='Re: [Tarantool-patches] [PATCH] lua: implement is_decimal check' \
    /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