Skip to content

Commit

Permalink
Merge pull request #250 from lovoo/streams-helper
Browse files Browse the repository at this point in the history
added strings to streams helper
  • Loading branch information
frairon authored Apr 16, 2020
2 parents 1eaa22f + b281011 commit 21558b3
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
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
}

0 comments on commit 21558b3

Please sign in to comment.