Fix union, complement, symdiff, and intersect for transient resources

Fixes #13181
This commit is contained in:
Bjørn Erik Pedersen
2024-12-22 17:59:03 +01:00
parent 48a7aee961
commit 4a5e94087b
6 changed files with 75 additions and 15 deletions

View File

@@ -249,3 +249,32 @@ tags: ['tag-b']
"2: Intersect: 1|\n2: Union: 3|\n2: SymDiff: 2|\n2: Uniq: 3|",
)
}
// Issue #13181
func TestUnionResourcesMatch(t *testing.T) {
t.Parallel()
files := `
-- config.toml --
disableKinds = ['rss','sitemap', 'taxonomy', 'term', 'page']
-- layouts/index.html --
{{ $a := resources.Match "*a*" }}
{{ $b := resources.Match "*b*" }}
{{ $union := $a | union $b }}
{{ range $i, $e := $union }}
{{ $i }}: {{ .Name }}
{{ end }}$
-- assets/a1.html --
<div>file1</div>
-- assets/a2.html --
<div>file2</div>
-- assets/a3_b1.html --
<div>file3</div>
-- assets/b2.html --
<div>file4</div>
`
b := hugolib.Test(t, files)
b.AssertFileContentExact("public/index.html", "0: /a3_b1.html\n\n1: /b2.html\n\n2: /a1.html\n\n3: /a2.html\n$")
}

View File

@@ -20,11 +20,12 @@ import (
"github.com/gohugoio/hugo/common/hashing"
"github.com/gohugoio/hugo/common/types"
"github.com/gohugoio/hugo/resources/resource"
)
var (
zero reflect.Value
errorType = reflect.TypeOf((*error)(nil)).Elem()
errorType = reflect.TypeFor[error]()
)
func numberToFloat(v reflect.Value) (float64, error) {
@@ -56,7 +57,13 @@ func normalize(v reflect.Value) any {
return f
}
}
return types.Unwrapv(v.Interface())
vv := types.Unwrapv(v.Interface())
if ip, ok := vv.(resource.TransientIdentifier); ok {
return ip.TransientKey()
}
return vv
}
// collects identities from the slices in seqs into a set. Numeric values are normalized,
@@ -151,7 +158,6 @@ func convertNumber(v reflect.Value, to reflect.Kind) (reflect.Value, error) {
case reflect.Uint64:
n = reflect.ValueOf(uint64(i))
}
}
if !n.IsValid() {