-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
Add update
template funcs to change maps and slices
#7232
Comments
The thing is, from your problematic case earlier: {{ $data := partialCached "foo" }}
{{ $data = $data | append "key" "value" }} The above would have exactly the same concurrency issues; the panic is one thing, but I since multiple threads are working on the same data structure -- the end result seems to be pretty random. |
Fair enough, but I think this proposal still has value. |
I think we need to let The performance problem with Maybe better would be to add a
|
I think Maybe some other languages like Javascript have some wording that make sense? |
We should make it work for slices to, so:
Would work. I thought about |
append
to build on mapsupdate
template funcs to change maps and slices
I'm looking for a use case but can't find one. Beside how could one know which index to update in the slice? Is there a way to identify the index of a value in a slice? An |
There is certainly some "shooting in the foot" potential for such a function, (
You can get it when ranging, e.g. |
Just got a cool showcase in for the docs site. |
Same for Are |
No, that does not change the slice. |
What about
|
??? |
Maybe ask the discourse for ideas? |
I think If we want to make it less evasive, maybe we could start wrapping data modifying functions in a keywords matching the type: |
@regisphilibert, your Of the options presented so far, I still like
While googling around, I saw another option: the Rust |
I like |
Is there no way at all to do this currently? I have a toml data template/file that I am having "Jsonified" based on a json.json file and I was leaving the date field set as "" expecting to be able to do something like: (Shortened) {{ range $log := $chlog }}
{{ if eq (index $log "date") "" }}
{{ (index $log "date") = (now.Format "2006-01-01") }} Which didn't work, so I tried to see if this would: {{.Scratch.Add "tomlData" $chlog}}
{{ range (.Scratch.Get "tomlData") }}
{{ if (eq (index . "date") "") }}
{{ .Scratch.SetInMap "tomlData" (index . "date") (now.Format "2006-01-01") }}
{{end}}
{{end}}
{{- jsonify (.Scratch.Get "tomlData") -}} That didn't work either. Is there some way to replace a blank value in a map |
@instance-id, please use the discussion forums for help with using Hugo. |
This issue has been automatically marked as stale because it has not had recent activity. The resources of the Hugo team are limited, and so we are asking for your help. |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I once wrote a proposal (#5604) for a complex data manipulation API which I now find unnecessary except for the ability to build on maps.
For now the only way to build on maps is either using
.Scratch
(SetInMap) or the merge function which tends to be much slower.A problematic data race (#7230 (comment)) was recently surfaced on.Scratch.SetInMap
. Applying a patch for it might slow the build of other more common usage.This proposal explores building on maps without Scratch.
Proposal
Introduce a global function to build on maps
1. Use the existing
append
functionIt is currently reserved for slices. This means the improved
append
would test for the input data type, and upon finding amap
, expect 3 arguments instead of 2:If key already exists, it would be overwritten.
2. A new function, yet to be named
It would take 3 parameter
If key already exists, it would be overwritten.
3.
keyVals
I know we use a
keyVals
function (undocumented outside of Related Content) I have no idea if this could be useful in this context, but some might chime in.Also
This would remove the use of
.Scratch.SetInMap
and allow the future retirement of.Scratch
. This was a great feature, but a workaround to now lifted Go Template limitations.The text was updated successfully, but these errors were encountered: