[Tarantool-patches] [PATCH] lua: implement is_decimal check
Serge Petrenko
sergepetrenko at tarantool.org
Fri Feb 14 17:36:47 MSK 2020
Hi! Thanks for the patch!
> 17 янв. 2020 г., в 11:03, olegrok at tarantool.org написал(а):
>
> From: Oleg Babin <babinoleg at 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 at tarantool.org
More information about the Tarantool-patches
mailing list