Skip to content
Snippets Groups Projects
Commit 39918baf authored by Vladislav Shpilevoy's avatar Vladislav Shpilevoy Committed by Kirill Yukhin
Browse files

key_def: key_def.new() accept both 'field' and 'fieldno'

Closes #4519

@TarantoolBot document
Title: key_def.new() accept both 'field' and 'fieldno'

Before the patch key_def.new() took an index part
array as it is returned in <index_object>.parts: each
part should include 'type', 'fieldno', and what else
.parts element contains.

But it was not possible to create a key_def from an
index definition - the array passed to
<space_object>.create_index() 'parts' argument. Because
key_def.new() didn't recognize 'field' option. That
might be useful, when a key_def is needed on a remote
client, where a space object and its indexes do not
exist. And it would be strange to force a user to
create them just so as he would be able to access

    <net_box connection>.space.<space_name>.
        index.<index_name>.parts

As well as it would be crutchy to make a user manually
replace 'field' with 'fieldno' in its index definition
just to create a key_def.

Additionally, an ability to pass an index definition
to a key_def constructor makes the API more symmetric.

Note, that it still is not 100% symmetric, because a
user can't pass field names to the key_def
constructor. A space is needed for that anyway.
parent 00c6c437
No related branches found
No related tags found
No related merge requests found
...@@ -88,8 +88,26 @@ luaT_key_def_set_part(struct lua_State *L, struct key_part_def *part, ...@@ -88,8 +88,26 @@ luaT_key_def_set_part(struct lua_State *L, struct key_part_def *part,
lua_pushstring(L, "fieldno"); lua_pushstring(L, "fieldno");
lua_gettable(L, -2); lua_gettable(L, -2);
if (lua_isnil(L, -1)) { if (lua_isnil(L, -1)) {
diag_set(IllegalParams, "fieldno must not be nil"); lua_pop(L, 1);
return -1; /*
* 'field' is an alias for fieldno to support the
* same parts format as is used in
* <space_object>.create_index() in Lua.
*/
lua_getfield(L, -1, "field");
if (lua_isnil(L, -1)) {
diag_set(IllegalParams,
"fieldno or field must not be nil");
return -1;
}
} else {
lua_getfield(L, -2, "field");
if (! lua_isnil(L, -1)) {
diag_set(IllegalParams,
"Conflicting options: fieldno and field");
return -1;
}
lua_pop(L, 1);
} }
/* /*
* Transform one-based Lua fieldno to zero-based * Transform one-based Lua fieldno to zero-based
......
...@@ -154,6 +154,29 @@ local key_def_new_cases = { ...@@ -154,6 +154,29 @@ local key_def_new_cases = {
}}, }},
exp_err = nil, exp_err = nil,
}, },
--
-- gh-4519: key_def should allow the same options as
-- <space_object>.create_index(). That is, a field number
-- should be allowed to be specified as `field`, not only
-- `fieldno`.
--
{
'Success case; `field` is alias to `fieldno`',
parts = {{
field = 1,
type = 'unsigned'
}},
exp_err = nil,
},
{
'Field and fieldno can not be set both',
parts = {{
field = 1,
fieldno = 1,
type = 'unsigned'
}},
exp_err = 'Conflicting options: fieldno and field',
}
} }
local test = tap.test('key_def') local test = tap.test('key_def')
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment