From 2fd98cb040b617d2a018ae00a3a68b320de7dbc6 Mon Sep 17 00:00:00 2001 From: Matthew Holt Date: Tue, 21 May 2019 14:22:21 -0600 Subject: [PATCH] Module.New() does not need to return an error --- admin.go | 6 +----- context.go | 7 ++----- modules.go | 2 +- modules/caddyhttp/caddyhttp.go | 2 +- modules/caddyhttp/caddylog/log.go | 2 +- modules/caddyhttp/fileserver/matcher.go | 2 +- modules/caddyhttp/fileserver/staticfiles.go | 2 +- modules/caddyhttp/headers/headers.go | 2 +- modules/caddyhttp/matchers.go | 18 +++++++++--------- modules/caddyhttp/reverseproxy/module.go | 2 +- modules/caddyhttp/rewrite/rewrite.go | 2 +- modules/caddyhttp/staticresp.go | 2 +- modules/caddyhttp/table.go | 4 ++-- modules/caddytls/acmemanager.go | 2 +- modules/caddytls/fileloader.go | 2 +- modules/caddytls/folderloader.go | 2 +- modules/caddytls/matchers.go | 2 +- modules/caddytls/tls.go | 2 +- storage.go | 2 +- 19 files changed, 29 insertions(+), 36 deletions(-) diff --git a/admin.go b/admin.go index a123a64f1..b3d012b08 100644 --- a/admin.go +++ b/admin.go @@ -42,11 +42,7 @@ func StartAdmin(addr string) error { ///// END PPROF STUFF ////// for _, m := range GetModules("admin") { - moduleValue, err := m.New() - if err != nil { - return fmt.Errorf("initializing module '%s': %v", m.Name, err) - } - route := moduleValue.(AdminRoute) + route := m.New().(AdminRoute) mux.Handle(route.Pattern, route) } diff --git a/context.go b/context.go index e437f08ca..bdf9f6056 100644 --- a/context.go +++ b/context.go @@ -75,10 +75,7 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{}, return nil, fmt.Errorf("module '%s' has no constructor", mod.Name) } - val, err := mod.New() - if err != nil { - return nil, fmt.Errorf("initializing module '%s': %v", mod.Name, err) - } + val := mod.New() // value must be a pointer for unmarshaling into concrete type if rv := reflect.ValueOf(val); rv.Kind() != reflect.Ptr { @@ -87,7 +84,7 @@ func (ctx Context) LoadModule(name string, rawMsg json.RawMessage) (interface{}, // fill in its config only if there is a config to fill in if len(rawMsg) > 0 { - err = json.Unmarshal(rawMsg, &val) + err := json.Unmarshal(rawMsg, &val) if err != nil { return nil, fmt.Errorf("decoding module config: %s: %v", mod.Name, err) } diff --git a/modules.go b/modules.go index afe4b385a..19d203daa 100644 --- a/modules.go +++ b/modules.go @@ -20,7 +20,7 @@ type Module struct { // invoke methods on the returned value. // It must return a pointer; if not, it // is converted into one. - New func() (interface{}, error) + New func() interface{} } func (m Module) String() string { return m.Name } diff --git a/modules/caddyhttp/caddyhttp.go b/modules/caddyhttp/caddyhttp.go index e5b9f61e3..09a130e17 100644 --- a/modules/caddyhttp/caddyhttp.go +++ b/modules/caddyhttp/caddyhttp.go @@ -22,7 +22,7 @@ func init() { err := caddy2.RegisterModule(caddy2.Module{ Name: "http", - New: func() (interface{}, error) { return new(App), nil }, + New: func() interface{} { return new(App) }, }) if err != nil { diff --git a/modules/caddyhttp/caddylog/log.go b/modules/caddyhttp/caddylog/log.go index d3110cc9c..f39fd8762 100644 --- a/modules/caddyhttp/caddylog/log.go +++ b/modules/caddyhttp/caddylog/log.go @@ -12,7 +12,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.middleware.log", - New: func() (interface{}, error) { return new(Log), nil }, + New: func() interface{} { return new(Log) }, }) } diff --git a/modules/caddyhttp/fileserver/matcher.go b/modules/caddyhttp/fileserver/matcher.go index 29805a2d3..819613184 100644 --- a/modules/caddyhttp/fileserver/matcher.go +++ b/modules/caddyhttp/fileserver/matcher.go @@ -11,7 +11,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.file", - New: func() (interface{}, error) { return new(FileMatcher), nil }, + New: func() interface{} { return new(FileMatcher) }, }) } diff --git a/modules/caddyhttp/fileserver/staticfiles.go b/modules/caddyhttp/fileserver/staticfiles.go index 19f5f68cc..c6a01ac3a 100644 --- a/modules/caddyhttp/fileserver/staticfiles.go +++ b/modules/caddyhttp/fileserver/staticfiles.go @@ -21,7 +21,7 @@ func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.responders.file_server", - New: func() (interface{}, error) { return new(FileServer), nil }, + New: func() interface{} { return new(FileServer) }, }) } diff --git a/modules/caddyhttp/headers/headers.go b/modules/caddyhttp/headers/headers.go index b56bbf9ca..e94fb54cb 100644 --- a/modules/caddyhttp/headers/headers.go +++ b/modules/caddyhttp/headers/headers.go @@ -11,7 +11,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.middleware.headers", - New: func() (interface{}, error) { return new(Headers), nil }, + New: func() interface{} { return new(Headers) }, }) } diff --git a/modules/caddyhttp/matchers.go b/modules/caddyhttp/matchers.go index 13e122a4b..64a931253 100644 --- a/modules/caddyhttp/matchers.go +++ b/modules/caddyhttp/matchers.go @@ -32,39 +32,39 @@ type ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.host", - New: func() (interface{}, error) { return matchHost{}, nil }, + New: func() interface{} { return matchHost{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.path", - New: func() (interface{}, error) { return matchPath{}, nil }, + New: func() interface{} { return matchPath{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.path_regexp", - New: func() (interface{}, error) { return new(matchPathRE), nil }, + New: func() interface{} { return new(matchPathRE) }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.method", - New: func() (interface{}, error) { return matchMethod{}, nil }, + New: func() interface{} { return matchMethod{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.query", - New: func() (interface{}, error) { return matchQuery{}, nil }, + New: func() interface{} { return matchQuery{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.header", - New: func() (interface{}, error) { return matchHeader{}, nil }, + New: func() interface{} { return matchHeader{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.header_regexp", - New: func() (interface{}, error) { return matchHeaderRE{}, nil }, + New: func() interface{} { return matchHeaderRE{} }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.protocol", - New: func() (interface{}, error) { return new(matchProtocol), nil }, + New: func() interface{} { return new(matchProtocol) }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.starlark_expr", - New: func() (interface{}, error) { return new(matchStarlarkExpr), nil }, + New: func() interface{} { return new(matchStarlarkExpr) }, }) } diff --git a/modules/caddyhttp/reverseproxy/module.go b/modules/caddyhttp/reverseproxy/module.go index 1bfc9ad50..7e149d00e 100755 --- a/modules/caddyhttp/reverseproxy/module.go +++ b/modules/caddyhttp/reverseproxy/module.go @@ -8,6 +8,6 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.responders.reverse_proxy", - New: func() (interface{}, error) { return new(LoadBalanced), nil }, + New: func() interface{} { return new(LoadBalanced) }, }) } diff --git a/modules/caddyhttp/rewrite/rewrite.go b/modules/caddyhttp/rewrite/rewrite.go index 9bd01c0bf..b7878a5cb 100644 --- a/modules/caddyhttp/rewrite/rewrite.go +++ b/modules/caddyhttp/rewrite/rewrite.go @@ -12,7 +12,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.middleware.rewrite", - New: func() (interface{}, error) { return new(Rewrite), nil }, + New: func() interface{} { return new(Rewrite) }, }) } diff --git a/modules/caddyhttp/staticresp.go b/modules/caddyhttp/staticresp.go index 47fae229e..62ab7dd7b 100644 --- a/modules/caddyhttp/staticresp.go +++ b/modules/caddyhttp/staticresp.go @@ -11,7 +11,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.responders.static", - New: func() (interface{}, error) { return new(Static), nil }, + New: func() interface{} { return new(Static) }, }) } diff --git a/modules/caddyhttp/table.go b/modules/caddyhttp/table.go index 8c3ebe0fc..fce426764 100644 --- a/modules/caddyhttp/table.go +++ b/modules/caddyhttp/table.go @@ -9,12 +9,12 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "http.middleware.table", - New: func() (interface{}, error) { return new(tableMiddleware), nil }, + New: func() interface{} { return new(tableMiddleware) }, }) caddy2.RegisterModule(caddy2.Module{ Name: "http.matchers.table", - New: func() (interface{}, error) { return new(tableMatcher), nil }, + New: func() interface{} { return new(tableMatcher) }, }) } diff --git a/modules/caddytls/acmemanager.go b/modules/caddytls/acmemanager.go index 59fc7c36e..1e37b8608 100644 --- a/modules/caddytls/acmemanager.go +++ b/modules/caddytls/acmemanager.go @@ -15,7 +15,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "tls.management.acme", - New: func() (interface{}, error) { return new(acmeManagerMaker), nil }, + New: func() interface{} { return new(acmeManagerMaker) }, }) } diff --git a/modules/caddytls/fileloader.go b/modules/caddytls/fileloader.go index fae2275a8..516c63258 100644 --- a/modules/caddytls/fileloader.go +++ b/modules/caddytls/fileloader.go @@ -11,7 +11,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "tls.certificates.load_files", - New: func() (interface{}, error) { return fileLoader{}, nil }, + New: func() interface{} { return fileLoader{} }, }) } diff --git a/modules/caddytls/folderloader.go b/modules/caddytls/folderloader.go index 9d46502c5..8ec982756 100644 --- a/modules/caddytls/folderloader.go +++ b/modules/caddytls/folderloader.go @@ -16,7 +16,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "tls.certificates.load_folders", - New: func() (interface{}, error) { return folderLoader{}, nil }, + New: func() interface{} { return folderLoader{} }, }) } diff --git a/modules/caddytls/matchers.go b/modules/caddytls/matchers.go index bda4ec2a0..e155efd66 100644 --- a/modules/caddytls/matchers.go +++ b/modules/caddytls/matchers.go @@ -12,7 +12,7 @@ type MatchServerName []string func init() { caddy2.RegisterModule(caddy2.Module{ Name: "tls.handshake_match.host", - New: func() (interface{}, error) { return MatchServerName{}, nil }, + New: func() interface{} { return MatchServerName{} }, }) } diff --git a/modules/caddytls/tls.go b/modules/caddytls/tls.go index 4743e6b51..4e21adeaf 100644 --- a/modules/caddytls/tls.go +++ b/modules/caddytls/tls.go @@ -15,7 +15,7 @@ import ( func init() { caddy2.RegisterModule(caddy2.Module{ Name: "tls", - New: func() (interface{}, error) { return new(TLS), nil }, + New: func() interface{} { return new(TLS) }, }) } diff --git a/storage.go b/storage.go index 2b77851a2..2e35ac158 100644 --- a/storage.go +++ b/storage.go @@ -11,7 +11,7 @@ import ( func init() { RegisterModule(Module{ Name: "caddy.storage.file_system", - New: func() (interface{}, error) { return new(fileStorage), nil }, + New: func() interface{} { return new(fileStorage) }, }) }