forgejo/modules
wxiaoguang 5b89670a31
Use a general Eval function for expressions in templates. (#23927)
One of the proposals in #23328

This PR introduces a simple expression calculator
(templates/eval/eval.go), it can do basic expression calculations.

Many untested template helper functions like `Mul` `Add` can be replaced
by this new approach.

Then these `Add` / `Mul` / `percentage` / `Subtract` / `DiffStatsWidth`
could all use this `Eval`.

And it provides enhancements for Golang templates, and improves
readability.

Some examples:

----

* Before: `{{Add (Mul $glyph.Row 12) 12}}`
* After: `{{Eval $glyph.Row "*" 12 "+" 12}}`

----

* Before: `{{if lt (Add $i 1) (len $.Topics)}}`
* After: `{{if Eval $i "+" 1 "<" (len $.Topics)}}`

## FAQ

### Why not use an existing expression package?

We need a highly customized expression engine:

* do the calculation on the fly, without pre-compiling
* deal with int/int64/float64 types, to make the result could be used in
Golang template.
* make the syntax could be used in the Golang template directly
* do not introduce too much complex or strange syntax, we just need a
simple calculator.
* it needs to strictly follow Golang template's behavior, for example,
Golang template treats all non-zero values as truth, but many 3rd
packages don't do so.

### What's the benefit?

* Developers don't need to add more `Add`/`Mul`/`Sub`-like functions,
they were getting more and more.
Now, only one `Eval` is enough for all cases.
* The new code reads better than old `{{Add (Mul $glyph.Row 12) 12}}`,
the old one isn't familiar to most procedural programming developers
(eg, the Golang expression syntax).
* The `Eval` is fully covered by tests, many old `Add`/`Mul`-like
functions were never tested.

### The performance?

It doesn't use `reflect`, it doesn't need to parse or compile when used
in Golang template, the performance is as fast as native Go template.

### Is it too complex? Could it be unstable?

The expression calculator program is a common homework for computer
science students, and it's widely used as a teaching and practicing
purpose for developers. The algorithm is pretty well-known.

The behavior can be clearly defined, it is stable.
2023-04-07 21:25:49 +08:00
..
actions Do not crash when parsing an invalid workflow file (#23972) 2023-04-07 20:12:10 +08:00
activitypub
analyze
auth
avatar
base Use a general Eval function for expressions in templates. (#23927) 2023-04-07 21:25:49 +08:00
cache
charset Use data-tooltip-content for tippy tooltip (#23649) 2023-03-24 18:35:38 +08:00
container
context Ensure RSS icon is present on all repo tabs (#23904) 2023-04-07 16:48:23 +08:00
csv
doctor Check LFS/Packages settings in dump and doctor command (#23631) 2023-03-23 20:30:28 +08:00
emoji
eventsource
generate
git Remove Repository.getFilesChanged to fix Actions paths and paths-ignore filter (#23920) 2023-04-07 14:42:43 +08:00
gitgraph
graceful
hcaptcha
highlight test_env: hardcode major go version in use (#23464) 2023-03-14 16:09:01 -04:00
hostmatcher
html
httpcache
httplib Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687) 2023-03-29 14:32:26 +08:00
indexer Add meilisearch support (#23136) 2023-03-28 22:23:23 -04:00
issue/template
json
label
lfs Make minio package support legacy MD5 checksum (#23768) 2023-03-28 11:10:24 -04:00
log
markup Append (comment) when a link points at a comment rather than the whole issue (#23734) 2023-04-03 16:02:57 +08:00
mcaptcha
metrics
migration
mirror
nosql
notification
options Introduce path Clean/Join helper functions (#23495) 2023-03-21 16:02:49 -04:00
packages Display image size for multiarch container images (#23821) 2023-04-02 17:53:37 +08:00
paginator
pprof
private Refactor internal API for git commands, use meaningful messages instead of "Internal Server Error" (#23687) 2023-03-29 14:32:26 +08:00
process
proxy
proxyprotocol
public Fix incorrect CORS failure detection logic (#23844) 2023-03-31 11:35:48 -04:00
queue
recaptcha
references
regexplru
repository
secret
session
setting Add ONLY_SHOW_RELEVANT_REPOS back, fix explore page bug, make code more strict (#23766) 2023-03-29 08:41:45 -05:00
sitemap
ssh
storage Make minio package support legacy MD5 checksum (#23768) 2023-03-28 11:10:24 -04:00
structs Add activity feeds API (#23494) 2023-04-04 21:35:31 +08:00
svg Keep (add if not existing) xmlns attribute for generated SVG images (#23410) 2023-03-21 13:39:27 +08:00
sync
system
templates Use a general Eval function for expressions in templates. (#23927) 2023-04-07 21:25:49 +08:00
test
timeutil Use data-tooltip-content for tippy tooltip (#23649) 2023-03-24 18:35:38 +08:00
translation Append (comment) when a link points at a comment rather than the whole issue (#23734) 2023-04-03 16:02:57 +08:00
turnstile
typesniffer
updatechecker
upload
uri
user
util Use a general Eval function for expressions in templates. (#23927) 2023-04-07 21:25:49 +08:00
validation
watcher
web
webhook