Tarantool development patches archive
 help / color / mirror / Atom feed
* [Tarantool-patches] [PATCH] lua: implement is_decimal check
@ 2020-01-17  8:03 olegrok
  2020-02-14 14:36 ` Serge Petrenko
  2020-02-17 11:35 ` Kirill Yukhin
  0 siblings, 2 replies; 3+ messages in thread
From: olegrok @ 2020-01-17  8:03 UTC (permalink / raw)
  To: tarantool-patches; +Cc: Oleg Babin

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

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Tarantool-patches] [PATCH] lua: implement is_decimal check
  2020-01-17  8:03 [Tarantool-patches] [PATCH] lua: implement is_decimal check olegrok
@ 2020-02-14 14:36 ` Serge Petrenko
  2020-02-17 11:35 ` Kirill Yukhin
  1 sibling, 0 replies; 3+ messages in thread
From: Serge Petrenko @ 2020-02-14 14:36 UTC (permalink / raw)
  To: olegrok; +Cc: Oleg Babin, tarantool-patches

Hi! Thanks for the patch!

> 17 янв. 2020 г., в 11:03, olegrok@tarantool.org написал(а):
> 
> 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
> 

LGTM

--
Serge Petrenko
sergepetrenko@tarantool.org

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [Tarantool-patches] [PATCH] lua: implement is_decimal check
  2020-01-17  8:03 [Tarantool-patches] [PATCH] lua: implement is_decimal check olegrok
  2020-02-14 14:36 ` Serge Petrenko
@ 2020-02-17 11:35 ` Kirill Yukhin
  1 sibling, 0 replies; 3+ messages in thread
From: Kirill Yukhin @ 2020-02-17 11:35 UTC (permalink / raw)
  To: olegrok; +Cc: Oleg Babin, tarantool-patches

Hello,

On 17 янв 11:03, olegrok@tarantool.org wrote:
> 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

LGTM.

I've checked your patch into master.

--
Regards, Kirill Yukhin

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2020-02-17 11:35 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-17  8:03 [Tarantool-patches] [PATCH] lua: implement is_decimal check olegrok
2020-02-14 14:36 ` Serge Petrenko
2020-02-17 11:35 ` Kirill Yukhin

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