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

proposal: Allow for primitives & fmt.Stringer in addition to string in templates #746

Open
delaneyj opened this issue May 19, 2024 · 2 comments

Comments

@delaneyj
Copy link
Contributor

Discussion happened previous at #609 but focused on integers. I understand @a-h position on clarity around integers and currency. It seems like in the currency case having a type Currency int64 with a String() string function would allow it to be a valid fmt.Stringer and avoid ambiguity. Personally i'd like to see all primitive types and fmt.Stringer accepted by the template. Since the code is generated you aren't paying the reflection cost each time. strconv already has functions for Bool, Float, Int, etc. If you want to have more control, implement the Stringer interface

@joerdav joerdav changed the title Allow for primitives & fmt.Stringer in addition to string in templates proposal: Allow for primitives & fmt.Stringer in addition to string in templates May 20, 2024
@joerdav joerdav added NeedsDecision Issue needs some more discussion so a decision can be made proposal labels May 20, 2024
@joerdav
Copy link
Collaborator

joerdav commented May 30, 2024

Looking at our proposal template I think we can answer some questions straight away:

Backwards compatibility - This change should be completely backwards compatible, the generated code may be slightly different but shouldn't be breaking.
Threat model - I imagine in this change we would convert to strings and do the same escaping we already do.
Go version - We may introduce generics here in runtime, but that is fine, the oldest supported version has generics.
Automatic migration - Not applicable?
Compile time vs runtime errors - I believe that we can use generics here which would hopefully provide useful errors.
Documentation - We can add to https://templ.guide/syntax-and-usage/expressions
Examples - A few examples around primitives and how to implement stringer would be useful (currency is a great example)

There is also the question:

Correctness over time - How can we reduce the risk of defects both now, and in future releases?

But overall, I think that this proposal stands up to our requirements, it is just a case of whether moving from opaque string rendering to transparent (in some cases) is what we want.

I think we can solve @a-h concern of displaying nil to a user for example my not supporting any types that are nillable.

The reason the last one was closed is because we didn't get much engagement or discussion in the issue to form a consensus or even a majority.

@delaneyj
Copy link
Contributor Author

Per a short discussion on Discord with @joerdav . It might make sense to use a TemplString() string interface as there can be a difference between how you would logging something and want it displayed in a Templ template. I think the display/nil concern is mitigated as you can either error out in the generated code via an if check, or possibly check and fallback to a TemplNilString() interface if satisfied.

@linear linear bot added Migrated and removed NeedsDecision Issue needs some more discussion so a decision can be made Migrated proposal labels Sep 4, 2024
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

No branches or pull requests

2 participants