Commit ce4c06c6 authored by Kevin Atkinson's avatar Kevin Atkinson

Extract non-core functionality from go-cid into go-cidutil.

parent 6c39b29c
...@@ -5,14 +5,15 @@ import ( ...@@ -5,14 +5,15 @@ import (
"os" "os"
"strings" "strings"
c "github.com/ipfs/go-cid" cidutil "github.com/ipfs/go-cidutil"
c "github.com/ipfs/go-cid"
mb "github.com/multiformats/go-multibase" mb "github.com/multiformats/go-multibase"
) )
func usage() { func usage() {
fmt.Fprintf(os.Stderr, "usage: %s [-b multibase-code] [-v cid-version] <fmt-str> <cid> ...\n\n", os.Args[0]) fmt.Fprintf(os.Stderr, "usage: %s [-b multibase-code] [-v cid-version] <fmt-str> <cid> ...\n\n", os.Args[0])
fmt.Fprintf(os.Stderr, "<fmt-str> is either 'prefix' or a printf style format string:\n%s", c.FormatRef) fmt.Fprintf(os.Stderr, "<fmt-str> is either 'prefix' or a printf style format string:\n%s", cidutil.FormatRef)
os.Exit(2) os.Exit(2)
} }
...@@ -89,9 +90,9 @@ outer: ...@@ -89,9 +90,9 @@ outer:
continue continue
} }
} }
str, err := c.Format(fmtStr, base, cid) str, err := cidutil.Format(fmtStr, base, cid)
switch err.(type) { switch err.(type) {
case c.FormatStringError: case cidutil.FormatStringError:
fmt.Fprintf(os.Stderr, "Error: %v\n", err) fmt.Fprintf(os.Stderr, "Error: %v\n", err)
os.Exit(2) os.Exit(2)
default: default:
......
package cid package cidutil
import ( import (
"bytes" "bytes"
"fmt" "fmt"
c "github.com/ipfs/go-cid"
mb "github.com/multiformats/go-multibase" mb "github.com/multiformats/go-multibase"
mh "github.com/multiformats/go-multihash" mh "github.com/multiformats/go-multihash"
) )
...@@ -34,7 +35,7 @@ used. For Cid version 1 the multibase prefix is included. ...@@ -34,7 +35,7 @@ used. For Cid version 1 the multibase prefix is included.
// Format formats a cid according to the format specificer as // Format formats a cid according to the format specificer as
// documented in the FormatRef constant // documented in the FormatRef constant
func Format(fmtStr string, base mb.Encoding, cid *Cid) (string, error) { func Format(fmtStr string, base mb.Encoding, cid *c.Cid) (string, error) {
p := cid.Prefix() p := cid.Prefix()
var out bytes.Buffer var out bytes.Buffer
var err error var err error
...@@ -127,7 +128,7 @@ func baseToString(base mb.Encoding) string { ...@@ -127,7 +128,7 @@ func baseToString(base mb.Encoding) string {
} }
func codecToString(num uint64) string { func codecToString(num uint64) string {
name, ok := CodecToStr[num] name, ok := c.CodecToStr[num]
if !ok { if !ok {
return fmt.Sprintf("codec?%d", num) return fmt.Sprintf("codec?%d", num)
} }
......
package cid package cidutil
import ( import (
"fmt" "fmt"
"testing" "testing"
c "github.com/ipfs/go-cid"
mb "github.com/multiformats/go-multibase" mb "github.com/multiformats/go-multibase"
) )
...@@ -55,13 +56,13 @@ func TestFmt(t *testing.T) { ...@@ -55,13 +56,13 @@ func TestFmt(t *testing.T) {
} }
func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) { func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) {
cid, err := Decode(cidStr) cid, err := c.Decode(cidStr)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
} }
base := newBase base := newBase
if newBase == -1 { if newBase == -1 {
base, _ = ExtractEncoding(cidStr) base, _ = c.ExtractEncoding(cidStr)
} }
str, err := Format(fmtStr, base, cid) str, err := Format(fmtStr, base, cid)
if err != nil { if err != nil {
......
{
"author": "kevina",
"bugs": {},
"gx": {
"dvcsimport": "github.com/ipfs/go-cidutil"
},
"gxDependencies": [
{
"author": "multiformats",
"hash": "QmPnFwZ2JXKnXgMw8CdBPxn7FWh6LLdjUjxV1fKHuJnkr8",
"name": "go-multihash",
"version": "1.0.8"
},
{
"author": "whyrusleeping",
"hash": "QmSbvata2WqNkqGtZNg8MR3SKwnB8iQ7vTPJgWqB8bC5kR",
"name": "go-multibase",
"version": "0.2.7"
},
{
"author": "whyrusleeping",
"hash": "QmcgadesKwu51kYF1SjpCLS6LEAdi4KRCfCSMQADgG5fs8",
"name": "go-cid",
"version": "0.7.26"
}
],
"gxVersion": "0.12.1",
"language": "go",
"license": "",
"name": "go-cidutil",
"releaseCmd": "git commit -a -m \"gx publish $VERSION\"",
"version": "0.0.0"
}
package cid package cidutil
import ( import (
"context" "context"
)
// Set is a implementation of a set of Cids, that is, a structure
// to which holds a single copy of every Cids that is added to it.
type Set struct {
set map[string]struct{}
}
// NewSet initializes and returns a new Set.
func NewSet() *Set {
return &Set{set: make(map[string]struct{})}
}
// Add puts a Cid in the Set.
func (s *Set) Add(c *Cid) {
s.set[string(c.Bytes())] = struct{}{}
}
// Has returns if the Set contains a given Cid.
func (s *Set) Has(c *Cid) bool {
_, ok := s.set[string(c.Bytes())]
return ok
}
// Remove deletes a Cid from the Set.
func (s *Set) Remove(c *Cid) {
delete(s.set, string(c.Bytes()))
}
// Len returns how many elements the Set has. c "github.com/ipfs/go-cid"
func (s *Set) Len() int { )
return len(s.set)
}
// Keys returns the Cids in the set.
func (s *Set) Keys() []*Cid {
out := make([]*Cid, 0, len(s.set))
for k := range s.set {
c, _ := Cast([]byte(k))
out = append(out, c)
}
return out
}
// Visit adds a Cid to the set only if it is
// not in it already.
func (s *Set) Visit(c *Cid) bool {
if !s.Has(c) {
s.Add(c)
return true
}
return false type Set = c.Set
}
// ForEach allows to run a custom function on each func NewSet() *Set { return c.NewSet() }
// Cid in the set.
func (s *Set) ForEach(f func(c *Cid) error) error {
for cs := range s.set {
c, _ := Cast([]byte(cs))
err := f(c)
if err != nil {
return err
}
}
return nil
}
// StreamingSet is an extension of Set which allows to implement back-pressure // StreamingSet is an extension of Set which allows to implement back-pressure
// for the Visit function // for the Visit function
type StreamingSet struct { type StreamingSet struct {
Set *Set Set *Set
New chan *Cid New chan *c.Cid
} }
// NewStreamingSet initializes and returns new Set. // NewStreamingSet initializes and returns new Set.
func NewStreamingSet() *StreamingSet { func NewStreamingSet() *StreamingSet {
return &StreamingSet{ return &StreamingSet{
Set: NewSet(), Set: c.NewSet(),
New: make(chan *Cid), New: make(chan *c.Cid),
} }
} }
// Visitor creates new visitor which adds a Cids to the set and emits them to // Visitor creates new visitor which adds a Cids to the set and emits them to
// the set.New channel // the set.New channel
func (s *StreamingSet) Visitor(ctx context.Context) func(c *Cid) bool { func (s *StreamingSet) Visitor(ctx context.Context) func(c *c.Cid) bool {
return func(c *Cid) bool { return func(c *c.Cid) bool {
if s.Set.Visit(c) { if s.Set.Visit(c) {
select { select {
case s.New <- c: case s.New <- c:
......
package cid
import (
"crypto/rand"
"errors"
"testing"
mh "github.com/multiformats/go-multihash"
)
func makeRandomCid(t *testing.T) *Cid {
p := make([]byte, 256)
_, err := rand.Read(p)
if err != nil {
t.Fatal(err)
}
h, err := mh.Sum(p, mh.SHA3, 4)
if err != nil {
t.Fatal(err)
}
cid := &Cid{
codec: 7,
version: 1,
hash: h,
}
return cid
}
func TestSet(t *testing.T) {
cid := makeRandomCid(t)
cid2 := makeRandomCid(t)
s := NewSet()
s.Add(cid)
if !s.Has(cid) {
t.Error("should have the CID")
}
if s.Len() != 1 {
t.Error("should report 1 element")
}
keys := s.Keys()
if len(keys) != 1 || !keys[0].Equals(cid) {
t.Error("key should correspond to Cid")
}
if s.Visit(cid) {
t.Error("visit should return false")
}
foreach := []*Cid{}
foreachF := func(c *Cid) error {
foreach = append(foreach, c)
return nil
}
if err := s.ForEach(foreachF); err != nil {
t.Error(err)
}
if len(foreach) != 1 {
t.Error("ForEach should have visited 1 element")
}
foreachErr := func(c *Cid) error {
return errors.New("test")
}
if err := s.ForEach(foreachErr); err == nil {
t.Error("Should have returned an error")
}
if !s.Visit(cid2) {
t.Error("should have visited a new Cid")
}
if s.Len() != 2 {
t.Error("len should be 2 now")
}
s.Remove(cid2)
if s.Len() != 1 {
t.Error("len should be 1 now")
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment