-
Notifications
You must be signed in to change notification settings - Fork 701
/
datasource.go
62 lines (53 loc) · 1.5 KB
/
datasource.go
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
51
52
53
54
55
56
57
58
59
60
61
62
package conf
import (
"errors"
"io"
"net/url"
)
var (
//ErrConfigAddr not config
ErrConfigAddr = errors.New("no config... ")
// ErrInvalidDataSource defines an error that the scheme has been registered
ErrInvalidDataSource = errors.New("invalid data source, please make sure the scheme has been registered")
datasourceBuilders = make(map[string]DataSourceCreatorFunc)
// configDecoder = make(map[string]Unmarshaller)
)
// DataSourceCreatorFunc represents a dataSource creator function
type DataSourceCreatorFunc func() DataSource
// DataSource ...
type DataSource interface {
ReadConfig() ([]byte, error)
IsConfigChanged() <-chan struct{}
io.Closer
}
// Register registers a dataSource creator function to the registry
func Register(scheme string, creator DataSourceCreatorFunc) {
datasourceBuilders[scheme] = creator
}
// CreateDataSource creates a dataSource witch has been registered
// func CreateDataSource(scheme string) (conf.DataSource, error) {
// creatorFunc, exist := registry[scheme]
// if !exist {
// return nil, ErrInvalidDataSource
// }
// return creatorFunc(), nil
// }
// NewDataSource ..
func NewDataSource(configAddr string) (DataSource, error) {
if configAddr == "" {
return nil, ErrConfigAddr
}
urlObj, err := url.Parse(configAddr)
if err != nil {
return nil, err
}
var scheme = urlObj.Scheme
if scheme == "" {
scheme = "file"
}
creatorFunc, exist := datasourceBuilders[scheme]
if !exist {
return nil, ErrInvalidDataSource
}
return creatorFunc(), nil
}