Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added strings to streams helper #250

Merged
merged 1 commit into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions graph.go
Original file line number Diff line number Diff line change
Expand Up @@ -384,3 +384,15 @@ func tableName(group Group) string {
func loopName(group Group) string {
return string(group) + loopSuffix
}

// StringsToStreams is a simple cast/conversion functions that allows to pass a slice
// of strings as a slice of Stream (Streams)
// Avoids the boilerplate loop over the string array that would be necessary otherwise.
func StringsToStreams(strings ...string) Streams {
streams := make(Streams, 0, len(strings))

for _, str := range strings {
streams = append(streams, Stream(str))
}
return streams
}
23 changes: 23 additions & 0 deletions graph_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,26 @@ func TestGroupGraph_Inputs(t *testing.T) {
test.AssertEqual(t, topics.Topic(), "a,b,c")
test.AssertTrue(t, strings.Contains(topics.String(), "a,b,c/*codec.String"))
}

func TestStringsToStreams(t *testing.T) {
inputTopics := []string{"input1",
"input2",
}

streams := StringsToStreams(inputTopics...)
test.AssertEqual(t, streams[0], Stream("input1"))
test.AssertEqual(t, streams[1], Stream("input2"))
}

func ExampleStringsToStreams() {
inputTopics := []string{"input1",
"input2",
"input3",
}

// use it, e.g. in the Inputs-Edge in the group graph
graph := DefineGroup("group",
Inputs(StringsToStreams(inputTopics...), new(codec.String), func(ctx Context, msg interface{}) {}),
)
_ = graph
}