From a2680c40d21a2fbf56684d1f4eae31d8ad02ced6 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 9 Dec 2024 23:32:12 -0500 Subject: [PATCH] Silly idea I had --- environment.go | 9 ++++++++- provider.go | 25 +++++++++++++++++-------- 2 files changed, 25 insertions(+), 9 deletions(-) diff --git a/environment.go b/environment.go index e94f6fa..f10aea2 100644 --- a/environment.go +++ b/environment.go @@ -35,9 +35,16 @@ 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.Providers { + if provider, ok := provider.(ConfigProcessor); ok { + err := provider.ProcessConfig(this.Conf) + if err != nil { return err } + } + } for _, provider := range this.Providers { if provider, ok := provider.(Configurable); ok { - provider.Configure(this.Conf) + err := provider.Configure(this.Conf) + if err != nil { return err } } if provider, ok := provider.(FuncProvider); ok { funcMap := provider.FuncMap() diff --git a/provider.go b/provider.go index 7df7b26..10ca6d7 100644 --- a/provider.go +++ b/provider.go @@ -5,6 +5,23 @@ import "html/template" // Provider is an object which provides extra functionality to an environment. type Provider any +// 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 +} + +// ConfigProcessor is an object that can alter the environment configuration +// before other providers are configured in order to provide a sort of macro +// system. +type ConfigProcessor interface { + // ProcessConfig analyzes and makes necessary changes to config. Unlike + // Configurable.Configure, it can modify config. + ProcessConfig (config Meta) error +} + // FuncProvider provides a template.FuncMap. type FuncProvider interface { Provider @@ -21,11 +38,3 @@ type FuncProviderFor interface { // may return nil, in which case its result is simply not considered. FuncMapFor (*Document) template.FuncMap } - -// 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 -}