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

Promise support in liquidMethodMissing & expressions #285

Closed
wants to merge 5 commits into from

Conversation

almousa1990
Copy link

@almousa1990 almousa1990 commented Dec 17, 2020

adds a promise support in liquidMethodMissing. This should extends Drop capability. Should fix #274

Also, adds a promise support in expressions. Should address #276

This is my first pull request, i ran the tests and all passed.

@almousa1990 almousa1990 changed the title Promise support in liquidMethodMissing Promise support in liquidMethodMissing & expressions Dec 18, 2020
@@ -3,7 +3,7 @@ export abstract class Drop {
return undefined
}

public liquidMethodMissing (key: string): Promise<string | undefined> | string | undefined {
public liquidMethodMissing (key: string): Promise<any | undefined> | any | undefined {
Copy link
Owner

Choose a reason for hiding this comment

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

After changed to any, maybe | undefined is no longer nessessary?

const scope = { 'var': Promise.resolve('var') }
const html = await liquid.parseAndRender(src, scope)
return expect(html).to.equal('success')
})
Copy link
Owner

Choose a reason for hiding this comment

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

It would be even better if you also add a case demonstrating {{ var }} also works.

if (isNil(obj)) return obj
obj = toLiquid(obj)
if (obj instanceof Promise) {
return obj.then(resolvedObj => readProperty(resolvedObj, key))
Copy link
Owner

Choose a reason for hiding this comment

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

I think we need await the obj when calling readProperty rather than inside readProperty, it's passed in by the caller after all.

If we also need to support toLiquid returning a Promise, we should make it explicit by obj = yield toLiquid(obj) and a corresponding test case. (I'm not sure if you want this, it's up to you).

Copy link
Author

Choose a reason for hiding this comment

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

But don't we still need to await inside readProperty if the promise was an object with properties?

Copy link
Owner

Choose a reason for hiding this comment

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

I believe not, readProperty is not recursive and only called from getFromScope. We can yield the obj before pass into the readProperty, like:

readProperty(yield scope, path)

Copy link
Author

Choose a reason for hiding this comment

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

Since i don't see a way to implement these changes without introducing breaking changes, i am going to close this PR.

Appreciated your time

@@ -67,9 +67,12 @@ export class Context {
}
Copy link
Owner

@harttle harttle Dec 18, 2020

Choose a reason for hiding this comment

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

These lines are possibly broken:

https://github.com/harttle/liquidjs/pull/285/files#diff-05ca53b312ab10fd3af29de11c6b119e8a3e13f8fe2ba6c9353977498da1ed41R43-R44

.reduce is executed syncly, and the following case will fail:

const val = await liquid.parseAndRender('{{ foo.bar }}', { foo: Promise.resolve({bar: 'BAR'}) })
expect(var).to.equal('BAR')

I suggest replace the .reduce() with for...of and yield readProperty().

Copy link
Author

Choose a reason for hiding this comment

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

Does it mean that getFromScope has to be generator function? i think this can be a breaking change.\

I tried the above test and it passed.

Copy link
Owner

Choose a reason for hiding this comment

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

It's OK to leave it alone. I'll handle this latter.

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.

liquidMethodMissing doesn't support object promises
2 participants