Skip to content

Commit

Permalink
remove interface
Browse files Browse the repository at this point in the history
  • Loading branch information
atoulme committed Dec 21, 2023
1 parent eb539b5 commit 5e5d8d7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 16 deletions.
18 changes: 7 additions & 11 deletions internal/sharedcomponent/sharedcomponent.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions internal/sharedcomponent/sharedcomponent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 28 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / windows-unittest

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 28 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 28 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / test-coverage

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 28 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / unittest-matrix (~1.20.12)

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 28 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface
}

func TestNewSharedComponentsCreateError(t *testing.T) {
comps := NewMap[component.ID, *baseComponent]().(*mapImpl[component.ID, *baseComponent])
comps := NewMap[component.ID, *baseComponent]().(*Map[component.ID, *baseComponent])

Check failure on line 32 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / windows-unittest

invalid operation: NewMap[component.ID, *baseComponent]() (value of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 32 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

invalid operation: NewMap[component.ID, *baseComponent]() (value of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 32 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / test-coverage

invalid operation: NewMap[component.ID, *baseComponent]() (value of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 32 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / unittest-matrix (~1.20.12)

invalid operation: NewMap[component.ID, *baseComponent]() (value of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 32 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: NewMap[component.ID, *baseComponent]() (value of type *Map[component.ID, *baseComponent]) is not an interface
assert.Len(t, comps.components, 0)
myErr := errors.New("my error")
_, err := comps.LoadOrStore(
Expand All @@ -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)

Check failure on line 54 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / windows-unittest

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 54 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 54 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / test-coverage

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 54 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / unittest-matrix (~1.20.12)

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 54 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface
assert.Same(t, nop, got.Unwrap())
gotSecond, err := comps.LoadOrStore(
id,
Expand All @@ -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)

Check failure on line 67 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / windows-unittest

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 67 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 67 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / test-coverage

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 67 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / unittest-matrix (~1.20.12)

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 67 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface
gotThird, err := comps.LoadOrStore(
id,
func() (*baseComponent, error) { return nop, nil },
Expand Down Expand Up @@ -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)

Check failure on line 144 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / windows-unittest

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 144 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / CodeQL-Build

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface (typecheck)

Check failure on line 144 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / test-coverage

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 144 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / unittest-matrix (~1.20.12)

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface

Check failure on line 144 in internal/sharedcomponent/sharedcomponent_test.go

View workflow job for this annotation

GitHub Actions / lint

invalid operation: comps (variable of type *Map[component.ID, *baseComponent]) is not an interface (typecheck)
assert.Same(t, comp, got.Unwrap())
}

Expand Down

0 comments on commit 5e5d8d7

Please sign in to comment.