frostfs-api-go/pkg/container/opts.go
Leonard Lyubich 63c8d6e9ea [#128] sdk: Use owner ID instead of NEO3Wallet
Replace owner.NEO3Wallet usage with owner.ID usage. Functions that process
the wallet address convert it to the owner identifier through the public
method.

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
2020-09-18 10:45:11 +03:00

109 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/owner"
)
type (
NewOption interface {
apply(*containerOptions)
}
attribute struct {
key string
value string
}
containerOptions struct {
acl uint32
policy string
attributes []attribute
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 string) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
// todo: make sanity check and store binary structure
option.policy = policy
})
}
func WithAttribute(key, value string) NewOption {
return newFuncContainerOption(func(option *containerOptions) {
attr := attribute{
key: key,
value: value,
}
option.attributes = append(option.attributes, attr)
})
}