Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
dms3
go-cidutil
Commits
ce4c06c6
Commit
ce4c06c6
authored
Aug 16, 2018
by
Kevin Atkinson
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Extract non-core functionality from go-cid into go-cidutil.
parent
6c39b29c
Changes
6
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
57 additions
and
172 deletions
+57
-172
cid-fmt/main.go
cid-fmt/main.go
+5
-4
format.go
format.go
+4
-3
format_test.go
format_test.go
+4
-3
package.json
package.json
+34
-0
set.go
set.go
+10
-70
set_test.go
set_test.go
+0
-92
No files found.
cid-fmt/main.go
View file @
ce4c06c6
...
@@ -5,14 +5,15 @@ import (
...
@@ -5,14 +5,15 @@ import (
"os"
"os"
"strings"
"strings"
c
"github.com/ipfs/go-cid"
c
idutil
"github.com/ipfs/go-cid
util
"
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"
,
c
idutil
.
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
:=
c
idutil
.
Format
(
fmtStr
,
base
,
cid
)
switch
err
.
(
type
)
{
switch
err
.
(
type
)
{
case
c
.
FormatStringError
:
case
c
idutil
.
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
:
...
...
format.go
View file @
ce4c06c6
package
cid
package
cid
util
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
)
}
}
...
...
format_test.go
View file @
ce4c06c6
package
cid
package
cid
util
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
{
...
...
package.json
0 → 100644
View file @
ce4c06c6
{
"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"
}
set.go
View file @
ce4c06c6
package
cid
package
cid
util
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
:
...
...
set_test.go
deleted
100644 → 0
View file @
6c39b29c
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"
)
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment