提交 47a46393 作者: Łukasz Magiera

coreapi WithOptions: apply on top of parent options

License: MIT
Signed-off-by: 's avatarŁukasz Magiera <magik6k@gmail.com>
上级 2c2f9f2b
......@@ -73,12 +73,18 @@ type CoreAPI struct {
isPublishAllowed func() error
// ONLY for re-applying options in WithOptions, DO NOT USE ANYWHERE ELSE
nd *core.IpfsNode
nd *core.IpfsNode
parentOpts options.ApiSettings
}
// NewCoreAPI creates new instance of IPFS CoreAPI backed by go-ipfs Node.
func NewCoreAPI(n *core.IpfsNode, opts ...options.ApiOption) (coreiface.CoreAPI, error) {
return (&CoreAPI{nd: n}).WithOptions(opts...)
parentOpts, err := options.ApiOptions()
if err != nil {
return nil, err
}
return (&CoreAPI{nd: n, parentOpts: *parentOpts}).WithOptions(opts...)
}
// Unixfs returns the UnixfsAPI interface implementation backed by the go-ipfs node
......@@ -133,7 +139,8 @@ func (api *CoreAPI) PubSub() coreiface.PubSubAPI {
// WithOptions returns api with global options applied
func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, error) {
settings, err := options.ApiOptions(opts...)
settings := api.parentOpts // make sure to copy
_, err := options.ApiOptionsTo(&settings, opts...)
if err != nil {
return nil, err
}
......@@ -166,7 +173,8 @@ func (api *CoreAPI) WithOptions(opts ...options.ApiOption) (coreiface.CoreAPI, e
pubSub: n.PubSub,
nd: n,
nd: n,
parentOpts: settings,
}
subApi.routing = func(allowOffline bool) (routing.IpfsRouting, error) {
......
......@@ -11,6 +11,10 @@ func ApiOptions(opts ...ApiOption) (*ApiSettings, error) {
Offline: false,
}
return ApiOptionsTo(options, opts...)
}
func ApiOptionsTo(options *ApiSettings, opts ...ApiOption) (*ApiSettings, error) {
for _, opt := range opts {
err := opt(options)
if err != nil {
......@@ -22,9 +26,9 @@ func ApiOptions(opts ...ApiOption) (*ApiSettings, error) {
type apiOpts struct{}
var Api dagOpts
var Api apiOpts
func (dagOpts) Offline(offline bool) ApiOption {
func (apiOpts) Offline(offline bool) ApiOption {
return func(settings *ApiSettings) error {
settings.Offline = offline
return nil
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论