forked from mostafa/xk6-kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bytearray.go
36 lines (31 loc) · 814 Bytes
/
bytearray.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
package kafka
import "github.com/riferrei/srclient"
type ByteArraySerde struct {
Serdes
}
const (
Bytes srclient.SchemaType = "BYTES"
)
// Serialize serializes the given data into a byte array.
func (*ByteArraySerde) Serialize(data interface{}, schema *Schema) ([]byte, *Xk6KafkaError) {
switch data := data.(type) {
case []byte:
return data, nil
case []interface{}:
arr := make([]byte, len(data))
for i, u := range data {
if u, ok := u.(float64); ok {
arr[i] = byte(u)
} else {
return nil, ErrFailedTypeCast
}
}
return arr, nil
default:
return nil, ErrInvalidDataType
}
}
// DeserializeByteArray returns the data as-is, because it is already a byte array.
func (*ByteArraySerde) Deserialize(data []byte, schema *Schema) (interface{}, *Xk6KafkaError) {
return data, nil
}