Skip to content

Commit

Permalink
Merge pull request #198 from go-openapi/ByteStreamProducer-handle-str…
Browse files Browse the repository at this point in the history
…ing-type

ByteStreamProducer-handle-string-type
  • Loading branch information
casualjim authored Apr 3, 2021
2 parents 6da6fd0 + ac4d104 commit 2ad8938
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 2ad8938

Please sign in to comment.