Silly idea I had

This commit is contained in:
Sasha Koshka 2024-12-09 23:32:12 -05:00
parent 8084b7749a
commit a2680c40d2
2 changed files with 25 additions and 9 deletions

View File

@ -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()

View File

@ -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
}