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

[Improvement] (merge function) Multiple merging dict #7595

Closed
ngdangtu-vn opened this issue Aug 25, 2020 · 6 comments · Fixed by #7601
Closed

[Improvement] (merge function) Multiple merging dict #7595

ngdangtu-vn opened this issue Aug 25, 2020 · 6 comments · Fixed by #7601

Comments

@ngdangtu-vn
Copy link

ngdangtu-vn commented Aug 25, 2020

My current hugo version:

  • Hugo Static Site Generator: v0.74.3-DA0437B4 linux/arm
  • BuildDate: 2020-07-23T16:22:34Z

The current merge function only allow merge a pair of dict. However, in a certain case, more than two dicts could be merged. For example, if you want to make JSON template, dict is the best option for clean code. But if an object with too many properties could lead to a huge trouble.

{{- $obj := newScratch -}}
{{- $obj.Add "list" slice -}}
{{ $paginator := .Paginate (where .Site.RegularPages "Type" .Site.Data.Sections.Home.main) }}
{{- range $paginator.Pages -}}
	{{- $obj.Add "list" (dict "title" .Title "url" .Permalink "author" .Params.author "desc" .Description "pubdate" (dict "short" (.PublishDate.Format .Site.Params.DateFormats.Short) "long" (.Date.Format "2006-01-02T15:04:05") )) -}}
{{- end -}}
{{- $obj.Get "list" | jsonify -}}

Instead of that, merge is recommend to break down an infinitive long line for the sake of clean code.

{{- $obj := newScratch -}}
{{- $obj.Add "list" slice -}}
{{ $paginator := .Paginate (where .Site.RegularPages "Type" .Site.Data.Sections.Home.main) }}
{{- range $paginator.Pages -}}
	{{- $sDate := dict "short" (.PublishDate.Format .Site.Params.DateFormats.Short) -}}
	{{- $lDate := dict "long" (.Date.Format "2006-01-02T15:04:05") -}}
	{{- $pubdate := dict "pubdate" (merge $sDate $lDate) -}}

	{{- $require := dict "title" .Title "url" .Permalink -}}
	{{- $optional := dict "author" .Params.author "desc" .Description -}}

	{{- $obj.Add "list" (merge $require $optional $pubdate) -}}
{{- end -}}
{{- $obj.Get "list" | jsonify -}}

But with the merge limited, the above code is impossible to achieve.

Error: Error building site: failed to render pages: render of "home" failed: "/bla/bla/bla/layouts/index.json:12:22": execute of template failed: template: index.json:12:22: executing "index.json" at <merge>: wrong number of args for merge: want 2 got 3

I think it would be nice if we could have this improvement for the next update.

@ngdangtu-vn
Copy link
Author

ngdangtu-vn commented Aug 25, 2020

Right now, I'm trying to get around with .SetInMap:

{{- $obj := newScratch -}}
{{- $obj.Add "list" slice -}}
{{ $paginator := .Paginate (where .Site.RegularPages "Type" .Site.Data.Sections.Home.main) }}
{{- range $paginator.Pages -}}
	{{- $obj.SetInMap "item" "title" .Title -}}
	{{- $obj.SetInMap "item" "url" .Permalink -}}
	{{- $obj.SetInMap "item" "author" .Params.author -}}
	{{- $obj.SetInMap "item" "desc" .Description -}}

	{{- $sDate := dict "short" (.PublishDate.Format .Site.Params.DateFormats.Short) -}}
	{{- $lDate := dict "long" (.Date.Format "2006-01-02T15:04:05") -}}
	{{- $obj.SetInMap "item" "pubdate" (merge $sDate $lDate) -}}

	{{- $obj.Add "list" ($obj.Get "item") -}}
	{{- $obj.Delete "item" -}}
{{- end -}}
{{- $obj.Get "list" | jsonify -}}

But for more complex json, merge function still seems to work better.

@bep
Copy link
Member

bep commented Aug 25, 2020

I haven't read this is in detail, but wouldn't this be somehow the same: {{ $m = $m | merge $m2 | merge $m3 }} ....?

@ngdangtu-vn
Copy link
Author

ngdangtu-vn commented Aug 25, 2020

Is it even possible to make a chain for merge? I didn't know about this.

Update:
Yeah, pipeline merge seems to work fine. But do you think this {{ $m = merge $m $m2 $m3 }} is better? It seems shorter to me :D

@bep
Copy link
Member

bep commented Aug 25, 2020

It seems shorter to me :D

And potentially more effective, I agree.

@bep bep added Enhancement and removed Proposal labels Aug 25, 2020
@bep bep added this to the v0.75 milestone Aug 25, 2020
moorereason added a commit to moorereason/hugo that referenced this issue Aug 28, 2020
@moorereason
Copy link
Contributor

I've submitted a PR for consideration.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 29, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants