-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathregister.go
More file actions
50 lines (40 loc) · 1.33 KB
/
register.go
File metadata and controls
50 lines (40 loc) · 1.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package queue
import (
"net/url"
"gopkg.in/src-d/go-errors.v0"
)
var (
// ErrUnsupportedProtocol is the error returned when a Broker does not know
// how to connect to a given URI.
ErrUnsupportedProtocol = errors.NewKind("unsupported protocol: %s")
// ErrMalformedURI is the error returned when a Broker does not know
// how to parse a given URI.
ErrMalformedURI = errors.NewKind("malformed connection URI: %s")
register = make(map[string]BrokerBuilder, 0)
)
// BrokerBuilder instantiates a new Broker based on the given uri.
type BrokerBuilder func(uri string) (Broker, error)
// Register registers a new BrokerBuilder to be used by NewBroker, this function
// should be used in an init function in the implementation packages such as
// `amqp` and `memory`.
func Register(name string, b BrokerBuilder) {
register[name] = b
}
// NewBroker creates a new Broker based on the given URI. In order to register
// different implementations the package should be imported, example:
//
// import _ "gopkg.in/src-d/go-queue.v1/amqp"
func NewBroker(uri string) (Broker, error) {
url, err := url.Parse(uri)
if err != nil {
return nil, ErrMalformedURI.Wrap(err, uri)
}
if url.Scheme == "" {
return nil, ErrMalformedURI.New(uri)
}
b, ok := register[url.Scheme]
if !ok {
return nil, ErrUnsupportedProtocol.New(url.Scheme)
}
return b(uri)
}