coreapi: name/key functional options

License: MIT
Signed-off-by: Łukasz Magiera <magik6k@gmail.com>


This commit was moved from ipfs/interface-go-ipfs-core@7a786c5509

This commit was moved from ipfs/boxo@c02dc49770
This commit is contained in:
Łukasz Magiera 2017-12-13 19:05:23 +01:00
parent cb3c588799
commit d52ea2fe1a
3 changed files with 169 additions and 4 deletions

View File

@ -99,14 +99,25 @@ type DagAPI interface {
}
type NameAPI interface {
Publish(ctx context.Context, path Path, validTime time.Duration, key string) (*IpnsEntry, error)
Resolve(ctx context.Context, name string, recursive bool, local bool, nocache bool) (Path, error)
Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (*IpnsEntry, error)
WithValidTime(validTime time.Duration) options.NamePublishOption
WithKey(key string) options.NamePublishOption
Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
WithRecursive(recursive bool) options.NameResolveOption
WithLocal(local bool) options.NameResolveOption
WithNoCache(nocache bool) options.NameResolveOption
}
type KeyAPI interface {
Generate(ctx context.Context, name string, algorithm string, size int) (string, error)
Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (string, error)
WithAlgorithm(algorithm string) options.KeyGenerateOption
WithSize(size int) options.KeyGenerateOption
Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (string, bool, error)
WithForce(force bool) options.KeyRenameOption
List(ctx context.Context) (map[string]string, error) //TODO: better key type?
Rename(ctx context.Context, oldName string, newName string, force bool) (string, bool, error)
Remove(ctx context.Context, name string) (string, error)
}

View File

@ -0,0 +1,65 @@
package options
type KeyGenerateSettings struct {
Algorithm string
Size int
}
type KeyRenameSettings struct {
Force bool
}
type KeyGenerateOption func(*KeyGenerateSettings) error
type KeyRenameOption func(*KeyRenameSettings) error
func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
options := &KeyGenerateSettings{
Algorithm: "rsa",
Size: 0,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) {
options := &KeyRenameSettings{
Force: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type KeyOptions struct{}
func (api *KeyOptions) WithAlgorithm(algorithm string) KeyGenerateOption {
return func(settings *KeyGenerateSettings) error {
settings.Algorithm = algorithm
return nil
}
}
func (api *KeyOptions) WithSize(size int) KeyGenerateOption {
return func(settings *KeyGenerateSettings) error {
settings.Size = size
return nil
}
}
func (api *KeyOptions) WithForce(force bool) KeyRenameOption {
return func(settings *KeyRenameSettings) error {
settings.Force = force
return nil
}
}

View File

@ -0,0 +1,89 @@
package options
import (
"time"
)
type NamePublishSettings struct {
ValidTime time.Duration
Key string
}
type NameResolveSettings struct {
Recursive bool
Local bool
Nocache bool
}
type NamePublishOption func(*NamePublishSettings) error
type NameResolveOption func(*NameResolveSettings) error
func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
options := &NamePublishSettings{
ValidTime: 24 * time.Hour,
Key: "self",
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
options := &NameResolveSettings{
Recursive: false,
Local: false,
Nocache: false,
}
for _, opt := range opts {
err := opt(options)
if err != nil {
return nil, err
}
}
return options, nil
}
type NameOptions struct{}
func (api *NameOptions) WithValidTime(validTime time.Duration) NamePublishOption {
return func(settings *NamePublishSettings) error {
settings.ValidTime = validTime
return nil
}
}
func (api *NameOptions) WithKey(key string) NamePublishOption {
return func(settings *NamePublishSettings) error {
settings.Key = key
return nil
}
}
func (api *NameOptions) WithRecursive(recursive bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Recursive = recursive
return nil
}
}
func (api *NameOptions) WithLocal(local bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Local = local
return nil
}
}
func (api *NameOptions) WithNoCache(nocache bool) NameResolveOption {
return func(settings *NameResolveSettings) error {
settings.Nocache = nocache
return nil
}
}