Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix opts table damaging by CRUD methods #200

Merged
merged 14 commits into from
Oct 12, 2021
Merged

Fix opts table damaging by CRUD methods #200

merged 14 commits into from
Oct 12, 2021

Conversation

AnaNek
Copy link
Contributor

@AnaNek AnaNek commented Aug 17, 2021

If we have opts variable and we pass it to some crud method
(upsert_object for example) and then use opts variable again
for crud method (get for example) we receive an error because
*_object method damages opts variable adding
add_space_schema_hash parameter to opts variable.

Closes #192

@AnaNek AnaNek requested review from ligurio and olegrok August 17, 2021 10:53
Copy link
Member

@ligurio ligurio left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the patch!

test/integration/simple_operations_test.lua Outdated Show resolved Hide resolved

-- insert_object
local insert_opts = {timeout = 1, bucket_id = 477, fields = {'name', 'age'}}
local new_insert_opts, err = g.cluster.main_server.net_box:eval([[
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you use eval() instead of call()?

    -- insert_object
    local result, err = g.cluster.main_server.net_box:call(
            'crud.insert_object', {
                'customers',
                {id = 1, name = 'Fedor', age = 59},
                insert_opts
            }
    )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If I would use call() I could not catch modification of opts table

crud/insert.lua Outdated Show resolved Hide resolved
Copy link
Contributor

@olegrok olegrok left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your patch. See my two suggestions.

crud/upsert.lua Outdated Show resolved Hide resolved
test/integration/simple_operations_test.lua Outdated Show resolved Hide resolved
crud/insert.lua Outdated Show resolved Hide resolved
test/integration/len_test.lua Outdated Show resolved Hide resolved
@@ -524,4 +524,17 @@ function utils.flatten_obj_reload(space_name, obj)
return schema.wrap_func_reload(flatten_obj, space_name, obj)
end

function utils.make_options(opts, add_space_schema_hash_value)
Copy link
Contributor

@mRrvz mRrvz Sep 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to me that the word create is more suitable here (not make), or get

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make_option name is often used for parsing options (in Python modules for example). Thats why it seems to me that this name is suitable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A bit more bikeshedding: it looks specific for the add_space_schema_hash option, so it would be better either choose a specific name or rewrite the function to be common:

-- Merge two options map.
--
-- `a` and/or `b` can be `nil`.
--
-- If `a.foo` and `b.foo` exists, prefer `b.foo`.
function utils.merge_options(a, b)
    return fun.chain(a or {}, b or {}):tomap()
end

So the call will be the following:

-opts = utils.make_options(opts, true)
+opts = utils.merge_options(opts, {add_space_schema_hash = true})

Maybe a bit more Lua tables, but the call becomes more readable (now we see what is the true/false value).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated:

+-- Merge two options map.
+--
+-- `opts_a` and/or `opts_b` can be `nil`.
+--
+-- If `opts_a.foo` and `opts_b.foo` exists, prefer `opts_b.foo`.
+function utils.merge_options(opts_a, opts_b)
+    return fun.chain(opts_a or {}, opts_b or {}):tomap()
+end

Copy link
Member

@Totktonada Totktonada left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM (but there is a nitpicking around naming, see below).

@AnaNek AnaNek merged commit d13fe63 into master Oct 12, 2021
@AnaNek AnaNek deleted the fix-opts-damage branch October 12, 2021 08:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CRUD methods damage opts table
5 participants