Skip to content

Commit

Permalink
Merge pull request #1569 from Mashape/fix/conf-parsing
Browse files Browse the repository at this point in the history
fix(conf) overcome penlight's 'list_delim' setting
  • Loading branch information
thibaultcha authored Aug 30, 2016
2 parents 8431fcc + 1ae98b5 commit 185fc64
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
20 changes: 12 additions & 8 deletions kong/conf_loader.lua
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ local function check_and_infer(conf)
local v_schema = CONF_INFERENCES[k] or {}
local typ = v_schema.typ

if type(value) == "string" then
value = string.gsub(value, "#.-$", "") -- remove trailing comment if any
value = pl_stringx.strip(value)
end

-- transform {boolean} values ("on"/"off" aliasing to true/false)
-- transform {ngx_boolean} values ("on"/"off" aliasing to on/off)
-- transform {explicit string} values (number values converted to strings)
Expand All @@ -135,13 +140,9 @@ local function check_and_infer(conf)
value = setmetatable(pl_stringx.split(value, ","), nil) -- remove List mt
end

if type(value) == "string" then
-- default type is string, and an empty if unset
value = value ~= "" and tostring(value) or nil
if value then
value = string.gsub(value, "#.-$", "")
value = pl_stringx.strip(value)
end
if value == "" then
-- unset values are removed
value = nil
end

typ = typ or "string"
Expand Down Expand Up @@ -279,7 +280,10 @@ local function load(path, custom_conf)

log.verbose("reading config file at %s", path)
local s = pl_stringio.open(f)
from_file_conf, err = pl_config.read(s)
from_file_conf, err = pl_config.read(s, {
smart = false,
list_delim = "_blank_" -- mandatory but we want to ignore it
})
s:close()
if not from_file_conf then return nil, err end
end
Expand Down
6 changes: 6 additions & 0 deletions spec/01-unit/02-conf_loader_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ describe("Configuration loader", function()
assert.equal("cassandra", conf.database)
assert.equal("debug", conf.log_level)
end)
it("overcomes penlight's list_delim option", function()
local conf = assert(conf_loader("spec/fixtures/to-strip.conf"))
assert.False(conf.dnsmasq)
assert.True(conf.plugins.foobar)
assert.True(conf.plugins["hello-world"])
end)

describe("inferences", function()
it("infer booleans (on/off/true/false strings)", function()
Expand Down
14 changes: 14 additions & 0 deletions spec/fixtures/to-strip.conf
Original file line number Diff line number Diff line change
@@ -1,3 +1,17 @@
database = cassandra # strip me
log_level = debug # strip this
# comment too
dnsmasq = off # Toggles if Kong should start/stop dnsmasq,
# which can be used as the Nginx DNS resolver.
# Using dnsmasq allows Nginx to resolve
# domains defined in /etc/hosts.
# dnsmasq must be installed and available in
# your $PATH.
dns_resolver = 8.8.8.8

custom_plugins = foobar,hello-world # Comma-separated list of additional plugins
# this node should load.
# Use this property to load custom plugins
# that are not bundled with Kong.
# Plugins will be loaded from the
# `kong.plugins.{name}.*` namespace.

0 comments on commit 185fc64

Please sign in to comment.