Code Monkey home page Code Monkey logo

Comments (16)

BigLep avatar BigLep commented on July 21, 2024 1

@schomatis : this is a quick item to tackle we believe. @Jorropo will be the code reviewer.

from go-cid.

schomatis avatar schomatis commented on July 21, 2024 1

I think I have converged with @Jorropo to put this in go-path then.

@trim21 Would something like this work for your use case?

	r, err := ipns.ResolveIPNS(context.TODO(), p.NameResolver(), name)
	if err != nil {
		return errors.Wrap(err, "failed to resolve ipns")
	}

	// c, err := cid.Parse(r) <- here
	c, err := r.ToCid()
	if err != nil {
		return errors.Wrapf(err, "failed to convert path %s to CID", r)
	}

from go-cid.

trim21 avatar trim21 commented on July 21, 2024 1

lgtm

from go-cid.

welcome avatar welcome commented on July 21, 2024

Thank you for submitting your first issue to this repository! A maintainer will be here shortly to triage and review.
In the meantime, please double-check that you have provided all the necessary information to make this process easy! Any information that can help save additional round trips is useful! We currently aim to give initial feedback within two business days. If this does not happen, feel free to leave a comment.
Please keep an eye on how this issue will be labeled, as labels give an overview of priorities, assignments and additional actions requested by the maintainers:

  • "Priority" labels will show how urgent this is for the team.
  • "Status" labels will show if this is ready to be worked on, blocked, or in progress.
  • "Need" labels will indicate if additional input or analysis is required.

Finally, remember to use https://discuss.ipfs.io if you just need general support.

from go-cid.

Stebalien avatar Stebalien commented on July 21, 2024

Well, it's not a cid. It's a path (e.g., /ipfs/CID). It usually contains a CID, but it might not (could be a dnslink, could be some other system).

from go-cid.

trim21 avatar trim21 commented on July 21, 2024

Well, it's not a cid. It's a path (e.g., /ipfs/CID). It usually contains a CID, but it might not (could be a dnslink, could be some other system).

yes, but string "/ipfs/CID" works for "cid.Parse"

That's what I'd like to see path.Path("/ipfs/did") works, too.

It could still throw a error if it's not a valid cid

from go-cid.

Stebalien avatar Stebalien commented on July 21, 2024

That's... moderately terrible. But I see your point.

from go-cid.

trim21 avatar trim21 commented on July 21, 2024

Simplest way it to check if it's a interface fmt.Stringer

from go-cid.

Jorropo avatar Jorropo commented on July 21, 2024

Sounds even more of a hack.

from go-cid.

trim21 avatar trim21 commented on July 21, 2024

this is because you can't import github.com/ipfs/go-path in go-cid, go-path already import go-cid. will cause a cycle import.

from go-cid.

schomatis avatar schomatis commented on July 21, 2024

I get the general need but I'm not sure what's the request in terms of implementation. Do we want to create a "parse path" API here? Is knowing paths a responsibility of the CID package? Normally the path contains (and it's aware of) the CID (the cycle import is already flagging that).

I'd expect to be the path package the owner of this. In addition to having a FromCid API it could also have the reciprocal ToCid, and let path be the one to tell you if it contains a consumable CID or not, instead of forcing the CID to figure it out from an opaque string.

I'm fine going in any direction here but need more clear input from anyone in this thread, blocking until then.

from go-cid.

Jorropo avatar Jorropo commented on July 21, 2024

@schomatis

We already have:

func Parse(v interface{}) (Cid, error) {
	switch v2 := v.(type) {
	case string:
		if strings.Contains(v2, "/ipfs/") {
			return Decode(strings.Split(v2, "/ipfs/")[1])
		}
		return Decode(v2)
 	// ...

I personally dislike this code, because if I give it /ipfs/Qmfoo/abc it's gonna return Qmfoo instead of an error.

However what is being asked here is to unify the behaviour of string and path.Path.

The canonically solution is:

 func Parse(v interface{}) (Cid, error) {
 	switch v2 := v.(type) {
 	case string:
 		if strings.Contains(v2, "/ipfs/") {
 			return Decode(strings.Split(v2, "/ipfs/")[1])
 		}
 		return Decode(v2)
+	case path.Path:
+		return Parse(string(v2))
 	case []byte:
 		return Cast(v2)
 	case mh.Multihash:
 		return tryNewCidV0(v2)
 	case Cid:
 		return v2, nil
 	default:
 		return Undef, fmt.Errorf("can't parse %+v as Cid", v2)
 	}
 }

but this does not work because of import cycles issue.

A "simple" solution could be:

+import "github.com/nofeaturesonlybugs/set/coerce"
 // ...
 func Parse(v interface{}) (Cid, error) {
+	if v2, err := coerce.String(v); err != nil {
+		if strings.Contains(v2, "/ipfs/") {
+			return Decode(strings.Split(v2, "/ipfs/")[1])
+		}
+		return Decode(v2)
+	}
 	switch v2 := v.(type) {
-	case string:
-		if strings.Contains(v2, "/ipfs/") {
-			return Decode(strings.Split(v2, "/ipfs/")[1])
-		}
-		return Decode(v2)
 	case []byte:
 		return Cast(v2)
 	case mh.Multihash:
 		return tryNewCidV0(v2)
 	case Cid:
 		return v2, nil
 	default:
 		return Undef, fmt.Errorf("can't parse %+v as Cid", v2)
 	}
 }

This will work thx to reflect.Type.Kind which will be string for path.Path.

I agree that this behaviour is weird and probably not what we want, but I can't help you on that because I don't know what it should be instead either (I guess it's your job to figure it out if you want to fix it 🙂).

from go-cid.

schomatis avatar schomatis commented on July 21, 2024

@Jorropo

(I guess it's your job to figure it out if you want to fix it slightly_smiling_face).

I've already done my job and told you: (*Path) ToCid() (Cid, error), in github.com/ipfs/go-path, not here.

You're the reviewer so it's your call how to move forward.

from go-cid.

Jorropo avatar Jorropo commented on July 21, 2024

@schomatis oh nvm, the solution here (in go-cid) would be:

type cidable interface{
  ToCid() (Cid, error)
}

 func Parse(v interface{}) (Cid, error) {
 	switch v2 := v.(type) {
 	case string:
 		if strings.Contains(v2, "/ipfs/") {
 			return Decode(strings.Split(v2, "/ipfs/")[1])
 		}
 		return Decode(v2)
+	case cidable:
+		return v2.ToCid()
 	case []byte:
 		return Cast(v2)
 	case mh.Multihash:
 		return tryNewCidV0(v2)
 	case Cid:
 		return v2, nil
 	default:
 		return Undef, fmt.Errorf("can't parse %+v as Cid", v2)
 	}
 }

?
(if yes then I'm good with this)

from go-cid.

schomatis avatar schomatis commented on July 21, 2024

Working on this in go-path, will close this when that lands over there.

from go-cid.

rvagg avatar rvagg commented on July 21, 2024

Going to close since ipfs/go-path#61 should be the appropriate way to solve this - if you're expecting something more than just a CID then use a higher-level API to solve it, in this case go-path seems to be the most appropriate wrapper to deal with this.

from go-cid.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.