Skip to content

Commit

Permalink
Merge branch 'refactor/nuke-constants' into next
Browse files Browse the repository at this point in the history
  • Loading branch information
thibaultcha committed Mar 25, 2016
2 parents 0c1678d + c4f4d77 commit cbd03da
Show file tree
Hide file tree
Showing 73 changed files with 417 additions and 608 deletions.
24 changes: 0 additions & 24 deletions .luacheckrc

This file was deleted.

15 changes: 8 additions & 7 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,14 @@ doc:
@ldoc -c config.ld kong

lint:
@find kong spec -name '*.lua' -not -name 'invalid-module.lua' -not -path 'kong/vendor/*' | xargs luacheck -q
@luacheck -q . \
--exclude-files 'kong/vendor/**/*.lua' \
--exclude-files 'spec/unit/fixtures/invalid-module.lua' \
--std 'ngx_lua+busted' \
--globals '_KONG' \
--globals 'ngx' \
--no-redefined \
--no-unused-args

test:
@busted -v spec/unit
Expand All @@ -50,12 +57,6 @@ test-plugins:
test-all:
@busted -v spec/

test-model:
@busted -v -o gtest spec/integration/model

test-model-only:
@busted -v -o gtest --tags=only spec/integration/model

coverage:
@rm -f luacov.*
@busted --coverage spec/
Expand Down
5 changes: 2 additions & 3 deletions bin/kong
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@
-- This script is not parsed by lapp due to limitations of the said framework as it
-- is currently implemented.

local luarocks = require "kong.cli.utils.luarocks"
local infos = luarocks.get_kong_infos()
local meta = require "kong.meta"
local commands = {
db = "kong.cli.cmds.db",
stop = "kong.cli.cmds.stop",
Expand All @@ -36,7 +35,7 @@ Usage: kong <command>
kong --help print this message
kong <command> --help print the help message of a command

%s@%s]], infos.name, infos.version)
%s@%s]], meta._NAME, meta._VERSION)

-- Determine validity of the given command
local cmd = arg[1]
Expand Down
33 changes: 12 additions & 21 deletions kong-0.7.0-0.rockspec
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ description = {
dependencies = {
"luasec ~> 0.5-2",

"penlight ~> 1.3.2",
"lua_uuid ~> 0.2.0-2",
"lua_system_constants ~> 0.1.1-0",
"luatz ~> 0.3-1",
Expand Down Expand Up @@ -44,11 +45,11 @@ build = {
["lapp"] = "kong/vendor/lapp.lua",
["resty_http"] = "kong/vendor/resty_http.lua",

["kong.meta"] = "kong/meta.lua",
["kong.constants"] = "kong/constants.lua",
["kong.singletons"] = "kong/singletons.lua",

["kong.cli.utils.logger"] = "kong/cli/utils/logger.lua",
["kong.cli.utils.luarocks"] = "kong/cli/utils/luarocks.lua",
["kong.cli.utils.ssl"] = "kong/cli/utils/ssl.lua",
["kong.cli.utils.input"] = "kong/cli/utils/input.lua",
["kong.cli.utils.services"] = "kong/cli/utils/services.lua",
Expand All @@ -67,6 +68,16 @@ build = {
["kong.cli.services.serf"] = "kong/cli/services/serf.lua",
["kong.cli.services.nginx"] = "kong/cli/services/nginx.lua",

["kong.api.app"] = "kong/api/app.lua",
["kong.api.api_helpers"] = "kong/api/api_helpers.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.lua",

["kong.tools.io"] = "kong/tools/io.lua",
["kong.tools.utils"] = "kong/tools/utils.lua",
["kong.tools.faker"] = "kong/tools/faker.lua",
Expand Down Expand Up @@ -107,15 +118,6 @@ build = {
["kong.dao.migrations.cassandra"] = "kong/dao/migrations/cassandra.lua",
["kong.dao.migrations.postgres"] = "kong/dao/migrations/postgres.lua",

["kong.api.app"] = "kong/api/app.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.route_helpers"] = "kong/api/route_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",

["kong.plugins.base_plugin"] = "kong/plugins/base_plugin.lua",

["kong.plugins.basic-auth.migrations.cassandra"] = "kong/plugins/basic-auth/migrations/cassandra.lua",
Expand Down Expand Up @@ -219,16 +221,6 @@ build = {
["kong.plugins.acl.api"] = "kong/plugins/acl/api.lua",
["kong.plugins.acl.daos"] = "kong/plugins/acl/daos.lua",

["kong.api.app"] = "kong/api/app.lua",
["kong.api.crud_helpers"] = "kong/api/crud_helpers.lua",
["kong.api.route_helpers"] = "kong/api/route_helpers.lua",
["kong.api.routes.kong"] = "kong/api/routes/kong.lua",
["kong.api.routes.apis"] = "kong/api/routes/apis.lua",
["kong.api.routes.consumers"] = "kong/api/routes/consumers.lua",
["kong.api.routes.plugins"] = "kong/api/routes/plugins.lua",
["kong.api.routes.cache"] = "kong/api/routes/cache.lua",
["kong.api.routes.cluster"] = "kong/api/routes/cluster.lua",

["kong.plugins.jwt.migrations.cassandra"] = "kong/plugins/jwt/migrations/cassandra.lua",
["kong.plugins.jwt.migrations.postgres"] = "kong/plugins/jwt/migrations/postgres.lua",
["kong.plugins.jwt.handler"] = "kong/plugins/jwt/handler.lua",
Expand Down Expand Up @@ -256,7 +248,6 @@ build = {
["kong.plugins.datadog.handler"] = "kong/plugins/datadog/handler.lua",
["kong.plugins.datadog.schema"] = "kong/plugins/datadog/schema.lua",
["kong.plugins.datadog.statsd_logger"] = "kong/plugins/datadog/statsd_logger.lua"

},
install = {
conf = { "kong.yml" },
Expand Down
63 changes: 63 additions & 0 deletions kong/api/api_helpers.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
local pl_string = require "pl.stringx"
local utils = require "kong.tools.utils"

local type = type
local pairs = pairs
local remove = table.remove

local _M = {}

-- Parses a form value, handling multipart/data values
-- @param `v` The value object
-- @return The parsed value
local function parse_value(v)
return type(v) == "table" and v.content or v -- Handle multipart
end

-- Put nested keys in objects:
-- Normalize dotted keys in objects.
-- Example: {["key.value.sub"]=1234} becomes {key = {value = {sub=1234}}
-- @param `obj` Object to normalize
-- @return `normalized_object`
function _M.normalize_nested_params(obj)
local new_obj = {}

local function attach_dotted_key(keys, attach_to, value)
local current_key = keys[1]

if #keys > 1 then
if not attach_to[current_key] then
attach_to[current_key] = {}
end
remove(keys, 1)
attach_dotted_key(keys, attach_to[current_key], value)
else
attach_to[current_key] = value
end
end

for k, v in pairs(obj) do
if type(v) == "table" then
-- normalize arrays since Lapis parses ?key[1]=foo as {["1"]="foo"} instead of {"foo"}
if utils.is_array(v) then
local arr = {}
for _, arr_v in pairs(v) do arr[#arr+1] = arr_v end
v = arr
else
v = _M.normalize_nested_params(v) -- recursive call on other table values
end
end

-- normalize sub-keys with dot notation
local keys = pl_string.split(k, ".")
if #keys > 1 then -- we have a key containing a dot
attach_dotted_key(keys, new_obj, parse_value(v))
else
new_obj[k] = parse_value(v) -- nothing special with that key, simply attaching the value
end
end

return new_obj
end

return _M
77 changes: 13 additions & 64 deletions kong/api/app.lua
Original file line number Diff line number Diff line change
@@ -1,62 +1,26 @@
local singletons = require "kong.singletons"
local lapis = require "lapis"
local utils = require "kong.tools.utils"
local stringy = require "stringy"
local responses = require "kong.tools.responses"
local singletons = require "kong.singletons"
local app_helpers = require "lapis.application"
local app = lapis.Application()

-- Parses a form value, handling multipart/data values
-- @param `v` The value object
-- @return The parsed value
local function parse_value(v)
return type(v) == "table" and v.content or v -- Handle multipart
end
local api_helpers = require "kong.api.api_helpers"

-- Put nested keys in objects:
-- Normalize dotted keys in objects.
-- Example: {["key.value.sub"]=1234} becomes {key = {value = {sub=1234}}
-- @param `obj` Object to normalize
-- @return `normalized_object`
local function normalize_nested_params(obj)
local new_obj = {}
local find = string.find

local function attach_dotted_key(keys, attach_to, value)
local current_key = keys[1]

if #keys > 1 then
if not attach_to[current_key] then
attach_to[current_key] = {}
end
table.remove(keys, 1)
attach_dotted_key(keys, attach_to[current_key], value)
else
attach_to[current_key] = value
end
end
local app = lapis.Application()

for k, v in pairs(obj) do
if type(v) == "table" then
-- normalize arrays since Lapis parses ?key[1]=foo as {["1"]="foo"} instead of {"foo"}
if utils.is_array(v) then
local arr = {}
for _, arr_v in pairs(v) do table.insert(arr, arr_v) end
v = arr
else
v = normalize_nested_params(v) -- recursive call on other table values
local function parse_params(fn)
return app_helpers.json_params(function(self, ...)
local content_type = self.req.headers["content-type"]
if content_type and find(content_type:lower(), "application/json", nil, true) then
if not self.json then
return responses.send_HTTP_BAD_REQUEST("Cannot parse JSON body")
end
end

-- normalize sub-keys with dot notation
local keys = stringy.split(k, ".")
if #keys > 1 then -- we have a key containing a dot
attach_dotted_key(keys, new_obj, parse_value(v))
else
new_obj[k] = parse_value(v) -- nothing special with that key, simply attaching the value
end
end

return new_obj
self.params = api_helpers.normalize_nested_params(self.params)
return fn(self, ...)
end)
end

local function on_error(self)
Expand All @@ -74,21 +38,6 @@ local function on_error(self)
end
end

local function parse_params(fn)
return app_helpers.json_params(function(self, ...)
local content_type = self.req.headers["content-type"]
if content_type and string.find(content_type:lower(), "application/json", nil, true) then
if not self.json then
return responses.send_HTTP_BAD_REQUEST("Cannot parse JSON body")
end
end
self.params = normalize_nested_params(self.params)
return fn(self, ...)
end)
end

app.parse_params = parse_params

app.default_route = function(self)
local path = self.req.parsed_url.path:match("^(.*)/$")

Expand Down
29 changes: 0 additions & 29 deletions kong/api/route_helpers.lua

This file was deleted.

Loading

0 comments on commit cbd03da

Please sign in to comment.