Skip to content

Commit

Permalink
Create a new Format method that is like StringOfBase but never errors
Browse files Browse the repository at this point in the history
  • Loading branch information
kevina committed Jul 27, 2018
1 parent 13d61b9 commit 8fd9ae9
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cid.go
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,18 @@ func (c *Cid) StringOfBase(base mbase.Encoding) (string, error) {
}
}

// Format return the string representation of a Cid
func (c *Cid) Format(base mbase.Encoder) string {
switch c.version {
case 0:
return c.hash.B58String()
case 1:
return base.Encode(c.bytesV1())
default:
panic("not possible to reach this point")
}
}

// Hash returns the multihash contained by a Cid.
func (c *Cid) Hash() mh.Multihash {
return c.hash
Expand Down
9 changes: 9 additions & 0 deletions cid_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,15 @@ func TestBasesMarshaling(t *testing.T) {
}

assertEqual(t, cid, out2)

prefix, err := mbase.NewEncoder(b)
if err != nil {
t.Fatal(err)
}
s2 := cid.Format(prefix)
if s != s2 {
t.Fatalf("'%s' != '%s'", s, s2)
}
}
}

Expand Down

0 comments on commit 8fd9ae9

Please sign in to comment.