Closed kpym closed 2 years ago
Hi I've added functions replaceAll and replaceAllRegex to v1.0.16 I know sprig and functions are highly inspired by sprig (sprig is also mentioned in functions.go) - however I needed more flexibility on functions design so I've decided for own functions implementation
btw: You are welcome to contribute and add functions if missing and generally usefull ;)
@mmalcek Thenks for considering my request !
I think that replaceAll
and replaceAllRegex
do not have the right signature. For all template functions the main agrument should be the last one, in this way we can pipe it. For example if I have a french decimal number written as string like "3,14" and I want to replace the decimal separator by point I would like to do {{ "3,14" | replaceAll "," "." }}
.
My suggestion is to replace the definitions with
func replaceAll(old, new, src string) string
func replaceAllRegex(regex, new, src string) string
By the way some functions in sprig
have the same problem (putting the main argument in first and not last position).
@mmalcek The same is true, IMO, for other functions in bafi
. For example if I use {{ 7 | sub 2 }}
I'll get -5
and not 5
. I think that sub(a,b)
should be read as "substract a from b" and not "substract b from a". Same for div
, mod
,...
I know that this may looks like personal preference, but it is not. It is how golang templates are supposed to work ;)
I Agree with your point and it's better to do it this way. I did rewrite latest functions replaceAll and replaceAllRegex as you suggested (release is the same v1.0.16 rebuilded) I will not touch other functions (at least for now) because this is a "breaknig change" so I'll think about some alias or change to major release with HUGE warning.
@mmalcek Thanks for considering my request and modifying so quickly ! 🙏 Indeed, when adding a template function, the best is to think about how it will be used with pipes. But there is one situation where this is not possible: when the function is variadic (accepting multiple last arguments with ...
). IMO, this is a point where the golang team made a bad decision by piping to the last, not to the first argument. But anyway, we can't change this, so we have to adapt 😋.
release is the same v1.0.16 rebuilded
In general, every time you add some functionality which is backward compatible you should change the second number, for example adding replaceAll
should be v1.16.0
. And every time you correct something minor, you should change the last one, for example v1.16.1
.
I say this because when I executed :
go install github.com/mmalcek/bafi@latest
nothing happened because the version number hasn't changed (v1.0.16
).
In any case, thanks for your work !
Hi, I'm not sure if there is any practical use case for that. I briefly checked gomplate and it looks like an alternative to BaFi.
replaceAll was added a long time ago and there are many other functions https://mmalcek.github.io/bafi/#additional-functions
On Sat, 30 Mar 2024 at 16:36, K̶e̶v̶i̶n̶ @.***> wrote:
Hi. Is it possible to integrate gomplate with this project.
It has a replaceAll https://docs.gomplate.ca/functions/strings/#stringsreplaceall function, and a lot of other functions.
— Reply to this email directly, view it on GitHub https://github.com/mmalcek/bafi/issues/6#issuecomment-2028129762, or unsubscribe https://github.com/notifications/unsubscribe-auth/AEOZOAR5R2CTIWA674FOFM3Y23LXJAVCNFSM5JRYL7EKU5DIOJSWCZC7NNSXTN2JONZXKZKDN5WW2ZLOOQ5TEMBSHAYTEOJXGYZA . You are receiving this because you were mentioned.Message ID: @.***>
I was looking for string replace template function, something like
{{ "toto" | replace "to" "bi" }}
or{{ "toto" | regReplace "^t" "T" }}
, but it looks like there is no such function for the moment.May be you can consider to include all functions from sprig ? This is almost a "standard" library of template functions.
Note: I see that we can use Lua to add custom functions, but for such basic operation it is probably better to have it by default (and native go is faster than lua).