From a0b3b11e6344ad4f0d0031bfc528d286b51a0098 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 26 Jul 2018 17:50:53 -0400 Subject: [PATCH 1/4] Create a new Format method that is like StringOfBase but never errors --- cid.go | 12 ++++++++++++ cid_test.go | 9 +++++++++ 2 files changed, 21 insertions(+) diff --git a/cid.go b/cid.go index 6938d3c..00775af 100644 --- a/cid.go +++ b/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 diff --git a/cid_test.go b/cid_test.go index 1540cf4..3a6db4b 100644 --- a/cid_test.go +++ b/cid_test.go @@ -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) + } } } From 9091e50b292578f2ec26b51852998212aa03d4f2 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Wed, 1 Aug 2018 15:34:58 -0400 Subject: [PATCH 2/4] Rename Format method to Encode. --- cid.go | 5 +++-- cid_test.go | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cid.go b/cid.go index 00775af..76e7b55 100644 --- a/cid.go +++ b/cid.go @@ -338,8 +338,9 @@ 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 { +// Encode return the string representation of a Cid in a given base +// when applicable +func (c *Cid) Encode(base mbase.Encoder) string { switch c.version { case 0: return c.hash.B58String() diff --git a/cid_test.go b/cid_test.go index 3a6db4b..2d4c297 100644 --- a/cid_test.go +++ b/cid_test.go @@ -153,11 +153,11 @@ func TestBasesMarshaling(t *testing.T) { assertEqual(t, cid, out2) - prefix, err := mbase.NewEncoder(b) + encoder, err := mbase.NewEncoder(b) if err != nil { t.Fatal(err) } - s2 := cid.Format(prefix) + s2 := cid.Encode(encoder) if s != s2 { t.Fatalf("'%s' != '%s'", s, s2) } From bea727bbd1e08f7de6c79316e6e2aa8d6872be65 Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Wed, 1 Aug 2018 16:51:58 -0400 Subject: [PATCH 3/4] Enhance tests. --- cid_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/cid_test.go b/cid_test.go index 2d4c297..97294c0 100644 --- a/cid_test.go +++ b/cid_test.go @@ -190,6 +190,22 @@ func TestV0Handling(t *testing.T) { if cid.String() != old { t.Fatal("marshaling roundtrip failed") } + + new, err := cid.StringOfBase(mbase.Base58BTC) + if err != nil { + t.Fatal(err) + } + if new != old { + t.Fatal("StringOfBase roundtrip failed") + } + + encoder, err := mbase.NewEncoder(mbase.Base58BTC) + if err != nil { + t.Fatal(err) + } + if cid.Encode(encoder) != old { + t.Fatal("Encode roundtrip failed") + } } func TestV0ErrorCases(t *testing.T) { From b3d85b3dee4fcd7c5c187f6c33efdc584c5aed3e Mon Sep 17 00:00:00 2001 From: Kevin Atkinson Date: Thu, 30 Aug 2018 00:38:17 -0400 Subject: [PATCH 4/4] Enhance documentation for Encode method. --- cid.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cid.go b/cid.go index 76e7b55..6bf2aa5 100644 --- a/cid.go +++ b/cid.go @@ -339,7 +339,8 @@ func (c *Cid) StringOfBase(base mbase.Encoding) (string, error) { } // Encode return the string representation of a Cid in a given base -// when applicable +// when applicable. Version 0 Cid's are always in Base58 as they do +// not take a multibase prefix. func (c *Cid) Encode(base mbase.Encoder) string { switch c.version { case 0: