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

@@ -47,6 +47,7 @@ var (
_ resource.Cloner = (*genericResource)(nil)
_ resource.ResourcesLanguageMerger = (*resource.Resources)(nil)
_ resource.Identifier = (*genericResource)(nil)
_ resource.TransientIdentifier = (*genericResource)(nil)
_ targetPathProvider = (*genericResource)(nil)
_ sourcePathProvider = (*genericResource)(nil)
_ identity.IdentityGroupProvider = (*genericResource)(nil)
@@ -359,6 +360,9 @@ func GetTestInfoForResource(r resource.Resource) GenericResourceTestInfo {
type genericResource struct {
publishInit *sync.Once
key string
keyInit *sync.Once
sd ResourceSourceDescriptor
paths internal.ResourcePaths
@@ -444,19 +448,24 @@ func (l *genericResource) Data() any {
}
func (l *genericResource) Key() string {
basePath := l.spec.Cfg.BaseURL().BasePathNoTrailingSlash
var key string
if basePath == "" {
key = l.RelPermalink()
} else {
key = strings.TrimPrefix(l.RelPermalink(), basePath)
}
l.keyInit.Do(func() {
basePath := l.spec.Cfg.BaseURL().BasePathNoTrailingSlash
if basePath == "" {
l.key = l.RelPermalink()
} else {
l.key = strings.TrimPrefix(l.RelPermalink(), basePath)
}
if l.spec.Cfg.IsMultihost() {
key = l.spec.Lang() + key
}
if l.spec.Cfg.IsMultihost() {
l.key = l.spec.Lang() + l.key
}
})
return key
return l.key
}
func (l *genericResource) TransientKey() string {
return l.Key()
}
func (l *genericResource) targetPath() string {
@@ -623,6 +632,7 @@ func (rc *genericResource) cloneWithUpdates(u *transformationUpdate) (baseResour
func (l genericResource) clone() *genericResource {
l.publishInit = &sync.Once{}
l.keyInit = &sync.Once{}
return &l
}

View File

@@ -170,11 +170,19 @@ type ResourcesLanguageMerger interface {
// Identifier identifies a resource.
type Identifier interface {
// Key is is mostly for internal use and should be considered opaque.
// Key is mostly for internal use and should be considered opaque.
// This value may change between Hugo versions.
Key() string
}
// TransientIdentifier identifies a transient resource.
type TransientIdentifier interface {
// TransientKey is mostly for internal use and should be considered opaque.
// This value is implemented by transient resources where pointers may be short lived and
// not suitable for use as a map keys.
TransientKey() string
}
// WeightProvider provides a weight.
type WeightProvider interface {
Weight() int

View File

@@ -187,6 +187,7 @@ func (r *Spec) NewResource(rd ResourceSourceDescriptor) (resource.Resource, erro
Staler: &AtomicStaler{},
h: &resourceHash{},
publishInit: &sync.Once{},
keyInit: &sync.Once{},
paths: rp,
spec: r,
sd: rd,

View File

@@ -52,8 +52,10 @@ var (
_ identity.IdentityGroupProvider = (*resourceAdapterInner)(nil)
_ resource.Source = (*resourceAdapter)(nil)
_ resource.Identifier = (*resourceAdapter)(nil)
_ resource.TransientIdentifier = (*resourceAdapter)(nil)
_ targetPathProvider = (*resourceAdapter)(nil)
_ sourcePathProvider = (*resourceAdapter)(nil)
_ resource.Identifier = (*resourceAdapter)(nil)
_ resource.ResourceNameTitleProvider = (*resourceAdapter)(nil)
_ resource.WithResourceMetaProvider = (*resourceAdapter)(nil)
_ identity.DependencyManagerProvider = (*resourceAdapter)(nil)
@@ -279,6 +281,10 @@ func (r *resourceAdapter) Key() string {
return r.target.(resource.Identifier).Key()
}
func (r *resourceAdapter) TransientKey() string {
return r.Key()
}
func (r *resourceAdapter) targetPath() string {
r.init(false, false)
return r.target.(targetPathProvider).targetPath()