Skip to content

Commit

Permalink
feat: add RequestParamValue.Bytes
Browse files Browse the repository at this point in the history
  • Loading branch information
aofei committed Nov 16, 2020
1 parent 8abdbf1 commit 1917812
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
10 changes: 10 additions & 0 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,16 @@ func (rpv *RequestParamValue) String() string {
return *rpv.s
}

// Bytes returns a `[]byte` from the underlying value of the rpv. It returns nil
// if the rpv is not text-based.
func (rpv *RequestParamValue) Bytes() []byte {
if s := rpv.String(); s != "" {
return []byte(s)
}

return nil
}

// File returns a `multipart.FileHeader` from the underlying value of the rpv.
func (rpv *RequestParamValue) File() (*multipart.FileHeader, error) {
if rpv.f == nil {
Expand Down
20 changes: 20 additions & 0 deletions request_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -793,6 +793,26 @@ func TestRequestParamValueString(t *testing.T) {
assert.NotNil(t, rpv.s)
}

func TestRequestParamValueBytes(t *testing.T) {
rpv := &RequestParamValue{
i: "foobar",
}
assert.Nil(t, rpv.s)

b := rpv.Bytes()
assert.Equal(t, []byte("foobar"), b)
assert.NotNil(t, rpv.s)

rpv = &RequestParamValue{
i: errors.New("foobar"),
}
assert.Nil(t, rpv.s)

b = rpv.Bytes()
assert.Equal(t, []byte("foobar"), b)
assert.NotNil(t, rpv.s)
}

func TestRequestParamValueFile(t *testing.T) {
rpv := &RequestParamValue{
i: &multipart.FileHeader{},
Expand Down

0 comments on commit 1917812

Please sign in to comment.