Skip to content

Commit 2f704b9

Browse files
committed
Address remaining review comments.
- use Name as the unique key to allow future extension to support multiple instances of the same type. - remove GetSourceByName/Type. Signed-off-by: Etai Lev Ran <[email protected]>
1 parent 9b72e7e commit 2f704b9

File tree

3 files changed

+5
-40
lines changed

3 files changed

+5
-40
lines changed

pkg/epp/datalayer/datasource.go

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,22 +66,12 @@ func (dsr *DataSourceRegistry) Register(src DataSource) error {
6666
if src == nil {
6767
return errors.New("unable to register a nil data source")
6868
}
69-
if _, loaded := dsr.sources.LoadOrStore(src.TypedName().Type, src); loaded {
69+
if _, loaded := dsr.sources.LoadOrStore(src.TypedName().Name, src); loaded {
7070
return fmt.Errorf("unable to register duplicate data source: %s", src.TypedName().String())
7171
}
7272
return nil
7373
}
7474

75-
// GetSourceByType fetches a source by type.
76-
func (dsr *DataSourceRegistry) GetSourceByType(dsType string) (DataSource, bool) {
77-
if val, ok := dsr.sources.Load(dsType); ok {
78-
if ds, ok := val.(DataSource); ok {
79-
return ds, true
80-
}
81-
}
82-
return nil, false
83-
}
84-
8575
// GetSources returns all registered sources.
8676
func (dsr *DataSourceRegistry) GetSources() []DataSource {
8777
var result []DataSource
@@ -101,21 +91,6 @@ func RegisterSource(src DataSource) error {
10191
return defaultDataSources.Register(src)
10292
}
10393

104-
// GetSourceByType returns a typed data source from the default registry.
105-
func GetSourceByType[T DataSource](dsType string) (T, bool) {
106-
v, ok := defaultDataSources.GetSourceByType(dsType)
107-
if !ok {
108-
var zero T
109-
return zero, false
110-
}
111-
src, ok := v.(T)
112-
if !ok {
113-
var zero T
114-
return zero, false
115-
}
116-
return src, true
117-
}
118-
11994
// GetSources returns the list of data sources registered in the default registry.
12095
func GetSources() []DataSource {
12196
return defaultDataSources.GetSources()

pkg/epp/datalayer/datasource_test.go

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,11 +53,6 @@ func TestRegisterAndGetSource(t *testing.T) {
5353
err = reg.Register(nil)
5454
assert.Error(t, err, "expected error on nil")
5555

56-
// Get by type
57-
got, found := reg.GetSourceByType(testType)
58-
assert.True(t, found, "expected to find registered data source")
59-
assert.Equal(t, testType, got.TypedName().Type)
60-
6156
// Get all sources
6257
all := reg.GetSources()
6358
assert.Len(t, all, 1)
@@ -67,21 +62,16 @@ func TestRegisterAndGetSource(t *testing.T) {
6762
err = RegisterSource(ds)
6863
assert.NoError(t, err, "expected no error on registration")
6964

70-
// Get by type
71-
got, found = GetSourceByType[*mockDataSource](testType)
72-
assert.True(t, found, "expected to find registered data source")
73-
assert.Equal(t, testType, got.TypedName().Type)
74-
7565
// Get all sources
7666
all = GetSources()
7767
assert.Len(t, all, 1)
7868
assert.Equal(t, testType, all[0].TypedName().Type)
7969
}
8070

81-
func TestGetNamedSourceWhenNotFound(t *testing.T) {
71+
func TestGetSourceWhenNoneAreRegistered(t *testing.T) {
8272
reg := DataSourceRegistry{}
83-
_, found := reg.GetSourceByType("missing")
84-
assert.False(t, found, "expected source to be missing")
73+
found := reg.GetSources()
74+
assert.Empty(t, found, "expected no sources to be returned")
8575
}
8676

8777
func TestValidateExtractorType(t *testing.T) {

pkg/epp/datalayer/metrics/datasource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ func (dataSrc *DataSource) AddExtractor(extractor datalayer.Extractor) error {
9090
if err := datalayer.ValidateExtractorType(PrometheusMetricType, extractor.ExpectedInputType()); err != nil {
9191
return err
9292
}
93-
if _, loaded := dataSrc.extractors.LoadOrStore(extractor.TypedName().Type, extractor); loaded {
93+
if _, loaded := dataSrc.extractors.LoadOrStore(extractor.TypedName().Name, extractor); loaded {
9494
return fmt.Errorf("attempt to add duplicate extractor %s to %s", extractor.TypedName(), dataSrc.TypedName())
9595
}
9696
return nil

0 commit comments

Comments
 (0)