[#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:
parent
73686827d3
commit
7627f8376a
2 changed files with 27 additions and 43 deletions
|
@ -32,11 +32,11 @@ type Container struct {
|
|||
// - attr: nil;
|
||||
// - policy: nil;
|
||||
// - ownerID: nil.
|
||||
func New(opts ...NewOption) *Container {
|
||||
func New(opts ...Option) *Container {
|
||||
cnrOptions := defaultContainerOptions()
|
||||
|
||||
for i := range opts {
|
||||
opts[i].apply(&cnrOptions)
|
||||
opts[i](&cnrOptions)
|
||||
}
|
||||
|
||||
cnr := new(Container)
|
||||
|
|
|
@ -8,9 +8,7 @@ import (
|
|||
)
|
||||
|
||||
type (
|
||||
NewOption interface {
|
||||
apply(*containerOptions)
|
||||
}
|
||||
Option func(*containerOptions)
|
||||
|
||||
containerOptions struct {
|
||||
acl uint32
|
||||
|
@ -33,72 +31,58 @@ func defaultContainerOptions() containerOptions {
|
|||
}
|
||||
}
|
||||
|
||||
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() Option {
|
||||
return func(option *containerOptions) {
|
||||
option.acl = acl.PublicBasicRule
|
||||
}
|
||||
}
|
||||
|
||||
func WithPublicBasicACL() NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
option.acl = acl.PublicBasicRule
|
||||
})
|
||||
}
|
||||
|
||||
func WithReadOnlyBasicACL() NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithReadOnlyBasicACL() Option {
|
||||
return func(option *containerOptions) {
|
||||
option.acl = acl.ReadOnlyBasicRule
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WithCustomBasicACL(acl uint32) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithCustomBasicACL(acl uint32) Option {
|
||||
return func(option *containerOptions) {
|
||||
option.acl = acl
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WithNonce(nonce uuid.UUID) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithNonce(nonce uuid.UUID) Option {
|
||||
return func(option *containerOptions) {
|
||||
option.nonce = nonce
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WithOwnerID(id *owner.ID) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithOwnerID(id *owner.ID) Option {
|
||||
return func(option *containerOptions) {
|
||||
option.owner = id
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WithNEO3Wallet(w *owner.NEO3Wallet) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithNEO3Wallet(w *owner.NEO3Wallet) Option {
|
||||
return 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) {
|
||||
func WithPolicy(policy *netmap.PlacementPolicy) Option {
|
||||
return func(option *containerOptions) {
|
||||
option.policy = policy
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func WithAttribute(key, value string) NewOption {
|
||||
return newFuncContainerOption(func(option *containerOptions) {
|
||||
func WithAttribute(key, value string) Option {
|
||||
return func(option *containerOptions) {
|
||||
attr := NewAttribute()
|
||||
attr.SetKey(key)
|
||||
attr.SetValue(value)
|
||||
|
||||
option.attributes = append(option.attributes, attr)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue