forked from TrueCloudLab/xk6-frostfs
51 lines
1.2 KiB
Go
51 lines
1.2 KiB
Go
|
package datagen
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"crypto/sha256"
|
||
|
"encoding/hex"
|
||
|
"io"
|
||
|
)
|
||
|
|
||
|
// Payload represents arbitrary data to be packed into S3 or native object.
|
||
|
// Implementations could be thread-unsafe.
|
||
|
type Payload interface {
|
||
|
// Reader returns io.Reader instance to read the payload.
|
||
|
// Must not be called twice.
|
||
|
Reader() io.Reader
|
||
|
// Bytes is a helper which reads all data from Reader() into slice.
|
||
|
// The sole purpose of this method is to simplify HTTP scenario,
|
||
|
// where all payload needs to be read and wrapped.
|
||
|
Bytes() []byte
|
||
|
// Size returns payload size, which is equal to the total amount of data
|
||
|
// that could be read from the Reader().
|
||
|
Size() int
|
||
|
// Hash returns payload sha256 hash. Must be called after all data is read from the reader.
|
||
|
Hash() string
|
||
|
}
|
||
|
|
||
|
type bytesPayload struct {
|
||
|
data []byte
|
||
|
}
|
||
|
|
||
|
func (p *bytesPayload) Reader() io.Reader {
|
||
|
return bytes.NewReader(p.data)
|
||
|
}
|
||
|
|
||
|
func (p *bytesPayload) Size() int {
|
||
|
return len(p.data)
|
||
|
}
|
||
|
|
||
|
func (p *bytesPayload) Hash() string {
|
||
|
h := sha256.Sum256(p.data[:])
|
||
|
return hex.EncodeToString(h[:])
|
||
|
}
|
||
|
|
||
|
func (p *bytesPayload) Bytes() []byte {
|
||
|
return p.data
|
||
|
}
|
||
|
|
||
|
func NewFixedPayload(data []byte) Payload {
|
||
|
return &bytesPayload{data: data}
|
||
|
}
|