From 5e5d8d702911b0e870e9947290f006a62f8a7ce2 Mon Sep 17 00:00:00 2001 From: Antoine Toulme Date: Wed, 20 Dec 2023 21:40:58 -0800 Subject: [PATCH] remove interface --- internal/sharedcomponent/sharedcomponent.go | 18 +++++++----------- .../sharedcomponent/sharedcomponent_test.go | 10 +++++----- 2 files changed, 12 insertions(+), 16 deletions(-) diff --git a/internal/sharedcomponent/sharedcomponent.go b/internal/sharedcomponent/sharedcomponent.go index 098de1f5b70a..417137a8919e 100644 --- a/internal/sharedcomponent/sharedcomponent.go +++ b/internal/sharedcomponent/sharedcomponent.go @@ -13,24 +13,20 @@ import ( "go.opentelemetry.io/collector/component" ) -// Map keeps reference of all created instances for a given shared key such as a component configuration. -type Map[K comparable, V component.Component] interface { - // LoadOrStore returns the already created instance if exists, otherwise creates a new instance - // and adds it to the map of references. - LoadOrStore(key K, create func() (V, error), telemetrySettings *component.TelemetrySettings) (*Component[V], error) -} - -func NewMap[K comparable, V component.Component]() Map[K, V] { - return &mapImpl[K, V]{ +func NewMap[K comparable, V component.Component]() *Map[K, V] { + return &Map[K, V]{ components: map[K]*Component[V]{}, } } -type mapImpl[K comparable, V component.Component] struct { +// Map keeps reference of all created instances for a given shared key such as a component configuration. +type Map[K comparable, V component.Component] struct { components map[K]*Component[V] } -func (m *mapImpl[K, V]) LoadOrStore(key K, create func() (V, error), telemetrySettings *component.TelemetrySettings) (*Component[V], error) { +// LoadOrStore returns the already created instance if exists, otherwise creates a new instance +// and adds it to the map of references. +func (m *Map[K, V]) LoadOrStore(key K, create func() (V, error), telemetrySettings *component.TelemetrySettings) (*Component[V], error) { if c, ok := m.components[key]; ok { // If we haven't already seen this telemetry settings, this shared component represents // another instance. Wrap ReportComponentStatus to report for all instances this shared diff --git a/internal/sharedcomponent/sharedcomponent_test.go b/internal/sharedcomponent/sharedcomponent_test.go index 767c7fa02c26..2467fb436871 100644 --- a/internal/sharedcomponent/sharedcomponent_test.go +++ b/internal/sharedcomponent/sharedcomponent_test.go @@ -25,11 +25,11 @@ type baseComponent struct { func TestNewMap(t *testing.T) { comps := NewMap[component.ID, *baseComponent]() - assert.Len(t, comps.(*mapImpl[component.ID, *baseComponent]).components, 0) + assert.Len(t, comps.(*Map[component.ID, *baseComponent]).components, 0) } func TestNewSharedComponentsCreateError(t *testing.T) { - comps := NewMap[component.ID, *baseComponent]().(*mapImpl[component.ID, *baseComponent]) + comps := NewMap[component.ID, *baseComponent]().(*Map[component.ID, *baseComponent]) assert.Len(t, comps.components, 0) myErr := errors.New("my error") _, err := comps.LoadOrStore( @@ -51,7 +51,7 @@ func TestSharedComponentsLoadOrStore(t *testing.T) { newNopTelemetrySettings(), ) require.NoError(t, err) - assert.Len(t, comps.(*mapImpl[component.ID, *baseComponent]).components, 1) + assert.Len(t, comps.(*Map[component.ID, *baseComponent]).components, 1) assert.Same(t, nop, got.Unwrap()) gotSecond, err := comps.LoadOrStore( id, @@ -64,7 +64,7 @@ func TestSharedComponentsLoadOrStore(t *testing.T) { // Shutdown nop will remove assert.NoError(t, got.Shutdown(context.Background())) - assert.Len(t, comps.(*mapImpl[component.ID, *baseComponent]).components, 0) + assert.Len(t, comps.(*Map[component.ID, *baseComponent]).components, 0) gotThird, err := comps.LoadOrStore( id, func() (*baseComponent, error) { return nop, nil }, @@ -141,7 +141,7 @@ func TestSharedComponentsReportStatus(t *testing.T) { telemetrySettings, ) require.NoError(t, err) - assert.Len(t, comps.(*mapImpl[component.ID, *baseComponent]).components, 1) + assert.Len(t, comps.(*Map[component.ID, *baseComponent]).components, 1) assert.Same(t, comp, got.Unwrap()) }