From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp34.i.mail.ru (smtp34.i.mail.ru [94.100.177.94]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 5917E469719 for ; Fri, 14 Feb 2020 17:36:48 +0300 (MSK) Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 13.0 \(3608.40.2.2.4\)) From: Serge Petrenko In-Reply-To: <20200117080301.97597-1-olegrok@tarantool.org> Date: Fri, 14 Feb 2020 17:36:47 +0300 Content-Transfer-Encoding: quoted-printable Message-Id: <3E3736BC-6DF4-4BE6-A0F5-21B2E35E98FD@tarantool.org> References: <20200117080301.97597-1-olegrok@tarantool.org> Subject: Re: [Tarantool-patches] [PATCH] lua: implement is_decimal check List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: olegrok@tarantool.org Cc: Oleg Babin , tarantool-patches@dev.tarantool.org Hi! Thanks for the patch! > 17 =D1=8F=D0=BD=D0=B2. 2020 =D0=B3., =D0=B2 11:03, = olegrok@tarantool.org =D0=BD=D0=B0=D0=BF=D0=B8=D1=81=D0=B0=D0=BB(=D0=B0): >=20 > From: Oleg Babin >=20 > Some of our users want to have a native method > to check is specified value 'decimal' or not >=20 > This patch introduces 'is_decimal' check in 'decimal' module >=20 > Closes #4623 >=20 > @TarantoolBot document > Title: decimal.is_decimal >=20 > 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=20 > src/lua/decimal.c | 28 ++++++++++++++++++++++++++++ > test/app/decimal.result | 29 +++++++++++++++++++++++++++++ > test/app/decimal.test.lua | 9 +++++++++ > 3 files changed, 66 insertions(+) >=20 > 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; > } >=20 > +/** > + * 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) !=3D LUA_TCDATA) > + return false; > + > + uint32_t ctypeid; > + luaL_checkcdata(L, index, &ctypeid); > + if (ctypeid !=3D 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; > } >=20 > +static int > +ldecimal_isdecimal(struct lua_State *L) > +{ > + if (lua_gettop(L) < 1) > + luaL_error(L, "usage: decimal.is_decimal(value)"); > + bool is_decimal =3D 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[] =3D { > {"precision", ldecimal_precision}, > {"abs", ldecimal_abs}, > {"new", ldecimal_new}, > + {"is_decimal", ldecimal_isdecimal}, > {NULL, NULL} > }; >=20 > 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 =3D 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 =3D 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')) > =E2=80=94=20 > 2.23.0 >=20 LGTM -- Serge Petrenko sergepetrenko@tarantool.org