From 9cb0b7bcaeb6e25b69192f798bc3311cf9568c7f Mon Sep 17 00:00:00 2001 From: Fritz Schneider Date: Wed, 11 Jul 2018 11:20:53 -1000 Subject: [PATCH 1/3] use value receiver --- cid.go | 2 +- cid_test.go | 29 +++++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/cid.go b/cid.go index 99a26a6..ff3072c 100644 --- a/cid.go +++ b/cid.go @@ -410,7 +410,7 @@ func (c *Cid) UnmarshalJSON(b []byte) error { // // Note that this formatting comes from the IPLD specification // (https://github.com/ipld/specs/tree/master/ipld) -func (c *Cid) MarshalJSON() ([]byte, error) { +func (c Cid) MarshalJSON() ([]byte, error) { return []byte(fmt.Sprintf("{\"/\":\"%s\"}", c.String())), nil } diff --git a/cid_test.go b/cid_test.go index b7bdd89..3ecc7ef 100644 --- a/cid_test.go +++ b/cid_test.go @@ -383,3 +383,32 @@ func TestFromJson(t *testing.T) { t.Fatal("json parsing failed") } } + +func TestJsonRoundTrip(t *testing.T) { + exp, err := Decode("zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG") + if err != nil { + t.Fatal(err) + } + + // Verify it works for a *Cid. + enc, err := json.Marshal(exp) + if err != nil { + t.Fatal(err) + } + var actual Cid + err = json.Unmarshal(enc, &actual) + if !exp.Equals(&actual) { + t.Fatal("cids not equal for *Cid") + } + + // Verify it works for a Cid. + enc, err = json.Marshal(*exp) + if err != nil { + t.Fatal(err) + } + var actual2 Cid + err = json.Unmarshal(enc, &actual2) + if !(*exp).Equals(&actual2) { + t.Fatal("cids not equal for *Cid") + } +} From b41162260a2b73e7a05c15801882068c8ba666fe Mon Sep 17 00:00:00 2001 From: Fritz Schneider Date: Wed, 11 Jul 2018 11:21:16 -1000 Subject: [PATCH 2/3] fix typo --- cid_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cid_test.go b/cid_test.go index 3ecc7ef..d7f42f2 100644 --- a/cid_test.go +++ b/cid_test.go @@ -409,6 +409,6 @@ func TestJsonRoundTrip(t *testing.T) { var actual2 Cid err = json.Unmarshal(enc, &actual2) if !(*exp).Equals(&actual2) { - t.Fatal("cids not equal for *Cid") + t.Fatal("cids not equal for Cid") } } From d204c18f7a724ec9f8203ba1e77b7aa7d7d8b58b Mon Sep 17 00:00:00 2001 From: Fritz Schneider Date: Wed, 11 Jul 2018 11:24:59 -1000 Subject: [PATCH 3/3] make test a little more clear --- cid_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cid_test.go b/cid_test.go index d7f42f2..6555251 100644 --- a/cid_test.go +++ b/cid_test.go @@ -408,7 +408,7 @@ func TestJsonRoundTrip(t *testing.T) { } var actual2 Cid err = json.Unmarshal(enc, &actual2) - if !(*exp).Equals(&actual2) { + if !exp.Equals(&actual2) { t.Fatal("cids not equal for Cid") } }