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

Conditional operands do not seem to evaluate lazily #85

Open
ffried opened this issue Dec 8, 2020 · 0 comments
Open

Conditional operands do not seem to evaluate lazily #85

ffried opened this issue Dec 8, 2020 · 0 comments
Labels
bug Something isn't working

Comments

@ffried
Copy link

ffried commented Dec 8, 2020

Describe the bug

A set of conditional operands as for example if (a || b) seems to evaluate all its operands (a and b) upfront.
This means that b will be evaluated even if a was already true.

To Reproduce

Steps to reproduce the behavior:

  1. Create a leaf file (e.g. page.leaf) with the following contents:
<!DOCTYPE html>
<html>
<body>
#if(!ctx.children || count(ctx.children) <= 0):
<ul>#for(child in ctx.children):<li>#(child)</li>#endfor</ul>
#else:
<p>No children.</p>
#endif
</body>
</html>
  1. Render the page with the following context:
struct Context: Encodable { let ctx: [String]? }
func configure(app: Application) {
    app.get(use: { $0.view.render("page", Context(ctx: nil)) })
}
  1. Rendering fails with "Unable to convert count parameter to LeafData collection" as thrown by the Count tag if the parameter is no collection. In this case, a breakpoint in Count prints the following for the first parameter (given that ctx.children is nil):
(lldb) po ctx.parameters
▿ 1 element
  ▿ 0 : void()?
    ▿ storage : void()?
      ▿ optional : 2 elements
        - .0 : nil
        - .1 : LeafKit.LeafData.NaturalType.void

Expected behavior

The page renders fine, showing "No children." with the following rendered HTML:

<!DOCTYPE html>
<html>
<body>
<p>No children.</p>
</body>
</html>

Environment

  • Vapor Framework version: 4.36.0
  • Leaf Framework version: 4.0.1
  • LeafKit Framework version: 1.0.0
  • Vapor Toolbox version: n/a
  • OS version: macOS 11.0.1 as well as Ubuntu 18.04
  • Swift version: 5.3.1

Additional context

I tried to dig a bit into LeafKit's source, and am pretty sure that the evaluation of all conditional operands is the problem. However, I could also be missing something, in which case my "attempted diagnosis" might be incorrect and the issue is actually something different.

@ffried ffried added the bug Something isn't working label Dec 8, 2020
ffried added a commit to sersoft-gmbh/route-docs that referenced this issue Dec 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

1 participant