Create a new Format method that is like StringOfBase but never errors

This commit is contained in:
Kevin Atkinson
2018-07-26 17:50:53 -04:00
parent 1766ab0fcf
commit a0b3b11e63
2 changed files with 21 additions and 0 deletions

12
cid.go
View File

@@ -338,6 +338,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

View File

@@ -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)
}
}
}