/
datasource_registry.go
45 lines (38 loc) · 1012 Bytes
/
datasource_registry.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
package datasources
import (
"fmt"
)
type DatasourceRegistry struct {
Sources []DatasourceInfo
}
var datasourceRegistry *DatasourceRegistry
func newDatasourceRegistry() *DatasourceRegistry {
return &DatasourceRegistry{}
}
func GetDatasourceRegistry() *DatasourceRegistry {
if datasourceRegistry == nil {
datasourceRegistry = newDatasourceRegistry()
}
return datasourceRegistry
}
// Register the given datasource with the registry. If the datasource already exists
// an error is thrown
func (d *DatasourceRegistry) Register(dsInfo DatasourceInfo) error {
for _, d := range d.Sources {
if d.ID == dsInfo.ID {
return fmt.Errorf("the data source %s is already registered", dsInfo.ID)
}
}
d.Sources = append(d.Sources, dsInfo)
return nil
}
// Get a datasource from the registry given its id.
// If the datasource does not exist, nil is returned
func (d *DatasourceRegistry) Get(id string) *DatasourceInfo {
for _, d := range d.Sources {
if d.ID == id {
return &d
}
}
return nil
}