Skip to content

Commit

Permalink
[#907] morph/container: Add native name and zone to PutArgs
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Leonard Lyubich committed Oct 12, 2021
1 parent 2de9c23 commit 51cf791
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 8 deletions.
19 changes: 19 additions & 0 deletions pkg/morph/client/container/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -71,6 +74,8 @@ func defaultConfig() *cfg {
putSizeMethod: defaultPutSizeMethod,
listSizesMethod: defaultListSizesMethod,
getSizeMethod: defaultGetSizeMethod,

putNamedMethod: defaultPutNamedMethod,
}
}

Expand Down Expand Up @@ -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
}
}
}
45 changes: 37 additions & 8 deletions pkg/morph/client/container/put.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 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", c.putMethod, err)
return fmt.Errorf("could not invoke method (%s): %w", method, err)
}

return nil
}

0 comments on commit 51cf791

Please sign in to comment.