commit 5f6d367c51f7545a940119e7a393baf982b80f3f from: Gleb Kashkin via: Igor Munkin date: Wed Jul 12 11:59:19 2023 UTC compat: add is_new and is_old to options It used to be somewhat complicated to check the effective value of a compat option, because `.current` could contain 'default' state. This patch introduces helper functions that take care of that. The following alternatives were considered: * `compat..effective` - it is excessive in the presence if `current` and `default`, and is visible in serialization * `compat..get()` - while it is a function, it does only half of the work required, user still has to compare result to 'new' Closes #8807 @TarantoolBot document Title: Add `:is_new/old()` helpers to tarantool.compat options `compat..current` can be 'new', 'old' or 'default', thus when it is default there must be an additional check if `compat..default` is 'new'. It is handier to have a helper to deal with that instead of complicated `if`: * check if effective value is 'new' before the patch: ```lua if compat..current == 'new' or (compat..current == 'default' and compat..default == 'new') then ... end ``` * after the patch: ```lua if compat.:is_new() then ... end ``` Please update [tutorial on using compat], maybe add an example to [Listing options details]. [tutorial on using compat]: https://www.tarantool.io/en/doc/latest/reference/reference_lua/compat/compat_tutorial/ [Listing options details]: https://www.tarantool.io/en/doc/latest/reference/reference_lua/compat/compat_tutorial/#listing-options-details commit - 294f2d616c12334b0b9344ee21399bea932c3538 commit + 5f6d367c51f7545a940119e7a393baf982b80f3f blob - /dev/null blob + e10a4a2247ac628f5ba9e77b7902084e3e2635aa (mode 644) --- /dev/null +++ changelogs/unreleased/gh-8807-compat-is-new-old.md @@ -0,0 +1,4 @@ +## feature/lua + +* Added the `:is_new()` and `:is_old()` helpers to `tarantool.compat` + options to simplify effective value checks (gh-8807). blob - 6727b5a8dd3b042f6c27827780c6bce6515da2a7 blob + f7d3f855cf78340107f7f4c820d8791dd88b1a1e --- src/lua/compat.lua +++ src/lua/compat.lua @@ -444,8 +444,30 @@ function compat_mt.__index(_, key) result.current = 'new' else result.current = 'old' + end + + -- Whether the effective value of the option is `new`. + function result.is_new(option) + if type(option) ~= 'table' then + error('usage: compat.:is_new()') + end + if option.current == 'new' then + return true + end + if option.current == 'old' then + return false + end + return option.default == 'new' end + -- Whether the effective value of the option is `old`. + function result.is_old(option) + if type(option) ~= 'table' then + error(('usage: compat.%s:is_old()'):format(key)) + end + return not option:is_new() + end + return result end blob - 356ed3a5da9dbaa82fbf770d082803a178b06fff blob + 01d27a4b8504465b4672cecb7af216a529f26d01 --- test/app-luatest/gh_7000_compat_module_test.lua +++ test/app-luatest/gh_7000_compat_module_test.lua @@ -214,6 +214,26 @@ end g.test_help = function() t.assert(compat.help()) +end + +g.test_is_new_is_old = function() + for _, option_def in pairs(definitions) do + local name = option_def.name + t.assert(compat[name]) + + compat[name] = 'new' + t.assert(compat[name]:is_new()) + t.assert_not(compat[name]:is_old()) + + compat[name] = 'old' + t.assert_not(compat[name]:is_new()) + t.assert(compat[name]:is_old()) + + compat[name] = 'default' + local is_new = option_def.default == 'new' + t.assert_equals(compat[name]:is_new(), is_new) + t.assert_equals(compat[name]:is_old(), not is_new) + end end g.test_hot_reload = function()