[#142] sdk/container: Add converters to and from v2

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-09-15 11:04:36 +03:00 committed by Stanislav Bogatyrev
parent 012cea1add
commit 3c7c363002
2 changed files with 22 additions and 10 deletions

View file

@ -100,7 +100,7 @@ func (c Client) putContainerV2(ctx context.Context, cnr *container.Container, op
}
reqBody := new(v2container.PutRequestBody)
reqBody.SetContainer(&cnr.Container)
reqBody.SetContainer(cnr.ToV2())
// sign container
signWrapper := v2signature.StableMarshalerWrapper{SM: reqBody.GetContainer()}
@ -182,9 +182,7 @@ func (c Client) getContainerV2(ctx context.Context, id *container.ID, opts ...Ca
return nil, errors.Wrap(err, "can't verify response message")
}
return &container.Container{
Container: *resp.GetBody().GetContainer(),
}, nil
return container.NewContainerFromV2(resp.GetBody().GetContainer()), nil
default:
return nil, unsupportedProtocolErr
}

View file

@ -8,7 +8,7 @@ type Container struct {
container.Container
}
func New(opts ...NewOption) (*Container, error) {
func New(opts ...NewOption) *Container {
cnrOptions := defaultContainerOptions()
for i := range opts {
opts[i].apply(&cnrOptions)
@ -18,14 +18,14 @@ func New(opts ...NewOption) (*Container, error) {
cnr.SetNonce(cnrOptions.nonce[:])
cnr.SetBasicACL(cnrOptions.acl)
if cnrOptions.policy != "" {
// todo: set placement policy
}
if cnrOptions.owner != nil {
cnr.SetOwnerID(cnrOptions.owner.ToV2())
}
if cnrOptions.policy != nil {
cnr.SetPlacementPolicy(cnrOptions.policy)
}
attributes := make([]*container.Attribute, len(cnrOptions.attributes))
for i := range cnrOptions.attributes {
attribute := new(container.Attribute)
@ -37,5 +37,19 @@ func New(opts ...NewOption) (*Container, error) {
cnr.SetAttributes(attributes)
}
return cnr, nil
return cnr
}
func (c Container) ToV2() *container.Container {
return &c.Container
}
func NewContainerFromV2(c *container.Container) *Container {
cnr := new(Container)
if c != nil {
cnr.Container = *c
}
return cnr
}