[#60] container: rename NewOption and make code more consistent

In all other places we use `Option` as `func`, there is no need in
additional indirection.

Signed-off-by: Evgenii Stratonikov <evgeniy@nspcc.ru>
This commit is contained in:
Evgenii Stratonikov 2021-11-08 15:25:13 +03:00 committed by Alex Vanin
parent 73686827d3
commit 7627f8376a
2 changed files with 27 additions and 43 deletions

View file

@ -32,11 +32,11 @@ type Container struct {
// - attr: nil; // - attr: nil;
// - policy: nil; // - policy: nil;
// - ownerID: nil. // - ownerID: nil.
func New(opts ...NewOption) *Container { func New(opts ...Option) *Container {
cnrOptions := defaultContainerOptions() cnrOptions := defaultContainerOptions()
for i := range opts { for i := range opts {
opts[i].apply(&cnrOptions) opts[i](&cnrOptions)
} }
cnr := new(Container) cnr := new(Container)

View file

@ -8,9 +8,7 @@ import (
) )
type ( type (
NewOption interface { Option func(*containerOptions)
apply(*containerOptions)
}
containerOptions struct { containerOptions struct {
acl uint32 acl uint32
@ -33,72 +31,58 @@ func defaultContainerOptions() containerOptions {
} }
} }
type funcContainerOption struct { func WithPublicBasicACL() Option {
f func(*containerOptions) return func(option *containerOptions) {
} option.acl = acl.PublicBasicRule
func (fco *funcContainerOption) apply(co *containerOptions) {
fco.f(co)
}
func newFuncContainerOption(f func(option *containerOptions)) *funcContainerOption {
return &funcContainerOption{
f: f,
} }
} }
func WithPublicBasicACL() NewOption { func WithReadOnlyBasicACL() Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
option.acl = acl.PublicBasicRule
})
}
func WithReadOnlyBasicACL() NewOption {
return newFuncContainerOption(func(option *containerOptions) {
option.acl = acl.ReadOnlyBasicRule option.acl = acl.ReadOnlyBasicRule
}) }
} }
func WithCustomBasicACL(acl uint32) NewOption { func WithCustomBasicACL(acl uint32) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
option.acl = acl option.acl = acl
}) }
} }
func WithNonce(nonce uuid.UUID) NewOption { func WithNonce(nonce uuid.UUID) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
option.nonce = nonce option.nonce = nonce
}) }
} }
func WithOwnerID(id *owner.ID) NewOption { func WithOwnerID(id *owner.ID) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
option.owner = id option.owner = id
}) }
} }
func WithNEO3Wallet(w *owner.NEO3Wallet) NewOption { func WithNEO3Wallet(w *owner.NEO3Wallet) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
if option.owner == nil { if option.owner == nil {
option.owner = new(owner.ID) option.owner = new(owner.ID)
} }
option.owner.SetNeo3Wallet(w) option.owner.SetNeo3Wallet(w)
}) }
} }
func WithPolicy(policy *netmap.PlacementPolicy) NewOption { func WithPolicy(policy *netmap.PlacementPolicy) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
option.policy = policy option.policy = policy
}) }
} }
func WithAttribute(key, value string) NewOption { func WithAttribute(key, value string) Option {
return newFuncContainerOption(func(option *containerOptions) { return func(option *containerOptions) {
attr := NewAttribute() attr := NewAttribute()
attr.SetKey(key) attr.SetKey(key)
attr.SetValue(value) attr.SetValue(value)
option.attributes = append(option.attributes, attr) option.attributes = append(option.attributes, attr)
}) }
} }