[#60] object/transformer: Group parameters of NewFormatTarget func

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2020-10-21 18:10:22 +03:00 committed by Alex Vanin
parent 2541ed4b8f
commit 4a56f82571
2 changed files with 23 additions and 16 deletions

View file

@ -69,7 +69,11 @@ func (p *Streamer) initTarget(prm *PutInitPrm) error {
p.target = transformer.NewPayloadSizeLimiter( p.target = transformer.NewPayloadSizeLimiter(
p.maxSizeSrc.MaxObjectSize(), p.maxSizeSrc.MaxObjectSize(),
func() transformer.ObjectTarget { func() transformer.ObjectTarget {
return transformer.NewFormatTarget(sessionKey, p.newCommonTarget(prm), sToken) return transformer.NewFormatTarget(&transformer.FormatterParams{
Key: sessionKey,
NextTarget: p.newCommonTarget(prm),
SessionToken: sToken,
})
}, },
) )

View file

@ -11,15 +11,20 @@ import (
) )
type formatter struct { type formatter struct {
nextTarget ObjectTarget prm *FormatterParams
key *ecdsa.PrivateKey
obj *object.RawObject obj *object.RawObject
sz uint64 sz uint64
}
token *token.SessionToken // FormatterParams groups NewFormatTarget parameters.
type FormatterParams struct {
Key *ecdsa.PrivateKey
NextTarget ObjectTarget
SessionToken *token.SessionToken
} }
// NewFormatTarget returns ObjectTarget instance that finalizes object structure // NewFormatTarget returns ObjectTarget instance that finalizes object structure
@ -32,11 +37,9 @@ type formatter struct {
// - sets payload size to the total length of all written chunks; // - sets payload size to the total length of all written chunks;
// - sets session token; // - sets session token;
// - calculates and sets verification fields (ID, Signature). // - calculates and sets verification fields (ID, Signature).
func NewFormatTarget(key *ecdsa.PrivateKey, nextTarget ObjectTarget, token *token.SessionToken) ObjectTarget { func NewFormatTarget(p *FormatterParams) ObjectTarget {
return &formatter{ return &formatter{
nextTarget: nextTarget, prm: p,
key: key,
token: token,
} }
} }
@ -47,7 +50,7 @@ func (f *formatter) WriteHeader(obj *object.RawObject) error {
} }
func (f *formatter) Write(p []byte) (n int, err error) { func (f *formatter) Write(p []byte) (n int, err error) {
n, err = f.nextTarget.Write(p) n, err = f.prm.NextTarget.Write(p)
f.sz += uint64(n) f.sz += uint64(n)
@ -57,16 +60,16 @@ func (f *formatter) Write(p []byte) (n int, err error) {
func (f *formatter) Close() (*AccessIdentifiers, error) { func (f *formatter) Close() (*AccessIdentifiers, error) {
f.obj.SetVersion(pkg.SDKVersion()) f.obj.SetVersion(pkg.SDKVersion())
f.obj.SetPayloadSize(f.sz) f.obj.SetPayloadSize(f.sz)
f.obj.SetSessionToken(f.token) f.obj.SetSessionToken(f.prm.SessionToken)
var parID *objectSDK.ID var parID *objectSDK.ID
if par := f.obj.GetParent(); par != nil { if par := f.obj.GetParent(); par != nil {
rawPar := objectSDK.NewRawFromV2(par.ToV2()) rawPar := objectSDK.NewRawFromV2(par.ToV2())
rawPar.SetSessionToken(f.token) rawPar.SetSessionToken(f.prm.SessionToken)
if err := objectSDK.SetIDWithSignature(f.key, rawPar); err != nil { if err := objectSDK.SetIDWithSignature(f.prm.Key, rawPar); err != nil {
return nil, errors.Wrap(err, "could not finalize parent object") return nil, errors.Wrap(err, "could not finalize parent object")
} }
@ -75,15 +78,15 @@ func (f *formatter) Close() (*AccessIdentifiers, error) {
f.obj.SetParent(rawPar.Object()) f.obj.SetParent(rawPar.Object())
} }
if err := objectSDK.SetIDWithSignature(f.key, f.obj.SDK()); err != nil { if err := objectSDK.SetIDWithSignature(f.prm.Key, f.obj.SDK()); err != nil {
return nil, errors.Wrap(err, "could not finalize object") return nil, errors.Wrap(err, "could not finalize object")
} }
if err := f.nextTarget.WriteHeader(f.obj); err != nil { if err := f.prm.NextTarget.WriteHeader(f.obj); err != nil {
return nil, errors.Wrap(err, "could not write header to next target") return nil, errors.Wrap(err, "could not write header to next target")
} }
if _, err := f.nextTarget.Close(); err != nil { if _, err := f.prm.NextTarget.Close(); err != nil {
return nil, errors.Wrap(err, "could not close next target") return nil, errors.Wrap(err, "could not close next target")
} }