From 433a112875c53066decd123ea5c2bfddd3e3bd6c Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 9 Dec 2024 22:57:59 -0500 Subject: [PATCH] Add Provider interface --- cmd/stepd/main.go | 2 +- environment.go | 13 ++++++++----- provider.go | 7 ++++++- providers/all.go | 6 +++--- 4 files changed, 18 insertions(+), 10 deletions(-) diff --git a/cmd/stepd/main.go b/cmd/stepd/main.go index b513c8e..1ef7f73 100644 --- a/cmd/stepd/main.go +++ b/cmd/stepd/main.go @@ -76,7 +76,7 @@ func main () { // set up the environment environment := step.Environment { } - environment.FuncProviders = providers.All() + environment.Providers = providers.All() err = environment.Init(context.Background()) if err != nil { log.Fatal(err) } diff --git a/environment.go b/environment.go index 1ba464e..7ea78aa 100644 --- a/environment.go +++ b/environment.go @@ -19,9 +19,10 @@ type Environment struct { // FS, if specified, is the filesystem that this environment will load // documents from. Its methods must be safe for concurrent execution. FS fs.FS - // FuncProviders is a slice of FuncMap providers whoose functions will - // be available to any documents this environment processes. - FuncProviders []FuncProvider + // Providers is a slice of objects which can provide various + // functionality to the environment. In order to be used, values in this + // slice must implement one or more of the interfaces in provider.go. + Providers []Provider documents usync.Locker[map[string] *Document] funcMap template.FuncMap @@ -31,7 +32,9 @@ type Environment struct { func (this *Environment) Init (ctx context.Context) error { this.documents = usync.NewLocker(make(map[string] *Document)) this.funcMap = make(template.FuncMap) - for _, provider := range this.FuncProviders { + for _, provider := range this.Providers { + provider, ok := provider.(FuncProvider) + if !ok { continue } funcMap := provider.FuncMap() if funcMap == nil { continue } for name, function := range funcMap { @@ -127,7 +130,7 @@ func (this *Environment) parse (name string, modTime time.Time, input io.Reader) for name, function := range this.funcMap { funcMap[name] = function } - for _, provider := range this.FuncProviders { + for _, provider := range this.Providers { provider, ok := provider.(FuncProviderFor) if !ok { continue } funcMapFor := provider.FuncMapFor(document) diff --git a/provider.go b/provider.go index 2db1d29..7df7b26 100644 --- a/provider.go +++ b/provider.go @@ -2,8 +2,12 @@ package step import "html/template" +// Provider is an object which provides extra functionality to an environment. +type Provider any + // FuncProvider provides a template.FuncMap. type FuncProvider interface { + Provider // FuncMap provides a template.FuncMap. It may return nil, in which case // its result is simply not considered. FuncMap () template.FuncMap @@ -12,7 +16,7 @@ type FuncProvider interface { // FuncProviderFor is an object that provides a template.FuncMap for a specific // document. type FuncProviderFor interface { - FuncProvider + Provider // FuncMap provides a template.FuncMap, given a particular document. It // may return nil, in which case its result is simply not considered. FuncMapFor (*Document) template.FuncMap @@ -20,6 +24,7 @@ type FuncProviderFor interface { // Configurable is an object that can be configured according to metadata. type Configurable interface { + Provider // Configure uses config to configure the object. It must not modify // config. Keys are namespaced with a '.' and are written in kebab case. Configure (config Meta) error diff --git a/providers/all.go b/providers/all.go index a4877eb..8d528ea 100644 --- a/providers/all.go +++ b/providers/all.go @@ -1,6 +1,6 @@ package providers -import "git.tebibyte.media/sashakoshka/step" +import "git.tebibyte.media/sashakoshka/step" import fpos "git.tebibyte.media/sashakoshka/step/providers/os" import fphttp "git.tebibyte.media/sashakoshka/step/providers/http" import fppath "git.tebibyte.media/sashakoshka/step/providers/path" @@ -11,8 +11,8 @@ import fpimport "git.tebibyte.media/sashakoshka/step/providers/import" import fpmarkdown "git.tebibyte.media/sashakoshka/step/providers/markdown" // All returns a slice of all providers defined in sub-packages. -func All () []step.FuncProvider { - return []step.FuncProvider { +func All () []step.Provider { + return []step.Provider { new(fpos.Provider), new(fphttp.Provider), new(fppath.Provider),