-
Notifications
You must be signed in to change notification settings - Fork 170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Liquid context debugging policy #849
Conversation
table = true | ||
} | ||
|
||
local function value_from(object) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cyclomatic complexity of function 'value_from' is too high (8 > 7)
return self | ||
end | ||
|
||
function _M:content(context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
unused argument 'self'
bae9cce
to
7058761
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm quite worried about the cjson sparse array change.
It looks like it is global setting for any cjson serialization, so it either should be set globally by APIcast or figure out some workaround.
If it is "raise exception" or "serialize them as string" then I think it is safe to use this feature, but it should be set by APIcast init process itself.
} | ||
|
||
local function value_from(object) | ||
if type(object) == 'string' or type(object) == 'number' then |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could be:
local typ = type(object)
if accepted_types_for_keys[t] then return object end
right?
|
||
if type(object) ~= 'table' then return nil end | ||
|
||
local res = {} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this can be a helper function value_from_table
. That would reduce cyclomatic complexity.
Building on top of the previous comment we can have a dispatch table:
local ident = function(...) return ... end
local value_from_fun = {
string = ident, number = ident,
table = function(table)
local res = {}
for k, v in pairs(table) do
local wanted_types = accepted_types_for_keys[type(k)] and
accepted_types_for_values[type(v)]
if wanted_types then
res[k] = value_from(v)
end
end
return res
end
And value_from
would just do dispatch:
local function value_from(object)
local fun = value_from_fun[type(object)]
if fun then return fun(object) end
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks a bit better 👍
if type(object) ~= 'table' then return nil end | ||
|
||
-- The context is a list where each element has a "current" and a "next". | ||
local current = object.current |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In theory we could add __pairs
metamethod to the linked list that understands this.
http://lua-users.org/wiki/GeneralizedPairsAndIpairs
Then you could just iterate through the context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That's a good idea, but I'd rather leave it for a future PR as it involves adding the functionality in the LinkedList
module, test it, and check whether we can use it in some of the existing callers.
-- With this call we just convert that number into a string. Not a big deal | ||
-- since nobody is going to use those fields from the lrucache instance in | ||
-- liquid. | ||
cjson.encode_sparse_array(true) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is global setting, right? That is quite dangerous. Maybe we should enable this in init for everyone.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should always encode table keys as strings when building the context content ? But that would probably break actual arrays.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I didn't notice. It wasn't my intention to set this global setting there.
I'll just probably implement some ad-hoc solution in ContextContent
. If the key is an integer and is greater than X then convert it to string.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here's how I implemented this: https://github.com/3scale/apicast/pull/849/files#diff-d126ed9fb83c6261c4bb9fefa80fd029R22
7058761
to
28bf4cd
Compare
end | ||
|
||
function _M.content(_, context) | ||
local content = context_content.from(context) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We also should add the ngx_variable, right?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're right. And to do that I needed to modify ngx_variable
so it behaves as a context (linked list): 822345d
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, the policy takes that into account now: 98d3d8a
So it's the same object as the policies context and we can call the same methods.
28bf4cd
to
e7798f1
Compare
-- we're going to implement an add-hoc solution here so we don't affect the | ||
-- other modules. We're going to convert those large ints to strings as a | ||
-- workaround. | ||
local max_integer_key = 10 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think arrays with 10 elements are common. Maybe better to set this to a something like 1000 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK. I'll raise the limit to 1k. I think that in practice large service IDs are going to be the problem.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good 👍
A module to extract the content of a context.
e7798f1
to
b8965b9
Compare
Closes #838