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@7a786c5509ec924e3cfe2c29b42f76c47ccecbc4 This commit was moved from ipfs/boxo@c02dc49770c2cbbb89c695c8e83bfc1276c6d665
Łukasz Magiera committed
Dec 13, 2017 at 19:05 UTC
d52ea2fe1ae543d162b8bb03dab2ab3c7f18d8a4
3 files changed
+169
-4
core/coreiface/interface.go
+15
-4
@@ -99,14 +99,25 @@ type DagAPI interface {
99
}
100
101
type NameAPI interface {
102
- Publish(ctx context.Context, path Path, validTime time.Duration, key string) (*IpnsEntry, error)
103
- Resolve(ctx context.Context, name string, recursive bool, local bool, nocache bool) (Path, error)
102
+ Publish(ctx context.Context, path Path, opts ...options.NamePublishOption) (*IpnsEntry, error)
103
+ WithValidTime(validTime time.Duration) options.NamePublishOption
104
+ WithKey(key string) options.NamePublishOption
105
+
106
+ Resolve(ctx context.Context, name string, opts ...options.NameResolveOption) (Path, error)
107
+ WithRecursive(recursive bool) options.NameResolveOption
108
+ WithLocal(local bool) options.NameResolveOption
109
+ WithNoCache(nocache bool) options.NameResolveOption
110
}
111
112
type KeyAPI interface {
107
- Generate(ctx context.Context, name string, algorithm string, size int) (string, error)
113
+ Generate(ctx context.Context, name string, opts ...options.KeyGenerateOption) (string, error)
114
+ WithAlgorithm(algorithm string) options.KeyGenerateOption
115
+ WithSize(size int) options.KeyGenerateOption
116
+
117
+ Rename(ctx context.Context, oldName string, newName string, opts ...options.KeyRenameOption) (string, bool, error)
118
+ WithForce(force bool) options.KeyRenameOption
119
+
120
List(ctx context.Context) (map[string]string, error) //TODO: better key type?
109
- Rename(ctx context.Context, oldName string, newName string, force bool) (string, bool, error)
121
Remove(ctx context.Context, name string) (string, error)
122
}
123
core/coreiface/options/key.go
new
+65
@@ -0,0 +1,65 @@
1
+package options
2
+
3
+type KeyGenerateSettings struct {
4
+ Algorithm string
5
+ Size int
6
+}
7
+
8
+type KeyRenameSettings struct {
9
+ Force bool
10
+}
11
+
12
+type KeyGenerateOption func(*KeyGenerateSettings) error
13
+type KeyRenameOption func(*KeyRenameSettings) error
14
+
15
+func KeyGenerateOptions(opts ...KeyGenerateOption) (*KeyGenerateSettings, error) {
16
+ options := &KeyGenerateSettings{
17
+ Algorithm: "rsa",
18
+ Size: 0,
19
+ }
20
+
21
+ for _, opt := range opts {
22
+ err := opt(options)
23
+ if err != nil {
24
+ return nil, err
25
+ }
26
+ }
27
+ return options, nil
28
+}
29
+
30
+func KeyRenameOptions(opts ...KeyRenameOption) (*KeyRenameSettings, error) {
31
+ options := &KeyRenameSettings{
32
+ Force: false,
33
+ }
34
+
35
+ for _, opt := range opts {
36
+ err := opt(options)
37
+ if err != nil {
38
+ return nil, err
39
+ }
40
+ }
41
+ return options, nil
42
+}
43
+
44
+type KeyOptions struct{}
45
+
46
+func (api *KeyOptions) WithAlgorithm(algorithm string) KeyGenerateOption {
47
+ return func(settings *KeyGenerateSettings) error {
48
+ settings.Algorithm = algorithm
49
+ return nil
50
+ }
51
+}
52
+
53
+func (api *KeyOptions) WithSize(size int) KeyGenerateOption {
54
+ return func(settings *KeyGenerateSettings) error {
55
+ settings.Size = size
56
+ return nil
57
+ }
58
+}
59
+
60
+func (api *KeyOptions) WithForce(force bool) KeyRenameOption {
61
+ return func(settings *KeyRenameSettings) error {
62
+ settings.Force = force
63
+ return nil
64
+ }
65
+}
core/coreiface/options/name.go
new
+89
@@ -0,0 +1,89 @@
1
+package options
2
+
3
+import (
4
+ "time"
5
+)
6
+
7
+type NamePublishSettings struct {
8
+ ValidTime time.Duration
9
+ Key string
10
+}
11
+
12
+type NameResolveSettings struct {
13
+ Recursive bool
14
+ Local bool
15
+ Nocache bool
16
+}
17
+
18
+type NamePublishOption func(*NamePublishSettings) error
19
+type NameResolveOption func(*NameResolveSettings) error
20
+
21
+func NamePublishOptions(opts ...NamePublishOption) (*NamePublishSettings, error) {
22
+ options := &NamePublishSettings{
23
+ ValidTime: 24 * time.Hour,
24
+ Key: "self",
25
+ }
26
+
27
+ for _, opt := range opts {
28
+ err := opt(options)
29
+ if err != nil {
30
+ return nil, err
31
+ }
32
+ }
33
+
34
+ return options, nil
35
+}
36
+
37
+func NameResolveOptions(opts ...NameResolveOption) (*NameResolveSettings, error) {
38
+ options := &NameResolveSettings{
39
+ Recursive: false,
40
+ Local: false,
41
+ Nocache: false,
42
+ }
43
+
44
+ for _, opt := range opts {
45
+ err := opt(options)
46
+ if err != nil {
47
+ return nil, err
48
+ }
49
+ }
50
+
51
+ return options, nil
52
+}
53
+
54
+type NameOptions struct{}
55
+
56
+func (api *NameOptions) WithValidTime(validTime time.Duration) NamePublishOption {
57
+ return func(settings *NamePublishSettings) error {
58
+ settings.ValidTime = validTime
59
+ return nil
60
+ }
61
+}
62
+
63
+func (api *NameOptions) WithKey(key string) NamePublishOption {
64
+ return func(settings *NamePublishSettings) error {
65
+ settings.Key = key
66
+ return nil
67
+ }
68
+}
69
+
70
+func (api *NameOptions) WithRecursive(recursive bool) NameResolveOption {
71
+ return func(settings *NameResolveSettings) error {
72
+ settings.Recursive = recursive
73
+ return nil
74
+ }
75
+}
76
+
77
+func (api *NameOptions) WithLocal(local bool) NameResolveOption {
78
+ return func(settings *NameResolveSettings) error {
79
+ settings.Local = local
80
+ return nil
81
+ }
82
+}
83
+
84
+func (api *NameOptions) WithNoCache(nocache bool) NameResolveOption {
85
+ return func(settings *NameResolveSettings) error {
86
+ settings.Nocache = nocache
87
+ return nil
88
+ }
89
+}