-
-
Notifications
You must be signed in to change notification settings - Fork 3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
commands: switch object to CoreAPI #4643
Conversation
core/coreapi/interface/interface.go
Outdated
|
||
// WithResolve is an option for ParsePath which when set to true tells | ||
// ParsePath to also resolve the path | ||
WithResolve(bool) options.ParsePathOption |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm wondering if I should make it default to true. cc @lgierth
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should definitely not be a method on the CoreAPI interface (just a function, or maybe just a type type WithResolve bool
). However, I notice we're already doing this all over the place. Any reason for it?
Anyways, do we ever actually need ResolvePath
to resolve? Doesn't everything that takes a path do resolution internally just in case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's based on a concept called 'functional options' from this article.
TL;DR - this pattern let's us expand the options without breaking anything, it might be overkill for some methods, but I prefer consistency more. But yeah, a separate method for resolving string path makes sense too
Anyways, do we ever actually need ResolvePath to resolve? Doesn't everything that takes a path do resolution internally just in case?
The problem is that paths are (currently) immutable and if user wanted to re-use the resolved path (ipns can be slow), we'd need to return the resolved path, which would result in calls to some methods getting a bit messy.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Having an explicit resolve will also make go-ipfs-api
implementation simpler.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's based on a concept called 'functional options' from this article.
I like the pattern, I'm just objecting to attaching these functions to the interface itself instead of leaving them as free functions. It actually goes against the spirit of the pattern as it makes it harder to grow the API: adding new options will break existing implementations of the CoreAPI interface.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The problem is that paths are (currently) immutable and if user wanted to re-use the resolved path (ipns can be slow), we'd need to return the resolved path, which would result in calls to some methods getting a bit messy.
Fair enough. However, in this case (and many others) we know that we're only going to use the path once (as far as I can tell).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I like the pattern, I'm just objecting to attaching these functions to the interface itself instead of leaving them as free functions.
I feel exactly the same.
0d2dd19
to
afb490b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
cursory review LGTM, would like one other +1 before merge
core/coreapi/interface/interface.go
Outdated
) | ||
|
||
// ObjectChange represents a change ia a graph | ||
// TODO: do we want this to be an interface? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Make a decision before merging (or, at least, file an issue).
core/coreapi/coreapi.go
Outdated
@@ -112,16 +114,26 @@ type path struct { | |||
} | |||
|
|||
// ParsePath parses path `p` using ipfspath parser, returns the parsed path. | |||
func ParsePath(p string) (coreiface.Path, error) { | |||
func (api *CoreAPI) ParsePath(ctx context.Context, p string, opts ...caopts.ParsePathOption) (coreiface.Path, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The second this resolves the path, it's no longer parsing it. Maybe, instead, expose a ResolvePathString
method (or just keep the steps separate).
core/coreapi/interface/interface.go
Outdated
|
||
// WithResolve is an option for ParsePath which when set to true tells | ||
// ParsePath to also resolve the path | ||
WithResolve(bool) options.ParsePathOption |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should definitely not be a method on the CoreAPI interface (just a function, or maybe just a type type WithResolve bool
). However, I notice we're already doing this all over the place. Any reason for it?
Anyways, do we ever actually need ResolvePath
to resolve? Doesn't everything that takes a path do resolution internally just in case?
core/coreapi/coreapi.go
Outdated
} | ||
|
||
// ParseCid parses the path from `c`, retruns the parsed path. | ||
func ParseCid(c *cid.Cid) coreiface.Path { | ||
func (api *CoreAPI) ParseCid(c *cid.Cid) coreiface.Path { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I really think this should remain a function.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's done that way to expose this function on the public API - which will be eventually implemented by things like go-ipfs-api, so we need an option to abstract this a bit.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will there ever be alternative implementations of path parsing? I'd rather not have to implement ParseCid
when I implement the core interface API.
Blocked on #4672 |
afb490b
to
f271b09
Compare
f271b09
to
3c67617
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Only a few nits. Other than that, negative diffstats give me such a warm and fuzzy feeling.
commands/request.go
Outdated
|
||
// NodeWithoutConstructing returns the underlying node variable | ||
// so that clients may close it. | ||
func (c *Context) NodeWithoutConstructing() *core.IpfsNode { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this used?
core/commands/env.go
Outdated
@@ -18,6 +19,16 @@ func GetNode(env interface{}) (*core.IpfsNode, error) { | |||
return ctx.GetNode() | |||
} | |||
|
|||
// GetApi extracts CoreAPI instance from the environment. | |||
func GetApi(env interface{}) (coreiface.CoreAPI, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should probably take cmds.Environment
(same thing but clearer).
core/coreapi/interface/object.go
Outdated
) | ||
|
||
// ObjectChange represents a change ia a graph | ||
// TODO: do we want this to be an interface? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think a type is sufficient.
core/coreapi/interface/object.go
Outdated
@@ -31,6 +31,38 @@ type ObjectStat struct { | |||
CumulativeSize int | |||
} | |||
|
|||
const ( | |||
// DiffAdd is a Type of ObjectChange where a link was added to the graph | |||
DiffAdd = iota |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe use a newtype here?
core/commands/object/patch.go
Outdated
// GetNode extracts the node from the environment. | ||
func GetNode(env interface{}) (*core.IpfsNode, error) { | ||
// GetApi extracts CoreAPI instance from the environment. | ||
func GetApi(env interface{}) (coreiface.CoreAPI, error) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Take an Environment
.
b09eecd
to
5eaf5ad
Compare
Fixed |
5eaf5ad
to
f03680b
Compare
rebased it since i messed it up by extracting things |
f03680b
to
62f986e
Compare
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
License: MIT Signed-off-by: Łukasz Magiera <[email protected]>
62f986e
to
60039f2
Compare
This PR wires CoreAPI into command utils and makes
ipfs object *
use the api.Only changing
ipfs object
to keep this PR small, will open another PRs for other command groups after this one is merged.