[#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 <leonard@nspcc.ru>
remotes/fyrchik/container-alias-fee
Leonard Lyubich 2021-10-12 17:46:45 +03:00 committed by Alex Vanin
parent 36c5e4c527
commit 7db47c88bf
2 changed files with 57 additions and 9 deletions

View File

@ -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
}
}
}

View File

@ -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
}