Skip to content

Commit

Permalink
Add connector.MakeFactoryMap (#6889)
Browse files Browse the repository at this point in the history
  • Loading branch information
djaglowski authored Jan 5, 2023
1 parent d8c9f24 commit 1f3c5fb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .chloggen/connector-factory-map.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# One of 'breaking', 'deprecation', 'new_component', 'enhancement', 'bug_fix'
change_type: enhancement

# The name of the component, or a single word describing the area of concern, (e.g. otlpreceiver)
component: connector

# A brief description of the change. Surround your text with quotes ("") if it needs to start with a backtick (`).
note: Add MakeFactoryMap

# One or more tracking issues or pull requests related to the change
issues: [6889]

# (Optional) One or more lines of additional information to render under the primary note.
# These lines will be padded with 2 spaces and then inserted directly into the document.
# Use pipe (|) for multiline entries.
subtext:
13 changes: 13 additions & 0 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,3 +453,16 @@ func NewFactory(cfgType component.Type, createDefaultConfig component.CreateDefa
}
return f
}

// MakeFactoryMap takes a list of connector factories and returns a map with factory type as keys.
// It returns a non-nil error when there are factories with duplicate type.
func MakeFactoryMap(factories ...Factory) (map[component.Type]Factory, error) {
fMap := map[component.Type]Factory{}
for _, f := range factories {
if _, ok := fMap[f.Type()]; ok {
return fMap, fmt.Errorf("duplicate connector factory %q", f.Type())
}
fMap[f.Type()] = f
}
return fMap, nil
}
38 changes: 38 additions & 0 deletions connector/connector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,44 @@ func TestNewFactoryWithAllTypes(t *testing.T) {
assert.NoError(t, err)
}

func TestMakeFactoryMap(t *testing.T) {
type testCase struct {
name string
in []Factory
out map[component.Type]Factory
}

p1 := NewFactory("p1", nil)
p2 := NewFactory("p2", nil)
testCases := []testCase{
{
name: "different names",
in: []Factory{p1, p2},
out: map[component.Type]Factory{
p1.Type(): p1,
p2.Type(): p2,
},
},
{
name: "same name",
in: []Factory{p1, p2, NewFactory("p1", nil)},
},
}

for i := range testCases {
tt := testCases[i]
t.Run(tt.name, func(t *testing.T) {
out, err := MakeFactoryMap(tt.in...)
if tt.out == nil {
assert.Error(t, err)
return
}
assert.NoError(t, err)
assert.Equal(t, tt.out, out)
})
}
}

func createTracesToTraces(context.Context, CreateSettings, component.Config, consumer.Traces) (Traces, error) {
return nil, nil
}
Expand Down

0 comments on commit 1f3c5fb

Please sign in to comment.