[#193] pkg/container: Wrap container structure with SDK functions

All setters and getters should work with SDK types.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
This commit is contained in:
Alex Vanin 2020-11-11 16:49:54 +03:00 committed by Alex Vanin
parent 7894749060
commit f16a420bed
3 changed files with 118 additions and 20 deletions

View file

@ -0,0 +1,56 @@
package container
import (
"github.com/nspcc-dev/neofs-api-go/v2/container"
)
type (
Attribute container.Attribute
Attributes []*Attribute
)
func NewAttribute() *Attribute {
return NewAttributeFromV2(new(container.Attribute))
}
func (a *Attribute) SetKey(v string) {
(*container.Attribute)(a).SetKey(v)
}
func (a *Attribute) SetValue(v string) {
(*container.Attribute)(a).SetValue(v)
}
func (a *Attribute) Key() string {
return (*container.Attribute)(a).GetKey()
}
func (a *Attribute) Value() string {
return (*container.Attribute)(a).GetValue()
}
func NewAttributeFromV2(v *container.Attribute) *Attribute {
return (*Attribute)(v)
}
func (a *Attribute) ToV2() *container.Attribute {
return (*container.Attribute)(a)
}
func NewAttributesFromV2(v []*container.Attribute) Attributes {
attrs := make(Attributes, 0, len(v))
for i := range v {
attrs = append(attrs, NewAttributeFromV2(v[i]))
}
return attrs
}
func (a Attributes) ToV2() []*container.Attribute {
attrs := make([]*container.Attribute, 0, len(a))
for i := range a {
attrs = append(attrs, a[i].ToV2())
}
return attrs
}

View file

@ -3,11 +3,14 @@ package container
import (
"crypto/sha256"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-api-go/v2/container"
)
type Container struct {
container.Container
v2 container.Container
}
func New(opts ...NewOption) *Container {
@ -21,36 +24,27 @@ func New(opts ...NewOption) *Container {
cnr.SetBasicACL(cnrOptions.acl)
if cnrOptions.owner != nil {
cnr.SetOwnerID(cnrOptions.owner.ToV2())
cnr.SetOwnerID(cnrOptions.owner)
}
if cnrOptions.policy != nil {
cnr.SetPlacementPolicy(cnrOptions.policy.ToV2())
cnr.SetPlacementPolicy(cnrOptions.policy)
}
attributes := make([]*container.Attribute, len(cnrOptions.attributes))
for i := range cnrOptions.attributes {
attribute := new(container.Attribute)
attribute.SetKey(cnrOptions.attributes[i].key)
attribute.SetValue(cnrOptions.attributes[i].value)
attributes[i] = attribute
}
if len(attributes) > 0 {
cnr.SetAttributes(attributes)
}
cnr.SetAttributes(cnrOptions.attributes)
return cnr
}
func (c Container) ToV2() *container.Container {
return &c.Container
return &c.v2
}
func NewContainerFromV2(c *container.Container) *Container {
cnr := new(Container)
if c != nil {
cnr.Container = *c
cnr.v2 = *c
}
return cnr
@ -69,3 +63,51 @@ func CalculateID(c *Container) *ID {
return id
}
func (c *Container) Version() *pkg.Version {
return pkg.NewVersionFromV2(c.v2.GetVersion())
}
func (c *Container) SetVersion(v *pkg.Version) {
c.v2.SetVersion(v.ToV2())
}
func (c *Container) OwnerID() *owner.ID {
return owner.NewIDFromV2(c.v2.GetOwnerID())
}
func (c *Container) SetOwnerID(v *owner.ID) {
c.v2.SetOwnerID(v.ToV2())
}
func (c *Container) Nonce() []byte {
return c.v2.GetNonce() // return uuid?
}
func (c *Container) SetNonce(v []byte) {
c.v2.SetNonce(v) // set uuid?
}
func (c *Container) BasicACL() uint32 {
return c.v2.GetBasicACL()
}
func (c *Container) SetBasicACL(v uint32) {
c.v2.SetBasicACL(v)
}
func (c *Container) Attributes() Attributes {
return NewAttributesFromV2(c.v2.GetAttributes())
}
func (c *Container) SetAttributes(v Attributes) {
c.v2.SetAttributes(v.ToV2())
}
func (c *Container) PlacementPolicy() *netmap.PlacementPolicy {
return netmap.NewPlacementPolicyFromV2(c.v2.GetPlacementPolicy())
}
func (c *Container) SetPlacementPolicy(v *netmap.PlacementPolicy) {
c.v2.SetPlacementPolicy(v.ToV2())
}

View file

@ -20,7 +20,7 @@ type (
containerOptions struct {
acl uint32
policy *netmap.PlacementPolicy
attributes []attribute
attributes Attributes
owner *owner.ID
nonce uuid.UUID
}
@ -100,10 +100,10 @@ func WithPolicy(policy *netmap.PlacementPolicy) NewOption {
func WithAttribute(key, value string) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
attr := attribute{
key: key,
value: value,
}
attr := NewAttribute()
attr.SetKey(key)
attr.SetValue(value)
option.attributes = append(option.attributes, attr)
})
}