coreapi WithOptions: apply on top of parent options
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
Łukasz Magiera committed
Dec 11, 2018 at 22:24 UTC
47a46393cf0dad35d36e1f7ba0f815604b0c4bef
2 files changed
+18
-6
core/coreapi/coreapi.go
+12
-4
@@ -73,12 +73,18 @@ type CoreAPI struct {
73
isPublishAllowed func() error
74
75
// ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
76
- nd *core.IpfsNode
76
+ nd *core.IpfsNode
77
+ parentOpts options.ApiSettings
78
}
79
80
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
81
func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI, error) {
81
- return (&CoreAPI{nd: n}).WithOptions(opts...)
82
+ parentOpts, err := options.ApiOptions()
83
+ if err != nil {
84
+ return nil, err
85
+ }
86
+
87
+ return (&CoreAPI{nd: n, parentOpts: *parentOpts}).WithOptions(opts...)
88
}
89
90
// Unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
@@ -133,7 +139,8 @@ func (api *CoreAPI) PubSub() coreiface.PubSubAPI {
139
140
// WithOptions returns api with global options applied
141
func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, error) {
136
- settings, err := options.ApiOptions(opts...)
142
+ settings := api.parentOpts // make sure to copy
143
+ _, err := options.ApiOptionsTo(&settings, opts...)
144
if err != nil {
145
return nil, err
146
}
@@ -166,7 +173,8 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
173
174
pubSub: n.PubSub,
175
169
- nd: n,
176
+ nd: n,
177
+ parentOpts: settings,
178
}
179
180
subApi.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
core/coreapi/interface/options/global.go
+6
-2
@@ -11,6 +11,10 @@ func ApiOptions(opts ...ApiOption) (*ApiSettings, error) {
11
Offline: false,
12
}
13
14
+ return ApiOptionsTo(options, opts...)
15
+}
16
+
17
+func ApiOptionsTo(options *ApiSettings, opts ...ApiOption) (*ApiSettings, error) {
18
for _, opt := range opts {
19
err := opt(options)
20
if err != nil {
@@ -22,9 +26,9 @@ func ApiOptions(opts ...ApiOption) (*ApiSettings, error) {
26
27
type apiOpts struct{}
28
25
-var Api dagOpts
29
+var Api apiOpts
30
27
-func (dagOpts) Offline(offline bool) ApiOption {
31
+func (apiOpts) Offline(offline bool) ApiOption {
32
return func(settings *ApiSettings) error {
33
settings.Offline = offline
34
return nil