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

describe module inheritance #362

Merged
merged 4 commits into from
May 11, 2017
Merged

describe module inheritance #362

merged 4 commits into from
May 11, 2017

Conversation

mikz
Copy link
Contributor

@mikz mikz commented May 5, 2017

closes #308

  • improve documentation in the example
  • provide whole module (blacklist.lua)
  • test that example module

and polish a bit balancer interface, so we can easily modify it from modules

@octobot
Copy link

octobot commented May 5, 2017

Spell Checker found issues

examples/custom-module/README.md

Line Typo
3 executed in each nginx phase: init, init_worker, rewrite, access
3 ed in each nginx phase: init, init_worker, rewrite, access, cont
3 n each nginx phase: init, init**_worker,** rewrite, access, content, lo
3 te, access, content, log, post**_action,** balancer, header_filter, bod
3 post_action, balancer, header**_filter,** body_filter etc.
3 balancer, header**_filter**, body**_filter** etc.
35 need some clever use of metatables. Here is a recommended skelet

Generated by 🚫 Danger

@mikz mikz force-pushed the custom-blacklist-module branch from d3fee52 to ff7df98 Compare May 5, 2017 16:13
@mikz mikz requested a review from davidor May 5, 2017 16:13
@mikz mikz added the B-current label May 5, 2017
@mikz mikz requested a review from mayorova May 5, 2017 16:31
-- load and initialize the parent module
local apicast = require('apicast').new()

-- this is used in User-Agent
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm missing something. I don't see how user-agent is related to this module.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Well the used module _NAME field is going to get used in User-Agent header from APIcast connecting to backend.

There is example module of IP blacklist in [`blacklist.lua`](blacklist.lua).

To honour the module inheritance, but still be able to override some methods from the `apicast` module, you'll
need some clever use of metatables. Here a recommended skeleton of the module inheritance:
Copy link
Contributor

Choose a reason for hiding this comment

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

typo: here's a recommended...

return setmetatable({}, mt)
end

function _M.log()
Copy link
Contributor

Choose a reason for hiding this comment

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

To me, this seems to indicate that defining log() is required, which is not the case. I would specify somewhere in this file that the purpose of this module is to log some things that apicast by itself does not, and that's why we need to override log().

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Would a comment be enough?

Copy link
Contributor

Choose a reason for hiding this comment

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

yes

@mikz mikz force-pushed the custom-blacklist-module branch 2 times, most recently from 7b6f6b3 to f0f7b13 Compare May 10, 2017 07:25
-- define a table, that is going to be this module metatable
-- if your table does not define a property, __index is going to get used
-- and so on until there are no metatables to check
-- so in this case the inheritance works like mod -> _M -> apicast
Copy link
Contributor

Choose a reason for hiding this comment

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

what is mod?

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 would be instance of the module. Not really sure how to write it.

Copy link
Contributor Author

@mikz mikz May 11, 2017

Choose a reason for hiding this comment

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

Would something like _M.new() -> _M -> apicast work for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, much clearer now :)

How about:

so in this case the inheritance works like this: local instance created with _M.new() -> _M -> apicast

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Perfect. Fixed.

@mayorova
Copy link
Contributor

Nice explanation! Just one thing to clarify, in a comment above...

@mikz mikz force-pushed the custom-blacklist-module branch from c2ee8b5 to 6b03a73 Compare May 11, 2017 06:50
@mikz mikz merged commit 595f51b into master May 11, 2017
@mikz mikz deleted the custom-blacklist-module branch May 11, 2017 07:16
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.

Describe module inheritance
4 participants