diff --git a/cid.go b/cid.go index 3145056..6d74dfb 100644 --- a/cid.go +++ b/cid.go @@ -14,6 +14,21 @@ import ( const UnsupportedVersionString = "" +var ( + // ErrVarintBuffSmall means that a buffer passed to the cid parser was not + // long enough, or did not contain an invalid cid + ErrVarintBuffSmall = errors.New("reading varint: buffer too small") + + // ErrVarintTooBig means that the varint in the given cid was above the + // limit of 2^64 + ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" + + " and not supported") + + // ErrCidTooShort means that the cid passed to decode was not long + // enough to be a valid Cid + ErrCidTooShort = errors.New("cid too short") +) + const ( Raw = 0x55 @@ -70,7 +85,7 @@ func Parse(v interface{}) (*Cid, error) { func Decode(v string) (*Cid, error) { if len(v) < 2 { - return nil, fmt.Errorf("cid too short") + return nil, ErrCidTooShort } if len(v) == 46 && v[:2] == "Qm" { @@ -90,12 +105,6 @@ func Decode(v string) (*Cid, error) { return Cast(data) } -var ( - ErrVarintBuffSmall = errors.New("reading varint: buffer to small") - ErrVarintTooBig = errors.New("reading varint: varint bigger than 64bits" + - " and not supported") -) - func uvError(read int) error { switch { case read == 0: diff --git a/cid_test.go b/cid_test.go index 5a5b939..5bf7a52 100644 --- a/cid_test.go +++ b/cid_test.go @@ -202,6 +202,18 @@ func TestHexDecode(t *testing.T) { } } +func ExampleDecode() { + encoded := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" + c, err := Decode(encoded) + if err != nil { + fmt.Printf("Error: %s", err) + return + } + + fmt.Println(c) + // Output: zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG +} + func TestFromJson(t *testing.T) { cval := "zb2rhhFAEMepUBbGyP1k8tGfz7BSciKXP6GHuUeUsJBaK6cqG" jsoncid := []byte(`{"/":"` + cval + `"}`) diff --git a/set.go b/set.go index 0a1cef2..bf5467c 100644 --- a/set.go +++ b/set.go @@ -27,7 +27,7 @@ func (s *Set) Len() int { func (s *Set) Keys() []*Cid { out := make([]*Cid, 0, len(s.set)) - for k, _ := range s.set { + for k := range s.set { c, _ := Cast([]byte(k)) out = append(out, c) } @@ -44,7 +44,7 @@ func (s *Set) Visit(c *Cid) bool { } func (s *Set) ForEach(f func(c *Cid) error) error { - for cs, _ := range s.set { + for cs := range s.set { c, _ := Cast([]byte(cs)) err := f(c) if err != nil {