Skip to content

Commit

Permalink
fix(extensions): propagate extensions to Collection values
Browse files Browse the repository at this point in the history
Ensure that when RegisterExtensions() is called on a non-empty
collection, new extensions are propagated to the collection's existing
values.

Signed-off-by: Sergei Trofimov <[email protected]>
  • Loading branch information
setrofim committed Aug 6, 2024
1 parent 290f46a commit 9d53459
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
9 changes: 9 additions & 0 deletions extensions/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,15 @@ func (o *Collection[P, I]) RegisterExtensions(exts Map) error {

o.valueExtensions.Set(exts)

for i := 0; i < len(o.Values); i++ {
var vi I = &o.Values[i]
if vi.GetExtensions() == nil {
if err := vi.RegisterExtensions(exts); err != nil {
return fmt.Errorf("error at index %d: %w", i, err)
}
}
}

return nil
}

Expand Down
12 changes: 12 additions & 0 deletions extensions/collection_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,18 @@ func Test_Collection_GetExtensions(t *testing.T) {
assert.NotNil(t, c.GetExtensions())
}

func Test_Collection_RegisterExtensions(t *testing.T) {
c := NewCollection[testExtensible]()

e := testExtensible{}
c.Add(&e)

err := c.RegisterExtensions(Map{testPoint: &testExtensions{}})
require.NoError(t, err)

assert.NotNil(t, c.Values[0].GetExtensions())
}

func Test_Map(t *testing.T) {
m := NewMap()
assert.Equal(t, 0, len(m))
Expand Down

0 comments on commit 9d53459

Please sign in to comment.