forked from TrueCloudLab/frostfs-sdk-go
[#64] transformer: Simplify interface
Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
This commit is contained in:
parent
d70ef2187b
commit
c359a7465a
6 changed files with 52 additions and 112 deletions
|
@ -4,47 +4,25 @@ import (
|
|||
"context"
|
||||
|
||||
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
||||
"github.com/nspcc-dev/neo-go/pkg/util/slice"
|
||||
)
|
||||
|
||||
type chanTarget struct {
|
||||
header *objectSDK.Object
|
||||
payload []byte
|
||||
ch chan<- *objectSDK.Object
|
||||
ch chan<- *objectSDK.Object
|
||||
}
|
||||
|
||||
// NewChannelTarget returns ObjectTarget which writes
|
||||
// object parts to a provided channel.
|
||||
func NewChannelTarget(ch chan<- *objectSDK.Object) ObjectTarget {
|
||||
func NewChannelTarget(ch chan<- *objectSDK.Object) ObjectWriter {
|
||||
return &chanTarget{
|
||||
ch: ch,
|
||||
}
|
||||
}
|
||||
|
||||
// WriteHeader implements the ObjectTarget interface.
|
||||
func (c *chanTarget) WriteHeader(_ context.Context, object *objectSDK.Object) error {
|
||||
c.header = object
|
||||
func (c *chanTarget) WriteObject(ctx context.Context, o *objectSDK.Object) error {
|
||||
select {
|
||||
case c.ch <- o:
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Write implements the ObjectTarget interface.
|
||||
func (c *chanTarget) Write(_ context.Context, p []byte) (n int, err error) {
|
||||
c.payload = append(c.payload, p...)
|
||||
return len(p), nil
|
||||
}
|
||||
|
||||
// Close implements the ObjectTarget interface.
|
||||
func (c *chanTarget) Close(ctx context.Context) (*AccessIdentifiers, error) {
|
||||
if len(c.payload) != 0 {
|
||||
c.header.SetPayload(slice.Copy(c.payload))
|
||||
}
|
||||
select {
|
||||
case c.ch <- c.header:
|
||||
case <-ctx.Done():
|
||||
return nil, ctx.Err()
|
||||
}
|
||||
|
||||
c.header = nil
|
||||
c.payload = nil
|
||||
return new(AccessIdentifiers), nil
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue