diff --git a/common/hcontext/context.go b/common/hcontext/context.go deleted file mode 100644 index 9524ef284..000000000 --- a/common/hcontext/context.go +++ /dev/null @@ -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) -} diff --git a/common/hugo/hugo.go b/common/hugo/hugo.go index 764a86a97..38f78bc35 100644 --- a/common/hugo/hugo.go +++ b/common/hugo/hugo.go @@ -28,12 +28,13 @@ import ( "github.com/bep/logg" "github.com/bep/godartsass/v2" - "github.com/gohugoio/hugo/common/hcontext" + "github.com/gohugoio/hugo/common/hexec" "github.com/gohugoio/hugo/common/loggers" "github.com/gohugoio/hugo/common/maps" "github.com/gohugoio/hugo/hugofs/files" + "github.com/bep/helpers/contexthelpers" "github.com/spf13/afero" iofs "io/fs" @@ -145,7 +146,7 @@ const ( contextKeyMarkupScope contextKey = iota ) -var markupScope = hcontext.NewContextDispatcher[string](contextKeyMarkupScope) +var markupScope = contexthelpers.NewContextDispatcher[string](contextKeyMarkupScope) type Context struct{} diff --git a/go.mod b/go.mod index 449c40ec4..f133774fa 100644 --- a/go.mod +++ b/go.mod @@ -14,7 +14,7 @@ require ( github.com/bep/golibsass v1.2.0 github.com/bep/goportabletext v0.1.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/lazycache v0.8.0 github.com/bep/logg v0.4.0 diff --git a/go.sum b/go.sum index 21c3d554a..b98375fc1 100644 --- a/go.sum +++ b/go.sum @@ -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/helpers v0.5.0 h1:rneezhnG7GzLFlsEWO/EnleaBRuluBDGFimalO6Y50o= 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/go.mod h1:23AF6O+4fUi9avjiydpKLStUNtJr5hJB4rarG18JpN8= github.com/bep/lazycache v0.8.0 h1:lE5frnRjxaOFbkPZ1YL6nijzOPPz6zeXasJq8WpG4L8= diff --git a/hugolib/page__content.go b/hugolib/page__content.go index 20abb7884..4b78a1ed0 100644 --- a/hugolib/page__content.go +++ b/hugolib/page__content.go @@ -26,8 +26,9 @@ import ( maps0 "maps" + "github.com/bep/helpers/contexthelpers" "github.com/bep/logg" - "github.com/gohugoio/hugo/common/hcontext" + "github.com/gohugoio/hugo/common/herrors" "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/hugo" @@ -673,7 +674,7 @@ const ( 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) { cp := c.pco diff --git a/resources/resource_factories/create/create.go b/resources/resource_factories/create/create.go index 2aecb5a93..14b17a105 100644 --- a/resources/resource_factories/create/create.go +++ b/resources/resource_factories/create/create.go @@ -23,6 +23,8 @@ import ( "strings" "time" + "github.com/bep/helpers/contexthelpers" + "github.com/bep/logg" "github.com/gohugoio/httpcache" hhttpcache "github.com/gohugoio/hugo/cache/httpcache" @@ -34,7 +36,7 @@ import ( "github.com/gohugoio/hugo/cache/dynacache" "github.com/gohugoio/hugo/cache/filecache" "github.com/gohugoio/hugo/common/hashing" - "github.com/gohugoio/hugo/common/hcontext" + "github.com/gohugoio/hugo/common/hugio" "github.com/gohugoio/hugo/common/tasks" "github.com/gohugoio/hugo/resources" @@ -48,7 +50,7 @@ type Client struct { httpClient *http.Client httpCacheConfig hhttpcache.ConfigCompiled cacheGetResource *filecache.Cache - resourceIDDispatcher hcontext.ContextDispatcher[string] + resourceIDDispatcher contexthelpers.ContextDispatcher[string] // Set when watching. remoteResourceChecker *tasks.RunEvery @@ -64,7 +66,7 @@ const ( // New creates a new Client with the given specification. func New(rs *resources.Spec) *Client { fileCache := rs.FileCaches.GetResourceCache() - resourceIDDispatcher := hcontext.NewContextDispatcher[string](contextKeyResourceID) + resourceIDDispatcher := contexthelpers.NewContextDispatcher[string](contextKeyResourceID) httpCacheConfig := rs.Cfg.GetConfigSection("httpCacheCompiled").(hhttpcache.ConfigCompiled) var remoteResourceChecker *tasks.RunEvery if rs.Cfg.Watching() && !httpCacheConfig.IsPollingDisabled() { diff --git a/tpl/template.go b/tpl/template.go index fde8b4bcc..bf76e99f5 100644 --- a/tpl/template.go +++ b/tpl/template.go @@ -21,8 +21,9 @@ import ( "sync" "unicode" + "github.com/bep/helpers/contexthelpers" bp "github.com/gohugoio/hugo/bufferpool" - "github.com/gohugoio/hugo/common/hcontext" + "github.com/gohugoio/hugo/identity" "github.com/gohugoio/hugo/langs" @@ -56,18 +57,18 @@ const ( // Context manages values passed in the context to templates. var Context = struct { - DependencyManagerScopedProvider hcontext.ContextDispatcher[identity.DependencyManagerScopedProvider] + DependencyManagerScopedProvider contexthelpers.ContextDispatcher[identity.DependencyManagerScopedProvider] GetDependencyManagerInCurrentScope func(context.Context) identity.Manager - DependencyScope hcontext.ContextDispatcher[int] - Page hcontext.ContextDispatcher[page] - IsInGoldmark hcontext.ContextDispatcher[bool] - CurrentTemplate hcontext.ContextDispatcher[*CurrentTemplateInfo] + DependencyScope contexthelpers.ContextDispatcher[int] + Page contexthelpers.ContextDispatcher[page] + IsInGoldmark contexthelpers.ContextDispatcher[bool] + CurrentTemplate contexthelpers.ContextDispatcher[*CurrentTemplateInfo] }{ - DependencyManagerScopedProvider: hcontext.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKeyDependencyManagerScopedProvider), - DependencyScope: hcontext.NewContextDispatcher[int](contextKeyDependencyScope), - Page: hcontext.NewContextDispatcher[page](contextKeyPage), - IsInGoldmark: hcontext.NewContextDispatcher[bool](contextKeyIsInGoldmark), - CurrentTemplate: hcontext.NewContextDispatcher[*CurrentTemplateInfo](cntextKeyCurrentTemplateInfo), + DependencyManagerScopedProvider: contexthelpers.NewContextDispatcher[identity.DependencyManagerScopedProvider](contextKeyDependencyManagerScopedProvider), + DependencyScope: contexthelpers.NewContextDispatcher[int](contextKeyDependencyScope), + Page: contexthelpers.NewContextDispatcher[page](contextKeyPage), + IsInGoldmark: contexthelpers.NewContextDispatcher[bool](contextKeyIsInGoldmark), + CurrentTemplate: contexthelpers.NewContextDispatcher[*CurrentTemplateInfo](cntextKeyCurrentTemplateInfo), } func init() {