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.
I'm wondering if I should make it default to true. cc @lgierth
There was a problem hiding this comment.
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.
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.
Having an explicit resolve will also make go-ipfs-api implementation simpler.
There was a problem hiding this comment.
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.
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.
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
whyrusleeping
left a comment
There was a problem hiding this comment.
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.
TODO: Make a decision before merging (or, at least, file an issue).
core/coreapi/coreapi.go
Outdated
|
|
||
| // 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.
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.
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.
I really think this should remain a function.
There was a problem hiding this comment.
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.
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
Stebalien
left a comment
There was a problem hiding this comment.
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 { |
core/commands/env.go
Outdated
| } | ||
|
|
||
| // GetApi extracts CoreAPI instance from the environment. | ||
| func GetApi(env interface{}) (coreiface.CoreAPI, error) { |
There was a problem hiding this comment.
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.
I think a type is sufficient.
core/coreapi/interface/object.go
Outdated
|
|
||
| const ( | ||
| // DiffAdd is a Type of ObjectChange where a link was added to the graph | ||
| DiffAdd = iota |
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) { |
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 <magik6k@gmail.com>
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
License: MIT Signed-off-by: Łukasz Magiera <magik6k@gmail.com>
62f986e to
60039f2
Compare
This PR wires CoreAPI into command utils and makes
ipfs object *use the api.Only changing
ipfs objectto keep this PR small, will open another PRs for other command groups after this one is merged.