add some more helper functions to ease integration into go-ipfs

This commit is contained in:
Jeromy
2016-09-01 15:46:36 -07:00
parent 7105263bfa
commit caf3bb2de8
2 changed files with 28 additions and 0 deletions

19
cid.go
View File

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

9
set.go
View File

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