frostfs-api-go/pkg/container/opts.go
Pavel Karpy 0206bd9a5d [#302] pkg/container: Convert nil Attributes to nil message
Make `Attributes.ToV2` method to return `nil`
when called on `nil`. Make `NewAttributesFromV2`
function to return `nil` when called on `nil`.
Write corresponding unit tests.

Signed-off-by: Pavel Karpy <carpawell@nspcc.ru>
2021-06-09 17:23:35 +03:00

104 lines
2.2 KiB
Go

package container
import (
"github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/pkg/acl"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
)
type (
NewOption interface {
apply(*containerOptions)
}
containerOptions struct {
acl uint32
policy *netmap.PlacementPolicy
attributes Attributes
owner *owner.ID
nonce uuid.UUID
}
)
func defaultContainerOptions() containerOptions {
rand, err := uuid.NewRandom()
if err != nil {
panic("can't create new random " + err.Error())
}
return containerOptions{
acl: acl.PrivateBasicRule,
nonce: rand,
}
}
type funcContainerOption struct {
f func(*containerOptions)
}
func (fco *funcContainerOption) apply(co *containerOptions) {
fco.f(co)
}
func newFuncContainerOption(f func(option *containerOptions)) *funcContainerOption {
return &funcContainerOption{
f: f,
}
}
func WithPublicBasicACL() NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.acl = acl.PublicBasicRule
})
}
func WithReadOnlyBasicACL() NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.acl = acl.ReadOnlyBasicRule
})
}
func WithCustomBasicACL(acl uint32) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.acl = acl
})
}
func WithNonce(nonce uuid.UUID) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.nonce = nonce
})
}
func WithOwnerID(id *owner.ID) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.owner = id
})
}
func WithNEO3Wallet(w *owner.NEO3Wallet) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
if option.owner == nil {
option.owner = new(owner.ID)
}
option.owner.SetNeo3Wallet(w)
})
}
func WithPolicy(policy *netmap.PlacementPolicy) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.policy = policy
})
}
func WithAttribute(key, value string) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
attr := NewAttribute()
attr.SetKey(key)
attr.SetValue(value)
option.attributes = append(option.attributes, attr)
})
}