Skip to content

Commit

Permalink
ByteStreamProducer-handle-string-type
Browse files Browse the repository at this point in the history
Signed-off-by: Youyuan Wu <[email protected]>
  • Loading branch information
youyuanwu committed Apr 3, 2021
1 parent 6da6fd0 commit ac4d104
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
5 changes: 5 additions & 0 deletions bytestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func ByteStreamProducer(opts ...byteStreamOpt) Producer {
}

if data != nil {
if str, ok := data.(string); ok {
_, err := writer.Write([]byte(str))
return err
}

if e, ok := data.(error); ok {
_, err := writer.Write([]byte(e.Error()))
return err
Expand Down
6 changes: 6 additions & 0 deletions bytestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,12 @@ func TestByteStreamProducer(t *testing.T) {
rdr.Reset()
}

// string can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, expected)) {
assert.Equal(t, expected, rdr.String())
rdr.Reset()
}

// binary slices can also be used to produce
if assert.NoError(t, cons.Produce(&rdr, []byte(expected))) {
assert.Equal(t, expected, rdr.String())
Expand Down

0 comments on commit ac4d104

Please sign in to comment.