Skip to content
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

Merged
merged 5 commits into from
Aug 1, 2018
Merged

Conversation

magik6k
Copy link
Member

@magik6k magik6k commented Feb 2, 2018

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.

@ghost ghost assigned magik6k Feb 2, 2018
@ghost ghost added the status/in-progress In progress label Feb 2, 2018

// WithResolve is an option for ParsePath which when set to true tells
// ParsePath to also resolve the path
WithResolve(bool) options.ParsePathOption
Copy link
Member Author

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

Copy link
Member

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?

Copy link
Member Author

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.

Copy link
Member Author

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.

Copy link
Member

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.

Copy link
Member

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).

Copy link
Member

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.

@magik6k magik6k requested review from a user and Stebalien February 3, 2018 00:17
@magik6k magik6k mentioned this pull request Feb 4, 2018
51 tasks
Copy link
Member

@whyrusleeping whyrusleeping left a 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

)

// ObjectChange represents a change ia a graph
// TODO: do we want this to be an interface?
Copy link
Member

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).

@@ -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) {
Copy link
Member

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).


// WithResolve is an option for ParsePath which when set to true tells
// ParsePath to also resolve the path
WithResolve(bool) options.ParsePathOption
Copy link
Member

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?

}

// 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 {
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member

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.

@magik6k
Copy link
Member Author

magik6k commented Feb 8, 2018

Blocked on #4672

@magik6k magik6k added the status/blocked Unable to be worked further until needs are met label Feb 8, 2018
@Kubuxu Kubuxu self-requested a review March 11, 2018 02:45
@magik6k magik6k added the topic/core-api Topic core-api label Mar 11, 2018
@magik6k magik6k removed the status/blocked Unable to be worked further until needs are met label Jul 19, 2018
@magik6k magik6k requested a review from Stebalien July 19, 2018 13:28
Copy link
Member

@Stebalien Stebalien left a 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.


// NodeWithoutConstructing returns the underlying node variable
// so that clients may close it.
func (c *Context) NodeWithoutConstructing() *core.IpfsNode {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this used?

@@ -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) {
Copy link
Member

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).

)

// ObjectChange represents a change ia a graph
// TODO: do we want this to be an interface?
Copy link
Member

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.

@@ -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
Copy link
Member

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?

// 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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Take an Environment.

@magik6k
Copy link
Member Author

magik6k commented Jul 25, 2018

Fixed

@Stebalien Stebalien added RFM and removed status/in-progress In progress labels Jul 26, 2018
@ghost ghost assigned whyrusleeping Jul 29, 2018
@ghost ghost added the status/in-progress In progress label Jul 29, 2018
@whyrusleeping
Copy link
Member

rebased it since i messed it up by extracting things

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>
@whyrusleeping whyrusleeping merged commit 83f22d5 into master Aug 1, 2018
@whyrusleeping whyrusleeping deleted the feat/coreapi-commands branch August 1, 2018 23:21
@ghost ghost removed the status/in-progress In progress label Aug 1, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
RFM topic/core-api Topic core-api
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants