-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from Kong/feat/update-to-new-db-and-pdk
feat(serverless-functions) update to new db and pdk
- Loading branch information
Showing
7 changed files
with
183 additions
and
183 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,29 @@ | ||
local Errors = require "kong.dao.errors" | ||
local typedefs = require "kong.db.schema.typedefs" | ||
|
||
|
||
local function check_functions(value) | ||
for i, entry in ipairs(value) do | ||
local _, err = loadstring(entry) | ||
if err then | ||
return false, Errors.schema("Error parsing post-function #" .. i .. ": " .. err) | ||
end | ||
local function validate_function(fun) | ||
local _, err = loadstring(fun) | ||
if err then | ||
return false, "Error parsing post-function: " .. err | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
return { | ||
no_consumer = true, | ||
api_only = true, | ||
|
||
name = "post-function", | ||
fields = { | ||
functions = { | ||
required = true, | ||
type = "array", | ||
} | ||
{ consumer = typedefs.no_consumer }, | ||
{ config = { | ||
type = "record", | ||
fields = { | ||
{ functions = { | ||
required = true, type = "array", | ||
elements = { type = "string", custom_validator = validate_function }, | ||
}, }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
self_check = function(schema, config, dao, is_updating) | ||
local _, err = check_functions(config.functions) | ||
if err then | ||
return false, err | ||
end | ||
|
||
return true | ||
end, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,35 +1,29 @@ | ||
local Errors = require "kong.dao.errors" | ||
local typedefs = require "kong.db.schema.typedefs" | ||
|
||
|
||
local function check_functions(value) | ||
for i, entry in ipairs(value) do | ||
local _, err = loadstring(entry) | ||
if err then | ||
return false, Errors.schema("Error parsing pre-function #" .. i .. ": " .. err) | ||
end | ||
local function validate_function(fun) | ||
local _, err = loadstring(fun) | ||
if err then | ||
return false, "Error parsing pre-function: " .. err | ||
end | ||
|
||
return true | ||
end | ||
|
||
|
||
return { | ||
no_consumer = true, | ||
api_only = true, | ||
|
||
name = "pre-function", | ||
fields = { | ||
functions = { | ||
required = true, | ||
type = "array", | ||
} | ||
{ consumer = typedefs.no_consumer }, | ||
{ config = { | ||
type = "record", | ||
fields = { | ||
{ functions = { | ||
required = true, type = "array", | ||
elements = { type = "string", custom_validator = validate_function }, | ||
}, }, | ||
}, | ||
}, | ||
}, | ||
}, | ||
|
||
self_check = function(schema, config, dao, is_updating) | ||
local _, err = check_functions(config.functions) | ||
if err then | ||
return false, err | ||
end | ||
|
||
return true | ||
end, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,38 @@ | ||
local validate_entity = require("kong.dao.schemas_validation").validate_entity | ||
local pre_schema = require "kong.plugins.pre-function.schema" | ||
local v = require("spec.helpers").validate_plugin_config_schema | ||
|
||
local mock_fn_one = 'print("hello world!")' | ||
local mock_fn_two = 'local x = 1' | ||
local mock_fn_invalid = 'print(' | ||
|
||
describe("pre-function schema", function() | ||
it("validates single function", function() | ||
local ok, err = validate_entity({ functions = { mock_fn_one } }, pre_schema) | ||
local ok, err = v({ functions = { mock_fn_one } }, pre_schema) | ||
|
||
assert.True(ok) | ||
assert.is_nil(err) | ||
assert.truthy(ok) | ||
assert.falsy(err) | ||
end) | ||
|
||
it("validates multiple functions", function() | ||
local ok, err = validate_entity({ functions = { mock_fn_one, mock_fn_two } }, pre_schema) | ||
local ok, err = v({ functions = { mock_fn_one, mock_fn_two } }, pre_schema) | ||
|
||
assert.True(ok) | ||
assert.is_nil(err) | ||
assert.truthy(ok) | ||
assert.falsy(err) | ||
end) | ||
|
||
describe("errors", function() | ||
it("with an invalid function", function() | ||
local ok, _, err = validate_entity({ functions = { mock_fn_invalid } }, pre_schema) | ||
local ok, err = v({ functions = { mock_fn_invalid } }, pre_schema) | ||
|
||
assert.False(ok) | ||
assert.equals("Error parsing pre-function #1: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.message) | ||
assert.falsy(ok) | ||
assert.equals("Error parsing pre-function: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.config.functions) | ||
end) | ||
|
||
it("with a valid and invalid function", function() | ||
local ok, _, err = validate_entity({ functions = { mock_fn_one, mock_fn_invalid } }, pre_schema) | ||
local ok, err = v({ functions = { mock_fn_one, mock_fn_invalid } }, pre_schema) | ||
|
||
assert.False(ok) | ||
assert.equals("Error parsing pre-function #2: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.message) | ||
assert.falsy(ok) | ||
assert.equals("Error parsing pre-function: [string \"print(\"]:1: unexpected symbol near '<eof>'", err.config.functions) | ||
end) | ||
end) | ||
end) |
Oops, something went wrong.