Skip to content

Commit

Permalink
Add MustParse
Browse files Browse the repository at this point in the history
  • Loading branch information
mg98 committed Jun 24, 2022
1 parent f4b3e66 commit b04449f
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,39 @@ func Parse(v interface{}) (Cid, error) {
}
}

// MustParse calls Parse but will panic on error.
func MustParse(v interface{}) Cid {
var (
c Cid
err error
)
switch v2 := v.(type) {
case string:
if strings.Contains(v2, "/ipfs/") {
c, err = Decode(strings.Split(v2, "/ipfs/")[1])
break
}
c, err = Decode(v2)
break
case []byte:
c, err = Cast(v2)
break
case mh.Multihash:
c, err = tryNewCidV0(v2)
break
case Cid:
c = v2
break
default:
c, err = Undef, fmt.Errorf("can't parse %+v as Cid", v2)
break
}
if err != nil {
panic(err)
}
return c
}

// Decode parses a Cid-encoded string and returns a Cid object.
// For CidV1, a Cid-encoded string is primarily a multibase string:
//
Expand Down

0 comments on commit b04449f

Please sign in to comment.