From 76018840d3081cadf3d4220464f7b005d8e0861c Mon Sep 17 00:00:00 2001 From: Michal Cichra Date: Tue, 4 Jul 2017 15:52:03 +0200 Subject: [PATCH] extract lua-resty-env --- CHANGELOG.md | 1 + apicast/apicast-0.1-0.rockspec | 1 + apicast/src/resty/env.lua | 91 ---------------------------------- spec/resty/env_spec.lua | 58 ---------------------- 4 files changed, 2 insertions(+), 149 deletions(-) delete mode 100644 apicast/src/resty/env.lua delete mode 100644 spec/resty/env_spec.lua diff --git a/CHANGELOG.md b/CHANGELOG.md index 105a1b823..0a52e6f52 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/). - APIcast module `balancer` method now accepts optional balancer [PR #362](https://github.com/3scale/apicast/pull/362) - Extracted lua-resty-url [PR #384](https://github.com/3scale/apicast/pull/384) +- Extracted lua-resty-env [PR #386](https://github.com/3scale/apicast/pull/386) ### Added diff --git a/apicast/apicast-0.1-0.rockspec b/apicast/apicast-0.1-0.rockspec index 6a10b684c..2a4fb55ee 100644 --- a/apicast/apicast-0.1-0.rockspec +++ b/apicast/apicast-0.1-0.rockspec @@ -7,6 +7,7 @@ dependencies = { 'router == 2.1-0', 'lua-resty-jwt == 0.1.10-1', 'lua-resty-url == 0.1.0-1', + 'lua-resty-env == 0.3.0-1', } build = { type = "builtin", diff --git a/apicast/src/resty/env.lua b/apicast/src/resty/env.lua deleted file mode 100644 index aab2ac476..000000000 --- a/apicast/src/resty/env.lua +++ /dev/null @@ -1,91 +0,0 @@ ------------- --- Resty ENV --- OpenResty module for working with ENV variables. --- --- @module resty.env --- @author mikz --- @license Apache License Version 2.0 - -local _M = { - _VERSION = '0.1' -} - -local getenv = os.getenv - -local cached = {} - -local function fetch(name) - local value - - if cached[name] then - value = _M.env[name] - else - value = getenv(name) - - ngx.log(ngx.DEBUG, 'env: ', name, ' = ', value) - _M.env[name] = value - - cached[name] = true - end - - return value -end - ---- Return the raw value from ENV. Uses local cache. --- @tparam string name name of the environment variable -function _M.get(name) - return _M.env[name] or fetch(name) -end - -local value_mapping = { - [''] = false -} - ---- Return value from ENV. ---- Returns false if it is empty. Uses @{get} internally. --- @tparam string name name of the environment variable -function _M.value(name) - local value = _M.get(name) - local mapped = value_mapping[value] - - if mapped == nil then - return value - else - return mapped - end -end - -local env_mapping = { - ['true'] = true, - ['false'] = false, - ['1'] = true, - ['0'] = false, - [''] = false -} - ---- Returns true/false from ENV variable. ---- Converts 0 to false and 1 to true. --- @tparam string name name of the environment variable -function _M.enabled(name) - return env_mapping[_M.get(name)] -end - ---- Sets value to the local cache. --- @tparam string name name of the environment variable --- @tparam string value value to be cached --- @see resty.env.get -function _M.set(name, value) - local env = _M.env - local previous = env[name] - env[name] = value - return previous -end - ---- Reset local cache. -function _M.reset() - _M.env = {} - cached = {} - return _M -end - -return _M.reset() diff --git a/spec/resty/env_spec.lua b/spec/resty/env_spec.lua deleted file mode 100644 index 9faabc4f2..000000000 --- a/spec/resty/env_spec.lua +++ /dev/null @@ -1,58 +0,0 @@ -local _M = require 'resty.env' - -describe('env', function() - local env - before_each(function() env = _M.env end) - after_each(function() _M.env = env end) - - describe('.get', function() - - local path = os.getenv("PATH") - - it('returns contents of the ENV variable', function() - assert.equal(path, _M.get('PATH')) - end) - - it('caches the result', function() - _M.get('PATH') - - assert.equal(path, _M.env.PATH) - end) - - it('reads from the cache first', function() - _M.env = { ['SOME_MISSING_ENV_VAR'] = 'somevalue' } - - assert.equal('somevalue', _M.get("SOME_MISSING_ENV_VAR")) - end) - end) - - describe('.value', function() - it('returns false instead of empty value', function() - _M.set('KEY', '') - assert.equal(false, _M.value('KEY')) - end) - - it('returns the value if not emptu', function() - _M.set('KEY', 'value') - assert.equal('value', _M.value('KEY')) - end) - end) - - describe('.set', function() - it('saves value to the cache', function() - _M.set('SOME_MISSING_KEY', 'val') - - assert.equal('val', _M.env.SOME_MISSING_KEY) - end) - end) - - describe('.reset', function() - it('cleans the cache', function() - _M.env.SOMETHING_EMPTY = 'foo' - - _M.reset() - - assert.same(nil, _M.env.SOMETHING_EMPTY) - end) - end) -end)