Skip to content
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

Merged
merged 8 commits into from
Aug 27, 2018
Merged

Liquid context debugging policy #849

merged 8 commits into from
Aug 27, 2018

Conversation

davidor
Copy link
Contributor

@davidor davidor commented Aug 22, 2018

Closes #838

@davidor davidor requested a review from a team as a code owner August 22, 2018 16:59
table = true
}

local function value_from(object)
Copy link

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)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused argument 'self'

@davidor davidor force-pushed the liquid-context-policy branch 2 times, most recently from bae9cce to 7058761 Compare August 22, 2018 18:58
@davidor davidor changed the title [WIP] Liquid context debugging policy Liquid context debugging policy Aug 22, 2018
@davidor davidor requested a review from mikz August 23, 2018 07:51
Copy link
Contributor

@mikz mikz left a 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
Copy link
Contributor

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 = {}
Copy link
Contributor

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

Copy link
Contributor Author

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
Copy link
Contributor

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.

Copy link
Contributor Author

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)
Copy link
Contributor

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@davidor davidor force-pushed the liquid-context-policy branch from 7058761 to 28bf4cd Compare August 23, 2018 14:19
end

function _M.content(_, context)
local content = context_content.from(context)
Copy link
Contributor

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?

Copy link
Contributor Author

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

Copy link
Contributor Author

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.
@davidor davidor force-pushed the liquid-context-policy branch from 28bf4cd to e7798f1 Compare August 27, 2018 09:46
@davidor davidor requested a review from mikz August 27, 2018 11:00
-- 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
Copy link
Contributor

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 ?

Copy link
Contributor Author

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.

Copy link
Contributor

@mikz mikz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good 👍

@davidor davidor force-pushed the liquid-context-policy branch from e7798f1 to b8965b9 Compare August 27, 2018 16:13
@davidor davidor merged commit c3ec9d1 into master Aug 27, 2018
@davidor davidor deleted the liquid-context-policy branch August 27, 2018 16:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants