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

Improve eq operator in where function #11287

Open
jmooring opened this issue Jul 26, 2023 · 7 comments
Open

Improve eq operator in where function #11287

jmooring opened this issue Jul 26, 2023 · 7 comments
Labels
Milestone

Comments

@jmooring
Copy link
Member

Use case 1: find all pages where tags equals ['foo'].
Use case 2: find all pages where tags equals ['foo','bar'].

This is not possible today. You can find all pages where these tags intersect a provided slice, but you cannot compare slices with the eq operator.

The eq operator does work if your front matter is a scalar, e.g.,

title = 'something 1`
tags = 'foo'

But you can't do this:

title = 'something 2`
tags = ['foo']

You should be able to get both of the pages above with:

{{ where .Pages "Params.tags" eq "foo" }} OR
{{ where .Pages "Params.tags" eq "foo" (slice "foo"}} 
@bep
Copy link
Member

bep commented Jul 26, 2023

Use case 1: find all pages where tags equals ['foo'].
Use case 2: find all pages where tags equals ['foo','bar'].

The above is rather technical use cases ...

I can understand if you want to find all pages tagged with foo, but finding all pages which has the tags foo and bar and only those, that's not a use case I have seen.

Note that Go cannot compare slices directly, so it would be a fairly costly operation if implemented.

@bep bep removed the NeedsTriage label Jul 26, 2023
@bep bep added this to the v0.116.0 milestone Jul 26, 2023
@jmooring
Copy link
Member Author

jmooring commented Jul 26, 2023

finding all pages which has the tags foo and bar and only those, that's not a use case I have seen

I'm fine with with limiting to one tag. The specific example is:

content/
├── publications/
│   ├── publication-1.md   <-- authors = ['John', 'Richard', 'Harold']
│   ├── publication-2.md   <-- authors = ['John', 'Richard']
│   └── publication-3.md   <-- authors = ['John']
└── _index.md

Find all publications where John is the only author. (common)
Find all publications where John and Richard are the only authors. (less common)

@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 5, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@bep bep modified the milestones: v0.121.0, v0.122.0 Dec 6, 2023
@bep bep modified the milestones: v0.122.0, v0.123.0, v0.124.0 Jan 27, 2024
@jmooring
Copy link
Member Author

Go 1.21 introduced the slices package which contains https://pkg.go.dev/slices#Equal.

@bep
Copy link
Member

bep commented Feb 18, 2024

Go 1.21 introduced the slices package which contains https://pkg.go.dev/slices#Equal.

The functional part of this has never been the challenge. If you look at that implementation you will find a ... loop.

@jmooring
Copy link
Member Author

Understood. Will stick with...

{{ $p := where site.RegularPages "Params.tags" "intersect" (slice "a" "b") }}
{{ range $p }}
  {{ if eq (len .Params.tags) 2 }}
    <h2><a href="{{ .RelPermalink }}">{{ .LinkTitle }}</a></h2>
  {{ end }}
{{ end }}

@bep bep reopened this Feb 18, 2024
@bep
Copy link
Member

bep commented Feb 18, 2024

To be clear:

tags = ['foo', 'bar']

{{ where .Pages "Params.tags" eq (slice "foo" "bar" }}

Would be cheapish to implement with strict ordering (['foo', 'bar'] != ['bar', 'foo']), but I'm not sure how useful that would be.

@jmooring
Copy link
Member Author

I agree that strict ordering limits its usage; not sure I'd pursue it.

@bep bep removed this from the v0.124.0 milestone Mar 4, 2024
@bep bep added this to the v0.125.0 milestone Mar 4, 2024
@bep bep modified the milestones: v0.125.0, v0.126.0 Apr 23, 2024
@bep bep modified the milestones: v0.126.0, v0.127.0 May 15, 2024
@bep bep modified the milestones: v0.127.0, v0.128.0 Jun 8, 2024
@bep bep modified the milestones: v0.128.0, v0.129.0 Jun 21, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants