common/hcontext: Replace with external package

This commit is contained in:
Bjørn Erik Pedersen
2025-08-21 21:58:43 +02:00
parent 13b43e6117
commit 186934feb4
7 changed files with 26 additions and 65 deletions

View File

@@ -1,46 +0,0 @@
// Copyright 2024 The Hugo Authors. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package hcontext
import "context"
// ContextDispatcher is a generic interface for setting and getting values from a context.
type ContextDispatcher[T any] interface {
Set(ctx context.Context, value T) context.Context
Get(ctx context.Context) T
}
// NewContextDispatcher creates a new ContextDispatcher with the given key.
func NewContextDispatcher[T any, R comparable](key R) ContextDispatcher[T] {
return keyInContext[T, R]{
id: key,
}
}
type keyInContext[T any, R comparable] struct {
zero T
id R
}
func (f keyInContext[T, R]) Get(ctx context.Context) T {
v := ctx.Value(f.id)
if v == nil {
return f.zero
}
return v.(T)
}
func (f keyInContext[T, R]) Set(ctx context.Context, value T) context.Context {
return context.WithValue(ctx, f.id, value)
}

View File

@@ -28,12 +28,13 @@ import (
"github.com/bep/logg" "github.com/bep/logg"
"github.com/bep/godartsass/v2" "github.com/bep/godartsass/v2"
"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/common/hexec" "github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/hugofs/files" "github.com/gohugoio/hugo/hugofs/files"
"github.com/bep/helpers/contexthelpers"
"github.com/spf13/afero" "github.com/spf13/afero"
iofs "io/fs" iofs "io/fs"
@@ -145,7 +146,7 @@ const (
contextKeyMarkupScope contextKey = iota contextKeyMarkupScope contextKey = iota
) )
var markupScope = hcontext.NewContextDispatcher[string](contextKeyMarkupScope) var markupScope = contexthelpers.NewContextDispatcher[string](contextKeyMarkupScope)
type Context struct{} type Context struct{}

2
go.mod
View File

@@ -14,7 +14,7 @@ require (
github.com/bep/golibsass v1.2.0 github.com/bep/golibsass v1.2.0
github.com/bep/goportabletext v0.1.0 github.com/bep/goportabletext v0.1.0
github.com/bep/gowebp v0.3.0 github.com/bep/gowebp v0.3.0
github.com/bep/helpers v0.5.0 github.com/bep/helpers v0.6.0
github.com/bep/imagemeta v0.12.0 github.com/bep/imagemeta v0.12.0
github.com/bep/lazycache v0.8.0 github.com/bep/lazycache v0.8.0
github.com/bep/logg v0.4.0 github.com/bep/logg v0.4.0

2
go.sum
View File

@@ -157,6 +157,8 @@ github.com/bep/gowebp v0.3.0 h1:MhmMrcf88pUY7/PsEhMgEP0T6fDUnRTMpN8OclDrbrY=
github.com/bep/gowebp v0.3.0/go.mod h1:ZhFodwdiFp8ehGJpF4LdPl6unxZm9lLFjxD3z2h2AgI= github.com/bep/gowebp v0.3.0/go.mod h1:ZhFodwdiFp8ehGJpF4LdPl6unxZm9lLFjxD3z2h2AgI=
github.com/bep/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o= github.com/bep/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o=
github.com/bep/helpers v0.5.0/go.mod h1:dSqCzIvHbzsk5YOesp1M7sKAq5xUcvANsRoKdawxH4Q= github.com/bep/helpers v0.5.0/go.mod h1:dSqCzIvHbzsk5YOesp1M7sKAq5xUcvANsRoKdawxH4Q=
github.com/bep/helpers v0.6.0 h1:qtqMCK8XPFNM9hp5Ztu9piPjxNNkk8PIyUVjg6v8Bsw=
github.com/bep/helpers v0.6.0/go.mod h1:IOZlgx5PM/R/2wgyCatfsgg5qQ6rNZJNDpWGXqDR044=
github.com/bep/imagemeta v0.12.0 h1:ARf+igs5B7pf079LrqRnwzQ/wEB8Q9v4NSDRZO1/F5k= github.com/bep/imagemeta v0.12.0 h1:ARf+igs5B7pf079LrqRnwzQ/wEB8Q9v4NSDRZO1/F5k=
github.com/bep/imagemeta v0.12.0/go.mod h1:23AF6O+4fUi9avjiydpKLStUNtJr5hJB4rarG18JpN8= github.com/bep/imagemeta v0.12.0/go.mod h1:23AF6O+4fUi9avjiydpKLStUNtJr5hJB4rarG18JpN8=
github.com/bep/lazycache v0.8.0 h1:lE5frnRjxaOFbkPZ1YL6nijzOPPz6zeXasJq8WpG4L8= github.com/bep/lazycache v0.8.0 h1:lE5frnRjxaOFbkPZ1YL6nijzOPPz6zeXasJq8WpG4L8=

View File

@@ -26,8 +26,9 @@ import (
maps0 "maps" maps0 "maps"
"github.com/bep/helpers/contexthelpers"
"github.com/bep/logg" "github.com/bep/logg"
"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/herrors"
"github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/hugo" "github.com/gohugoio/hugo/common/hugo"
@@ -673,7 +674,7 @@ const (
contextKeyContentCallback contextKey = iota contextKeyContentCallback contextKey = iota
) )
var setGetContentCallbackInContext = hcontext.NewContextDispatcher[func(*pageContentOutput, contentTableOfContents)](contextKeyContentCallback) var setGetContentCallbackInContext = contexthelpers.NewContextDispatcher[func(*pageContentOutput, contentTableOfContents)](contextKeyContentCallback)
func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfContents, error) { func (c *cachedContentScope) contentToC(ctx context.Context) (contentTableOfContents, error) {
cp := c.pco cp := c.pco

View File

@@ -23,6 +23,8 @@ import (
"strings" "strings"
"time" "time"
"github.com/bep/helpers/contexthelpers"
"github.com/bep/logg" "github.com/bep/logg"
"github.com/gohugoio/httpcache" "github.com/gohugoio/httpcache"
hhttpcache "github.com/gohugoio/hugo/cache/httpcache" hhttpcache "github.com/gohugoio/hugo/cache/httpcache"
@@ -34,7 +36,7 @@ import (
"github.com/gohugoio/hugo/cache/dynacache" "github.com/gohugoio/hugo/cache/dynacache"
"github.com/gohugoio/hugo/cache/filecache" "github.com/gohugoio/hugo/cache/filecache"
"github.com/gohugoio/hugo/common/hashing" "github.com/gohugoio/hugo/common/hashing"
"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/hugio"
"github.com/gohugoio/hugo/common/tasks" "github.com/gohugoio/hugo/common/tasks"
"github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/resources"
@@ -48,7 +50,7 @@ type Client struct {
httpClient *http.Client httpClient *http.Client
httpCacheConfig hhttpcache.ConfigCompiled httpCacheConfig hhttpcache.ConfigCompiled
cacheGetResource *filecache.Cache cacheGetResource *filecache.Cache
resourceIDDispatcher hcontext.ContextDispatcher[string] resourceIDDispatcher contexthelpers.ContextDispatcher[string]
// Set when watching. // Set when watching.
remoteResourceChecker *tasks.RunEvery remoteResourceChecker *tasks.RunEvery
@@ -64,7 +66,7 @@ const (
// New creates a new Client with the given specification. // New creates a new Client with the given specification.
func New(rs *resources.Spec) *Client { func New(rs *resources.Spec) *Client {
fileCache := rs.FileCaches.GetResourceCache() fileCache := rs.FileCaches.GetResourceCache()
resourceIDDispatcher := hcontext.NewContextDispatcher[string](contextKeyResourceID) resourceIDDispatcher := contexthelpers.NewContextDispatcher[string](contextKeyResourceID)
httpCacheConfig := rs.Cfg.GetConfigSection("httpCacheCompiled").(hhttpcache.ConfigCompiled) httpCacheConfig := rs.Cfg.GetConfigSection("httpCacheCompiled").(hhttpcache.ConfigCompiled)
var remoteResourceChecker *tasks.RunEvery var remoteResourceChecker *tasks.RunEvery
if rs.Cfg.Watching() && !httpCacheConfig.IsPollingDisabled() { if rs.Cfg.Watching() && !httpCacheConfig.IsPollingDisabled() {

View File

@@ -21,8 +21,9 @@ import (
"sync" "sync"
"unicode" "unicode"
"github.com/bep/helpers/contexthelpers"
bp "github.com/gohugoio/hugo/bufferpool" bp "github.com/gohugoio/hugo/bufferpool"
"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/identity" "github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/langs" "github.com/gohugoio/hugo/langs"
@@ -56,18 +57,18 @@ const (
// Context manages values passed in the context to templates. // Context manages values passed in the context to templates.
var Context = struct { var Context = struct {
DependencyManagerScopedProvider hcontext.ContextDispatcher[identity.DependencyManagerScopedProvider] DependencyManagerScopedProvider contexthelpers.ContextDispatcher[identity.DependencyManagerScopedProvider]
GetDependencyManagerInCurrentScope func(context.Context) identity.Manager GetDependencyManagerInCurrentScope func(context.Context) identity.Manager
DependencyScope hcontext.ContextDispatcher[int] DependencyScope contexthelpers.ContextDispatcher[int]
Page hcontext.ContextDispatcher[page] Page contexthelpers.ContextDispatcher[page]
IsInGoldmark hcontext.ContextDispatcher[bool] IsInGoldmark contexthelpers.ContextDispatcher[bool]
CurrentTemplate hcontext.ContextDispatcher[*CurrentTemplateInfo] CurrentTemplate contexthelpers.ContextDispatcher[*CurrentTemplateInfo]
}{ }{
DependencyManagerScopedProvider: hcontext.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKeyDependencyManagerScopedProvider), DependencyManagerScopedProvider: contexthelpers.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKeyDependencyManagerScopedProvider),
DependencyScope: hcontext.NewContextDispatcher[int](contextKeyDependencyScope), DependencyScope: contexthelpers.NewContextDispatcher[int](contextKeyDependencyScope),
Page: hcontext.NewContextDispatcher[page](contextKeyPage), Page: contexthelpers.NewContextDispatcher[page](contextKeyPage),
IsInGoldmark: hcontext.NewContextDispatcher[bool](contextKeyIsInGoldmark), IsInGoldmark: contexthelpers.NewContextDispatcher[bool](contextKeyIsInGoldmark),
CurrentTemplate: hcontext.NewContextDispatcher[*CurrentTemplateInfo](cntextKeyCurrentTemplateInfo), CurrentTemplate: contexthelpers.NewContextDispatcher[*CurrentTemplateInfo](cntextKeyCurrentTemplateInfo),
} }
func init() { func init() {