Skip to content
This repository was archived by the owner on May 1, 2020. It is now read-only.

Fix programmatic method invocation.#9

Merged
eahefnawy merged 3 commits into
serverless:masterfrom
Pavel910:master
Oct 24, 2019
Merged

Fix programmatic method invocation.#9
eahefnawy merged 3 commits into
serverless:masterfrom
Pavel910:master

Conversation

@Pavel910
Copy link
Copy Markdown
Contributor

@Pavel910 Pavel910 commented Oct 23, 2019

@eahefnawy this PR contains a small fix that was breaking the programmatic invocation of custom methods on the @serverless/template component. CLI usage however was working normally.

I also added a README file with some examples as I see a lot of people struggling with programmatic usage of this component.

This has been tested manually for both CLI and programmatic usage, all the cases I could think of.

Comment thread serverless.js
return new Proxy(defaultFunction, {
get: (obj, prop) => {
// This handles the weird case when `then` is called on the `defaultFunction`
if (prop === 'then') {
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is this happening? I would suspect that properties from a promise are bleeding through here. However guarding against then does not guarantee that other things may also cause trouble. I suggest that createCustomMethodHandler should be renamed tryCreateCustomMethodHandler and if a component cannot be found, instead of creating an error, rather just return obj[prop]

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately that is not possible, since you're passing template path at method invocation and not Template component construction.

Comment thread utils.js
`Attempting to run method "${method}" on template aliases: ${components.join(', ')}`
)

// Make sure the template is an object
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is it important to make sure the template is an object? Can this be explained in this comment?

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

On line 349 above, is it prudent to throw an Error? Perhaps if there was a proper testing framework that would guarantee the behavior of this, however since there is no framework and this is a core part of serverless components, I suggest not throwing an error.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

template input of Template component can accept both string (path to file) and an object, which you can prepare in advance and simply pass for execution.

@barrysteyn
Copy link
Copy Markdown

barrysteyn commented Oct 23, 2019

@eahefnawy This is fixing a breaking change, so it is super important to be addressed. @Pavel910
Can I make the following comments:

  1. Throwing an error in createCustomMethod may be appropriate, but without any testing, changes like this can (and in fact, this change does) cause unintended consequences.
  2. Perhaps unit/integration tests on the core libraries. Not saying that things need to be tested vigorously but it may be a good idea (in fact, I will open an issue for this).
  3. Thanks very much for the README. It is super helpful.

EDIT: Here is issue for testing https://github.com/serverless/components/issues/511

@Pavel910
Copy link
Copy Markdown
Contributor Author

@barrysteyn throwing there is ok because there is nothing else that can be executed if there is no custom method (read my comment in the review section).

To make this component better overall, the template path should be passed to constructor, and not to methods themselves (then your proposed solution with trying would work nicely), but then it is a major change that also involves updates to CLI. That's something @eahefnawy must allow us to do :)

@barrysteyn
Copy link
Copy Markdown

To make this component better overall, the template path should be passed to constructor, and not to methods themselves (then your proposed solution with trying would work nicely), but then it is a major change that also involves updates to CLI. That's something @eahefnawy must allow us to do :)

It seems like this may be the way to go. However such a big change may need to wait for testing (sorry I am going on about testing, but it seems tough to make any change to a core library without testing).

@Pavel910
Copy link
Copy Markdown
Contributor Author

@barrysteyn Agreed, it would be nice to have at least basic tests in place.

@eahefnawy
Copy link
Copy Markdown
Contributor

Thanks folks. Testing is absolutely planned. We just didn't 100% settle on the API yet and currently taking feedback. PR like these are exactly the feedback that we need.

LG2M 😊

@eahefnawy eahefnawy merged commit 4d52f4f into serverless:master Oct 24, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants