From e8345a3be6cc6bf8054a7e9118056c00b436cc9c Mon Sep 17 00:00:00 2001 From: Karol Wypchlo Date: Tue, 15 Mar 2022 22:53:31 +0100 Subject: [PATCH] extract is_table_empty to separate utils lib --- docker/nginx/libs/skynet/account.lua | 12 ++---------- docker/nginx/libs/utils.lua | 12 ++++++++++++ docker/nginx/libs/utils.spec.lua | 11 +++++++++++ 3 files changed, 25 insertions(+), 10 deletions(-) create mode 100644 docker/nginx/libs/utils.lua create mode 100644 docker/nginx/libs/utils.spec.lua diff --git a/docker/nginx/libs/skynet/account.lua b/docker/nginx/libs/skynet/account.lua index 72a2c9ea..aa8b4414 100644 --- a/docker/nginx/libs/skynet/account.lua +++ b/docker/nginx/libs/skynet/account.lua @@ -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 diff --git a/docker/nginx/libs/utils.lua b/docker/nginx/libs/utils.lua new file mode 100644 index 00000000..b05d0861 --- /dev/null +++ b/docker/nginx/libs/utils.lua @@ -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 \ No newline at end of file diff --git a/docker/nginx/libs/utils.spec.lua b/docker/nginx/libs/utils.spec.lua new file mode 100644 index 00000000..970b279f --- /dev/null +++ b/docker/nginx/libs/utils.spec.lua @@ -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) \ No newline at end of file