3
0
Files
corteza/store/connect.go
2020-09-08 07:54:23 +02:00

37 lines
736 B
Go

package store
import (
"context"
"fmt"
"strings"
)
type (
ConnectorFn func(ctx context.Context, dsn string) (s Storable, err error)
)
var (
registered = make(map[string]ConnectorFn)
)
func Connect(ctx context.Context, dsn string) (s Storable, err error) {
var storeType = strings.SplitN(dsn, "://", 2)[0]
if storeType == "" {
// Backward compatibility
storeType = "mysql"
}
if conn, ok := registered[storeType]; ok {
return conn(ctx, dsn)
} else {
return nil, fmt.Errorf("unknown store type used: %q (check your storage configuration)", storeType)
}
}
// Register add on ore more store types and their connector fn
func Register(fn ConnectorFn, tt ...string) {
for _, t := range tt {
registered[t] = fn
}
}