From caf3bb2de8c0e5e54b8e3d56c557c1d7f5cc40de Mon Sep 17 00:00:00 2001 From: Jeromy Date: Thu, 1 Sep 2016 15:46:36 -0700 Subject: [PATCH] add some more helper functions to ease integration into go-ipfs --- cid.go | 19 +++++++++++++++++++ set.go | 9 +++++++++ 2 files changed, 28 insertions(+) diff --git a/cid.go b/cid.go index cf693a9..a46ddd9 100644 --- a/cid.go +++ b/cid.go @@ -145,3 +145,22 @@ func (c *Cid) Equals(o *Cid) bool { c.version == o.version && bytes.Equal(c.hash, o.hash) } + +func (c *Cid) UnmarshalJSON(b []byte) error { + if len(b) < 2 { + return fmt.Errorf("invalid cid json blob") + } + out, err := Decode(string(b[1 : len(b)-1])) + if err != nil { + return err + } + + c.version = out.version + c.hash = out.hash + c.codec = out.codec + return nil +} + +func (c *Cid) MarshalJSON() ([]byte, error) { + return []byte(fmt.Sprintf("\"%s\"", c.String())), nil +} diff --git a/set.go b/set.go index ec97a1f..a237ae3 100644 --- a/set.go +++ b/set.go @@ -33,3 +33,12 @@ func (s *Set) Keys() []*Cid { } return out } + +func (s *Set) Visit(c *Cid) bool { + if !s.Has(c) { + s.Add(c) + return true + } + + return false +}