extract is_table_empty to separate utils lib

This commit is contained in:
Karol Wypchlo 2022-03-15 22:53:31 +01:00
parent d631aad058
commit e8345a3be6
No known key found for this signature in database
GPG Key ID: B515DE9EEBE241E1
3 changed files with 25 additions and 10 deletions

View File

@ -14,15 +14,6 @@ local anon_limits = {
["registry"] = 250
}
-- utility function for checking if table is empty
function is_table_empty(check)
-- bind next to local variable to achieve ultimate efficiency
-- https://stackoverflow.com/a/1252776
local next = next
return next(check) == nil
end
-- get all non empty authentication headers from request, we want to return
-- all of them and let accounts service deal with validation and prioritisation
function _M.get_auth_headers()
@ -69,10 +60,11 @@ end
function _M.get_account_limits()
local cjson = require('cjson')
local utils = require('utils')
local auth_headers = _M.get_auth_headers()
-- simple case of anonymous request - none of available auth headers exist
if is_table_empty(auth_headers) then
if utils.is_table_empty(auth_headers) then
return anon_limits
end

View File

@ -0,0 +1,12 @@
local _M = {}
-- utility function for checking if table is empty
function is_table_empty(check)
-- bind next to local variable to achieve ultimate efficiency
-- https://stackoverflow.com/a/1252776
local next = next
return next(check) == nil
end
return _M

View File

@ -0,0 +1,11 @@
local utils = require('utils')
describe("is_table_empty", function()
it("should return true for empty table", function()
assert.is_true(utils.is_table_empty({}))
end)
it("should return false for not empty table", function()
assert.is_false(utils.is_table_empty({ ["foo"] = "bar" }))
end)
end)