From 7db47c88bf33b1c0e67e21ea790618c3c5d3f558 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 12 Oct 2021 17:46:45 +0300 Subject: [PATCH] [#907] morph/container: Add native name and zone to PutArgs Add `PutArgs.SetNativeNameWithZone` method which sets native name and zone for container. Call `putNamed` method of Container contract if name is set, otherwise call `put` method. Signed-off-by: Leonard Lyubich --- pkg/morph/client/container/client.go | 19 +++++++++++ pkg/morph/client/container/put.go | 47 ++++++++++++++++++++++------ 2 files changed, 57 insertions(+), 9 deletions(-) diff --git a/pkg/morph/client/container/client.go b/pkg/morph/client/container/client.go index 3c068d69..7fc51852 100644 --- a/pkg/morph/client/container/client.go +++ b/pkg/morph/client/container/client.go @@ -29,6 +29,7 @@ type Option func(*cfg) type cfg struct { putMethod, // put container method name for invocation + putNamedMethod, // put named container method name for invocation putSizeMethod, // put container size method name for invocation listSizesMethod, // list container sizes method name for invocation getSizeMethod, // get container size method name for invocation @@ -55,6 +56,8 @@ const ( defaultPutSizeMethod = "putContainerSize" // default "put container size" method name defaultListSizesMethod = "listContainerSizes" // default "list container sizes" method name defaultGetSizeMethod = "getContainerSize" // default "get container size" method name + + defaultPutNamedMethod = "putNamed" // default put named container method name ) func defaultConfig() *cfg { @@ -71,6 +74,8 @@ func defaultConfig() *cfg { putSizeMethod: defaultPutSizeMethod, listSizesMethod: defaultListSizesMethod, getSizeMethod: defaultGetSizeMethod, + + putNamedMethod: defaultPutNamedMethod, } } @@ -270,3 +275,17 @@ func WithGetSizeMethod(n string) Option { } } } + +// WithPutNamedMethod returns a client constructor option that +// specifies the method name of "put named container" operation. +// +// Ignores empty value. +// +// If option not provided, "putNamed" is used. +func WithPutNamedMethod(n string) Option { + return func(c *cfg) { + if n != "" { + c.putNamedMethod = n + } + } +} diff --git a/pkg/morph/client/container/put.go b/pkg/morph/client/container/put.go index 5852dbb0..85cd1828 100644 --- a/pkg/morph/client/container/put.go +++ b/pkg/morph/client/container/put.go @@ -14,6 +14,8 @@ type PutArgs struct { publicKey []byte // public key of container owner token []byte // binary session token + + name, zone string // native name and zone } // SetPublicKey sets the public key of container owner @@ -41,19 +43,46 @@ func (p *PutArgs) SetSessionToken(v []byte) { p.token = v } -// Put invokes the call of put container method +// SetNativeNameWithZone sets container native name and its zone. +func (p *PutArgs) SetNativeNameWithZone(name, zone string) { + p.name, p.zone = name, zone +} + +// Put invokes the call of put (named if name is set) container method // of NeoFS Container contract. func (c *Client) Put(args PutArgs) error { - err := c.client.Invoke( - c.putMethod, - args.cnr, - args.sig, - args.publicKey, - args.token, + var ( + err error + method string ) - if err != nil { - return fmt.Errorf("could not invoke method (%s): %w", c.putMethod, err) + if args.name != "" { + err = c.client.Invoke( + c.putNamedMethod, + args.cnr, + args.sig, + args.publicKey, + args.token, + args.name, + args.zone, + ) + + method = c.putNamedMethod + } else { + err = c.client.Invoke( + c.putMethod, + args.cnr, + args.sig, + args.publicKey, + args.token, + ) + + method = c.putMethod } + + if err != nil { + return fmt.Errorf("could not invoke method (%s): %w", method, err) + } + return nil }