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
8aa9fde8
Unverified
Commit
8aa9fde8
authored
Aug 21, 2018
by
Kevin Atkinson
Committed by
GitHub
Aug 21, 2018
Browse files
Options
Browse Files
Download
Plain Diff
Merge pull request #4 from ipfs/kevina/inliner
Add Inliner CID Builder.
parents
b6f51d59
4709f60d
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
59 additions
and
0 deletions
+59
-0
inline.go
inline.go
+26
-0
inline_test.go
inline_test.go
+33
-0
No files found.
inline.go
0 → 100644
View file @
8aa9fde8
package
cidutil
import
(
cid
"github.com/ipfs/go-cid"
mhash
"github.com/multiformats/go-multihash"
)
// InlineBuilder is a cid.Builder that will use the id multihash when the
// size of the content is no more than limit
type
InlineBuilder
struct
{
cid
.
Builder
// Parent Builder
Limit
int
// Limit (inclusive)
}
// WithCodec implements the cid.Builder interface
func
(
p
InlineBuilder
)
WithCodec
(
c
uint64
)
cid
.
Builder
{
return
InlineBuilder
{
p
.
Builder
.
WithCodec
(
c
),
p
.
Limit
}
}
// Sum implements the cid.Builder interface
func
(
p
InlineBuilder
)
Sum
(
data
[]
byte
)
(
*
cid
.
Cid
,
error
)
{
if
len
(
data
)
>
p
.
Limit
{
return
p
.
Builder
.
Sum
(
data
)
}
return
cid
.
V1Builder
{
Codec
:
p
.
GetCodec
(),
MhType
:
mhash
.
ID
}
.
Sum
(
data
)
}
inline_test.go
0 → 100644
View file @
8aa9fde8
package
cidutil
import
(
"math/rand"
"testing"
cid
"github.com/ipfs/go-cid"
mhash
"github.com/multiformats/go-multihash"
)
func
TestInlineBuilderSmallValue
(
t
*
testing
.
T
)
{
builder
:=
InlineBuilder
{
cid
.
V0Builder
{},
64
}
c
,
err
:=
builder
.
Sum
([]
byte
(
"Hello World"
))
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
c
.
Prefix
()
.
MhType
!=
mhash
.
ID
{
t
.
Fatal
(
"Inliner builder failed to use ID Multihash on small values"
)
}
}
func
TestInlinerBuilderLargeValue
(
t
*
testing
.
T
)
{
builder
:=
InlineBuilder
{
cid
.
V0Builder
{},
64
}
data
:=
make
([]
byte
,
512
)
rand
.
Read
(
data
)
c
,
err
:=
builder
.
Sum
(
data
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
if
c
.
Prefix
()
.
MhType
==
mhash
.
ID
{
t
.
Fatal
(
"Inliner builder used ID Multihash on large values"
)
}
}
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