From 7627f8376ab0a3cf0d3d4b8927ad90d641b4acb2 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Mon, 8 Nov 2021 15:25:13 +0300 Subject: [PATCH] [#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 --- container/container.go | 4 +-- container/opts.go | 66 ++++++++++++++++-------------------------- 2 files changed, 27 insertions(+), 43 deletions(-) diff --git a/container/container.go b/container/container.go index f1660413..02e5320c 100644 --- a/container/container.go +++ b/container/container.go @@ -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) diff --git a/container/opts.go b/container/opts.go index 05db06b7..d9ac6671 100644 --- a/container/opts.go +++ b/container/opts.go @@ -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) - }) + } }