Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Handling of binary content in actions and events #355

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions bees/placeholders.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@
package bees

import (
"bytes"
"errors"
"fmt"
"io/ioutil"
"net/url"
"reflect"
"strconv"
Expand Down Expand Up @@ -227,6 +229,80 @@ func ConvertValue(v interface{}, dst interface{}) error {
panic(fmt.Sprintf("Unhandled type %+v for url.Values conversion", reflect.TypeOf(vt)))
}

case *BinaryValue:
switch vt := v.(type) {
case *BinaryValue:
d = vt
case []byte:
if d != nil {
d.MimeType = "unknwown"
d.Data = ioutil.NopCloser(bytes.NewBuffer(vt))
} else {
*d = BinaryValue{
MimeType: "unknown",
Data: ioutil.NopCloser(bytes.NewBuffer(vt)),
}
}

default:
panic(fmt.Sprintf("Unhandled type %+v for *BinaryValue conversion", reflect.TypeOf(vt)))
}

case *[]*BinaryValue:
switch vt := v.(type) {
case []*BinaryValue:
d = &vt
case *[]*BinaryValue:
d = vt
case [][]byte:
bvs := make([]*BinaryValue, len(vt))
for i, b := range vt {
bvs[i] = &BinaryValue{
MimeType: "unknown",
Data: ioutil.NopCloser(bytes.NewBuffer(b)),
}
}
*d = bvs
default:
panic(fmt.Sprintf("Unhandled type %+v for []*BinaryValue conversion", reflect.TypeOf(vt)))
}

case *[]byte:
switch vt := v.(type) {
case []byte:
*d = vt
case *[]byte:
d = vt
case *BinaryValue:
b, err := ioutil.ReadAll(vt.Data)
if err != nil {
panic(fmt.Sprintf("Unhandled error during type conversion for []byte from *BinaryValue: %s", err))
}
*d = b
default:
panic(fmt.Sprintf("Unhandled type %+v for []byte conversion", reflect.TypeOf(vt)))
}

case *[][]byte:
switch vt := v.(type) {
case [][]byte:
d = &vt
case *[][]byte:
d = vt
case []*BinaryValue:
bb := make([][]byte, len(vt))
for _, bv := range vt {
b, err := ioutil.ReadAll(bv.Data)
if err != nil {
panic(fmt.Sprintf("Unhandled error during type conversion for [][]byte from []*BinaryValue: %s", err))
}
bb = append(bb, b)
}
d = &bb
default:
panic(fmt.Sprintf("Unhandled type %+v for [][]byte conversion", reflect.TypeOf(vt)))
}

default:
panic(fmt.Sprintf("Unhandled dst type %+v", reflect.TypeOf(dst)))
}
Expand Down
77 changes: 77 additions & 0 deletions bees/placeholders_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package bees

import (
"bytes"
"io/ioutil"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestBinaryConversions(t *testing.T) {
testCases := []struct {
dst interface{}
v interface{}
name string
assert func(t *testing.T, v interface{}, dst interface{})
}{
{
name: "Test conversion from BinaryValue to []byte",
dst: &[]byte{},
v: &BinaryValue{Data: ioutil.NopCloser(bytes.NewBuffer([]byte{0x00, 0x01, 0xFF}))},
assert: func(t *testing.T, v interface{}, dst interface{}) {
b, ok := dst.(*[]byte)
require.True(t, ok)
assert.Len(t, *b, 3)
},
},

{
name: "Test conversion from []byte to *BinaryValue",
dst: &BinaryValue{},
v: []byte{0x00, 0x01, 0xFF},
assert: func(t *testing.T, v interface{}, dst interface{}) {
b, ok := dst.(*BinaryValue)
require.True(t, ok)
require.NotNil(t, b.Data)
bb, _ := ioutil.ReadAll(b.Data)
assert.Len(t, bb, 3)
},
},

{
name: "Test conversion from [][]byte to *[]*BinaryValue",
dst: &[]*BinaryValue{},
v: [][]byte{{0x00}, {0x01}, {0x02}},
assert: func(t *testing.T, v interface{}, dst interface{}) {
b, ok := dst.(*[]*BinaryValue)
require.True(t, ok)

require.Len(t, *b, 3)
for _, bv := range *b {
bb, _ := ioutil.ReadAll(bv.Data)
assert.Len(t, bb, 1)
}
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
if err, ok := r.(error); ok {
t.Fatalf("Failed to convert values: %s", err)
} else {
t.Fatalf("Failed to convert values: unknown reason")
}
}
}()
ConvertValue(tc.v, tc.dst)
if tc.assert != nil {
tc.assert(t, tc.v, tc.dst)
}
})
}
}
8 changes: 8 additions & 0 deletions bees/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package bees

import "io"

type BinaryValue struct {
MimeType string
Data io.ReadCloser
}
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ require (
github.com/sromku/go-gitter v0.0.0-20170828210750-70f7030a94a6
github.com/ssor/bom v0.0.0-20170718123548-6386211fdfcf // indirect
github.com/stretchr/objx v0.2.0 // indirect
github.com/stretchr/testify v1.4.0
github.com/tomnomnom/linkheader v0.0.0-20180905144013-02ca5825eb80 // indirect
github.com/xrash/smetrics v0.0.0-20170218160415-a3153f7040e9 // indirect
golang.org/x/crypto v0.0.0-20190426145343-a29dc8fdc734
Expand Down