Create a new Format method that is like StringOfBase but never errors
This commit is contained in:
12
cid.go
12
cid.go
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user