From f16a420bed509e129b65a14fc0751ffb850a4752 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Wed, 11 Nov 2020 16:49:54 +0300 Subject: [PATCH] [#193] pkg/container: Wrap container structure with SDK functions All setters and getters should work with SDK types. Signed-off-by: Alex Vanin --- pkg/container/attribute.go | 56 +++++++++++++++++++++++++++++ pkg/container/container.go | 72 ++++++++++++++++++++++++++++++-------- pkg/container/opts.go | 10 +++--- 3 files changed, 118 insertions(+), 20 deletions(-) create mode 100644 pkg/container/attribute.go diff --git a/pkg/container/attribute.go b/pkg/container/attribute.go new file mode 100644 index 0000000..6f72c4c --- /dev/null +++ b/pkg/container/attribute.go @@ -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 +} diff --git a/pkg/container/container.go b/pkg/container/container.go index df6732f..3d64633 100644 --- a/pkg/container/container.go +++ b/pkg/container/container.go @@ -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()) +} diff --git a/pkg/container/opts.go b/pkg/container/opts.go index 106f52b..424891d 100644 --- a/pkg/container/opts.go +++ b/pkg/container/opts.go @@ -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) }) }