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

Silence some warnings in escaping #58

Closed
wants to merge 1 commit into from

Conversation

jonathanstowe
Copy link

If the object passed to the template has undefined keys (or methods that return Nil,) then there were warnings produced from the escaping. For an Associative this could potentially be worked around at source, but for an object that has methods that may return Nil ( say an ORM row object,) it's a bit less tractable.

If the object passed to the template has undefined keys (or methods that
return Nil,) then there were warnings produced from the escaping. For an
Associative this could potentially be worked around at source, but for
an object that has methods that may return Nil ( say an ORM row object,)
it's a bit less tractable.
@Altai-man
Copy link
Member

Isn't this PR hide potential bugs instead of helping to reveal them in the caller's code?

@jonathanstowe
Copy link
Author

jonathanstowe commented Oct 27, 2021

Well it might, but unfortunately there is no way of distinguishing between deliberate and expected behaviour and bug in this case, if, for instance, one has an object with attributes that are optionally defined then one doesn't particularly want to be warned about it, and changing the interface of the object, which may already be used happily in other places, in order to quiet warnings here is not really ideal ( or even possible.)

If there is a bug in the thing that is generating the data passed to the template then that can and should be determined by other means, the template module should just assume that the caller knows what they are doing in this case (all other things being equal.)

@Altai-man
Copy link
Member

DISCLAIMER: To be fair, I completely agree with you the way Cro gives us tons of warnings now is not natural. More so, the warnings are useless, it doesn't tell you a thing like "In your template X at line Y caret Z you use this attribute, but it was Nil, oh noes!", it just says "Wow, I saw a Nil" and that's it, making debugging a rather cumbersome activity I myself dislike, We even have a ticket at #40
But the way this PR resolves this seems less optimal to me.

if, for instance, one has an object with attributes that are optionally defined then one doesn't particularly want to be warned about it, and changing the interface of the object, which may already be used happily in other places, in order to quiet warnings here is not really ideal ( or even possible.)

Hmm.

As an example, let's say you have an object with field 'foo' which can be Nil / type object / whatever "not a real value".
Where it is used happily in other places is, I'd assume, accommodated to the fact. So it has checks like "If foo is present, do X, otherwise do Y". How are templates different as a consumer of this object?

IMO for deliberately omitted fields, a nice idea is to do <?.foo><.foo></?> which does the same thing but should give no warnings I think? But if the user has no idea foo can be Nil and passes it to just <.foo>, then there is a proper warning to be issued.

At worst case this behaviour can/should be configurable if people don't want to write the conditional wrapper, but making no warn policy a default sounds like a potentially dangerous thing, it's one thing when you write an HTML template assuming some things to be empty and another thing when you get a bunch of broken HTML attrs due to it (as it happens with e.g. the logs website).

No?

If there is a bug in the thing that is generating the data passed to the template then that can and should be determined by other means

Why should it be this way, can you please elaborate considering the foo example I described? A template sees an immediate issue at hand and warns about it, why something else should be preferred?

@lizmat
Copy link

lizmat commented Oct 27, 2021

Yeah, I agree the warnings are fine IFF they tell me where in the template / what key / method was used, etc.

The way it is now, is more like "computer says no". :-)

@jonathanstowe
Copy link
Author

IMO for deliberately omitted fields, a nice idea is to do ..

Yep, that works. I'm cool with doing that but it would be nicer if some diagnostic information could be output, right now it is difficult to determine where the undefined value is being used. Also it might be worth documenting that a warning will be issued and how to avoid it.

@Altai-man
Copy link
Member

Also it might be worth documenting that a warning will be issued and how to avoid it.

A proper solution for the ticket I mentioned should be enough to tick all the boxes. Should I close this PR?

@lizmat
Copy link

lizmat commented Oct 27, 2021

Re silencing warnings: if that were my intent, I could easily prefix render-template with a quietly.

I'm looking forward to the day where I can make a warnings collector using macros and RakuAST, which I have now done in App::IRC::Log:

my %warnings is BagHash;
CONTROL {
    when CX::Warn {
        %warnings.add(.message);
        .resume;
    }
}
$file.spurt: render-template $crot, %params;
for %warnings.sort(*.key) -> (:key($message), :value($seen)) {
    note $seen > 1
      ?? $seen ~ "x $message"
      !! $message;
}

which greatly reduces the amount of noise on STDERR :-)

@jonathanstowe
Copy link
Author

No, it's fine I'll close 😄

@jnthn jnthn mentioned this pull request Nov 5, 2021
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.

None yet

3 participants