diff options
author | Matthew Holt <[email protected]> | 2019-08-21 10:46:35 -0600 |
---|---|---|
committer | Matthew Holt <[email protected]> | 2019-08-21 10:46:35 -0600 |
commit | c9980fd3671d873a7197a5ac4d6ac9d6b046abb6 (patch) | |
tree | 75c301ab10590fb5f7d5b869a3424b8d46176bbf /context.go | |
parent | c4159ef76d279d6a84257b24dbe97430af32eb1e (diff) | |
download | caddy-c9980fd3671d873a7197a5ac4d6ac9d6b046abb6.tar.gz caddy-c9980fd3671d873a7197a5ac4d6ac9d6b046abb6.zip |
Refactor Caddyfile adapter and module registration
Use piles from which to draw config values.
Module values can return their name, so now we can do two-way mapping
from value to name and name to value; whereas before we could only map
name to value. This was problematic with the Caddyfile adapter since
it receives values and needs to know the name to put in the config.
Diffstat (limited to 'context.go')
-rw-r--r-- | context.go | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/context.go b/context.go index 17488e403..2fd84d5f4 100644 --- a/context.go +++ b/context.go @@ -99,11 +99,16 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{}, return nil, fmt.Errorf("module '%s' has no constructor", mod.Name) } - val := mod.New() + val := mod.New().(interface{}) - // value must be a pointer for unmarshaling into concrete type + // value must be a pointer for unmarshaling into concrete type, even if + // the module's concrete type is a slice or map; New() *should* return + // a pointer, otherwise unmarshaling errors or panics will occur if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr { - val = reflect.New(rv.Type()).Elem().Addr().Interface() + log.Printf("[WARNING] ModuleInfo.New() for module '%s' did not return a pointer,"+ + " so we are using reflection to make a pointer instead; please fix this by"+ + " using new(Type) or &Type notation in your module's New() function.", name) + val = reflect.New(rv.Type()).Elem().Addr().Interface().(Module) } // fill in its config only if there is a config to fill in |