Skip to content

Commit

Permalink
Merge pull request #229 from lexis-project/master
Browse files Browse the repository at this point in the history
Support string* in ByteStreamConsumer.
  • Loading branch information
casualjim committed Nov 12, 2021
2 parents 0e1252f + 36a1a97 commit 23bff48
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
7 changes: 7 additions & 0 deletions bytestream.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ func ByteStreamConsumer(opts ...byteStreamOpt) Consumer {
return bu.UnmarshalBinary(b)
}

if data != nil {
if str, ok := data.(*string); ok {
*str = string(b)
return nil
}
}

if t := reflect.TypeOf(data); data != nil && t.Kind() == reflect.Ptr {
v := reflect.Indirect(reflect.ValueOf(data))
if t = v.Type(); t.Kind() == reflect.Slice && t.Elem().Kind() == reflect.Uint8 {
Expand Down
6 changes: 6 additions & 0 deletions bytestream_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ func TestByteStreamConsumer(t *testing.T) {
assert.Equal(t, expected, b.String())
}

//can consume as a string
var s string
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &s)) {
assert.Equal(t, expected, s)
}

// can consume as an UnmarshalBinary
var bu binaryUnmarshalDummy
if assert.NoError(t, cons.Consume(bytes.NewBufferString(expected), &bu)) {
Expand Down

0 comments on commit 23bff48

Please sign in to comment.