own the usage of reflection into the RegisterType

allowing the users to only pass instances of the interfaces
This commit is contained in:
Mohammed Al Sahaf 2022-06-14 20:51:58 +03:00
parent 52f43d2f4c
commit b825a10927
16 changed files with 50 additions and 61 deletions

View file

@ -34,7 +34,6 @@ import (
"net/url"
"os"
"path"
"reflect"
"regexp"
"strconv"
"strings"
@ -56,9 +55,7 @@ func init() {
if env, exists := os.LookupEnv("CADDY_ADMIN"); exists {
DefaultAdminListen = env
}
RegisterType("caddy.config_loaders", []reflect.Type{
reflect.TypeOf((*ConfigLoader)(nil)).Elem(),
})
RegisterType("caddy.config_loaders", []interface{}{(*ConfigLoader)(nil)})
}
// AdminConfig configures Caddy's API endpoint, which is used

View file

@ -27,7 +27,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"runtime/debug"
"strconv"
"strings"
@ -43,11 +42,11 @@ import (
)
func init() {
RegisterType("", []reflect.Type{
reflect.TypeOf((*App)(nil)).Elem(),
RegisterType("", []interface{}{
(*App)(nil),
})
RegisterType("caddy.storage", []reflect.Type{
reflect.TypeOf((*StorageConverter)(nil)).Elem(),
RegisterType("caddy.storage", []interface{}{
(*StorageConverter)(nil),
})
}

View file

@ -7,8 +7,8 @@ import (
_ "github.com/caddyserver/caddy/v2/modules/standard"
)
// Validates Caddy's the registered internal types implement the necessary interfaces of their
// namespaces
// Validates that Caddy's registered internal modules implement the necessary interfaces of their
// respective namespaces
func TestTypes(t *testing.T) {
var i int
for _, v := range caddy.Modules() {

View file

@ -24,7 +24,6 @@ import (
"net"
"net/netip"
"os"
"reflect"
"strconv"
"strings"
"sync"
@ -39,8 +38,8 @@ import (
)
func init() {
RegisterType("caddy.listeners", []reflect.Type{
reflect.TypeOf((*ListenerWrapper)(nil)).Elem(),
RegisterType("caddy.listeners", []interface{}{
(*ListenerWrapper)(nil),
})
}

View file

@ -20,7 +20,6 @@ import (
"io"
"log"
"os"
"reflect"
"strings"
"sync"
"time"
@ -34,11 +33,11 @@ func init() {
RegisterModule(StdoutWriter{})
RegisterModule(StderrWriter{})
RegisterModule(DiscardWriter{})
RegisterType("caddy.logging.encoders", []reflect.Type{
reflect.TypeOf((*zapcore.Encoder)(nil)).Elem(),
RegisterType("caddy.logging.encoders", []interface{}{
(*zapcore.Encoder)(nil),
})
RegisterType("caddy.logging.writers", []reflect.Type{
reflect.TypeOf((*WriterOpener)(nil)).Elem(),
RegisterType("caddy.logging.writers", []interface{}{
(*WriterOpener)(nil),
})
}

View file

@ -21,7 +21,6 @@ import (
"fmt"
weakrand "math/rand"
"net/http"
"reflect"
"strings"
"sync"
"time"
@ -34,11 +33,11 @@ import (
func init() {
caddy.RegisterModule(HTTPBasicAuth{})
caddy.RegisterType("http.authentication.hashes", []reflect.Type{
reflect.TypeOf((*Comparer)(nil)).Elem(),
caddy.RegisterType("http.authentication.hashes", []interface{}{
(*Comparer)(nil),
})
caddy.RegisterType("http.authentication.providers", []reflect.Type{
reflect.TypeOf((*Authenticator)(nil)).Elem(),
caddy.RegisterType("http.authentication.providers", []interface{}{
(*Authenticator)(nil),
})
weakrand.Seed(time.Now().UnixNano())

View file

@ -26,7 +26,6 @@ import (
"math"
"net"
"net/http"
"reflect"
"sort"
"strconv"
"strings"
@ -38,8 +37,8 @@ import (
func init() {
caddy.RegisterModule(Encode{})
caddy.RegisterType("http.encoders", []reflect.Type{
reflect.TypeOf((*Encoding)(nil)).Elem(),
caddy.RegisterType("http.encoders", []interface{}{
(*Encoding)(nil),
})
}

View file

@ -26,7 +26,6 @@ import (
"os"
"path"
"path/filepath"
"reflect"
"runtime"
"strconv"
"strings"
@ -42,8 +41,8 @@ import (
func init() {
weakrand.Seed(time.Now().UnixNano())
caddy.RegisterType("http.precompressed", []reflect.Type{
reflect.TypeOf((*encode.Precompressed)(nil)).Elem(),
caddy.RegisterType("http.precompressed", []interface{}{
(*encode.Precompressed)(nil),
})
caddy.RegisterModule(FileServer{})

View file

@ -214,8 +214,8 @@ func init() {
caddy.RegisterModule(MatchHeaderRE{})
caddy.RegisterModule(new(MatchProtocol))
caddy.RegisterModule(MatchNot{})
caddy.RegisterType("http.matchers", []reflect.Type{
reflect.TypeOf((*RequestMatcher)(nil)).Elem(),
caddy.RegisterType("http.matchers", []interface{}{
(*RequestMatcher)(nil),
})
}

View file

@ -27,7 +27,6 @@ import (
"net/netip"
"net/textproto"
"net/url"
"reflect"
"strconv"
"strings"
"sync"
@ -47,17 +46,17 @@ import (
func init() {
caddy.RegisterModule(Handler{})
caddy.RegisterType("http.reverse_proxy.circuit_breakers", []reflect.Type{
reflect.TypeOf((*CircuitBreaker)(nil)).Elem(),
caddy.RegisterType("http.reverse_proxy.circuit_breakers", []interface{}{
(*CircuitBreaker)(nil),
})
caddy.RegisterType("http.reverse_proxy.selection_policies", []reflect.Type{
reflect.TypeOf((*Selector)(nil)).Elem(),
caddy.RegisterType("http.reverse_proxy.selection_policies", []interface{}{
(*Selector)(nil),
})
caddy.RegisterType("http.reverse_proxy.transport", []reflect.Type{
reflect.TypeOf((*http.RoundTripper)(nil)).Elem(),
caddy.RegisterType("http.reverse_proxy.transport", []interface{}{
(*http.RoundTripper)(nil),
})
caddy.RegisterType("http.reverse_proxy.upstreams", []reflect.Type{
reflect.TypeOf((*UpstreamSource)(nil)).Elem(),
caddy.RegisterType("http.reverse_proxy.upstreams", []interface{}{
(*UpstreamSource)(nil),
})
}

View file

@ -18,14 +18,13 @@ import (
"encoding/json"
"fmt"
"net/http"
"reflect"
"github.com/caddyserver/caddy/v2"
)
func init() {
caddy.RegisterType("http.handlers", []reflect.Type{
reflect.TypeOf((*MiddlewareHandler)(nil)).Elem(),
caddy.RegisterType("http.handlers", []interface{}{
(*MiddlewareHandler)(nil),
})
}

View file

@ -20,7 +20,6 @@ import (
"errors"
"fmt"
"net/http"
"reflect"
"strings"
"time"
@ -32,14 +31,14 @@ import (
)
func init() {
caddy.RegisterType("dns.provider", []reflect.Type{
reflect.TypeOf((*acmez.Solver)(nil)).Elem(),
caddy.RegisterType("dns.provider", []interface{}{
(*acmez.Solver)(nil),
})
caddy.RegisterType("tls.get_certificate", []reflect.Type{
reflect.TypeOf((*certmagic.Manager)(nil)).Elem(),
caddy.RegisterType("tls.get_certificate", []interface{}{
(*certmagic.Manager)(nil),
})
caddy.RegisterType("tls.issuance", []reflect.Type{
reflect.TypeOf((*certmagic.Issuer)(nil)).Elem(),
caddy.RegisterType("tls.issuance", []interface{}{
(*certmagic.Issuer)(nil),
})
}

View file

@ -23,7 +23,6 @@ import (
"io"
"os"
"path/filepath"
"reflect"
"strings"
"github.com/mholt/acmez"
@ -34,8 +33,8 @@ import (
func init() {
caddy.RegisterModule(LeafCertClientAuth{})
caddy.RegisterType("tls.handshake_match", []reflect.Type{
reflect.TypeOf((*ConnectionMatcher)(nil)).Elem(),
caddy.RegisterType("tls.handshake_match", []interface{}{
(*ConnectionMatcher)(nil),
})
}

View file

@ -21,7 +21,6 @@ import (
"fmt"
"io"
"log"
"reflect"
"runtime/debug"
"sync"
"time"
@ -30,8 +29,8 @@ import (
)
func init() {
caddy.RegisterType("tls.stek", []reflect.Type{
reflect.TypeOf((*STEKProvider)(nil)).Elem(),
caddy.RegisterType("tls.stek", []interface{}{
(*STEKProvider)(nil),
})
}

View file

@ -21,7 +21,6 @@ import (
"fmt"
"log"
"net/http"
"reflect"
"runtime/debug"
"sync"
"time"
@ -37,8 +36,8 @@ func init() {
caddy.RegisterModule(TLS{})
caddy.RegisterModule(AutomateLoader{})
caddy.RegisterType("tls.certificates", []reflect.Type{
reflect.TypeOf((*CertificateLoader)(nil)).Elem(),
caddy.RegisterType("tls.certificates", []interface{}{
(*CertificateLoader)(nil),
})
}

View file

@ -7,7 +7,11 @@ import (
var namespaceTypes map[string][]reflect.Type = make(map[string][]reflect.Type)
func RegisterType(namespace string, types []reflect.Type) {
func RegisterType(namespace string, vals []interface{}) {
var types []reflect.Type
for _, v := range vals {
reflect.TypeOf(v).Elem()
}
if _, ok := namespaceTypes[namespace]; ok {
panic("namespace is already registered")
}