add an example to the tests to play around with the feature

This commit is contained in:
Jeromy
2017-03-18 19:17:44 -07:00
parent a22bf1e2bf
commit 92cac2f002
3 changed files with 30 additions and 9 deletions

23
cid.go
View File

@@ -14,6 +14,21 @@ import (
const UnsupportedVersionString = "<unsupported cid version>"
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:

View File

@@ -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 + `"}`)

4
set.go
View File

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