-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Create new Builder interface for creating CIDs. #53
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
5d8ad3e
Create new Format interface for creating CIDs.
kevina 6f95156
Update deprecated note to reflect code review.
kevina 88cd5dc
Add WithCodec and GetCodec methods to format interface.
kevina ad88cb1
Rename Format to Builder.
kevina 67951e2
Move deprecated function to there own file.
kevina ae25e25
Remove PrefixToBuilder as it is needed right now.
kevina 8f7ba15
Documentation Cleanups.
kevina f868375
Enhance Tests.
kevina 86805e7
Change field names in V1Builder to match Prefix.
kevina File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package cid | ||
|
||
import ( | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
type Builder interface { | ||
Sum(data []byte) (*Cid, error) | ||
GetCodec() uint64 | ||
WithCodec(uint64) Builder | ||
} | ||
|
||
type V0Builder struct{} | ||
|
||
type V1Builder struct { | ||
Codec uint64 | ||
MhType uint64 | ||
MhLength int // MhLength <= 0 means the default length | ||
} | ||
|
||
func (p Prefix) GetCodec() uint64 { | ||
return p.Codec | ||
} | ||
|
||
func (p Prefix) WithCodec(c uint64) Builder { | ||
if c == p.Codec { | ||
return p | ||
} | ||
p.Codec = c | ||
if c != DagProtobuf { | ||
p.Version = 1 | ||
} | ||
return p | ||
} | ||
|
||
func (p V0Builder) Sum(data []byte) (*Cid, error) { | ||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewCidV0(hash), nil | ||
} | ||
|
||
func (p V0Builder) GetCodec() uint64 { | ||
return DagProtobuf | ||
} | ||
|
||
func (p V0Builder) WithCodec(c uint64) Builder { | ||
if c == DagProtobuf { | ||
return p | ||
} | ||
return V1Builder{Codec: c, MhType: mh.SHA2_256} | ||
} | ||
|
||
func (p V1Builder) Sum(data []byte) (*Cid, error) { | ||
mhLen := p.MhLength | ||
if mhLen <= 0 { | ||
mhLen = -1 | ||
} | ||
hash, err := mh.Sum(data, p.MhType, mhLen) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return NewCidV1(p.Codec, hash), nil | ||
} | ||
|
||
func (p V1Builder) GetCodec() uint64 { | ||
return p.Codec | ||
} | ||
|
||
func (p V1Builder) WithCodec(c uint64) Builder { | ||
p.Codec = c | ||
return p | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package cid | ||
|
||
import ( | ||
"testing" | ||
|
||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
func TestV0Builder(t *testing.T) { | ||
data := []byte("this is some test content") | ||
|
||
// Construct c1 | ||
format := V0Builder{} | ||
c1, err := format.Sum(data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Construct c2 | ||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c2 := NewCidV0(hash) | ||
|
||
if !c1.Equals(c2) { | ||
t.Fatal("cids mismatch") | ||
} | ||
if c1.Prefix() != c2.Prefix() { | ||
t.Fatal("prefixes mismatch") | ||
} | ||
} | ||
|
||
func TestV1Builder(t *testing.T) { | ||
data := []byte("this is some test content") | ||
|
||
// Construct c1 | ||
format := V1Builder{Codec: DagCBOR, MhType: mh.SHA2_256} | ||
c1, err := format.Sum(data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
// Construct c2 | ||
hash, err := mh.Sum(data, mh.SHA2_256, -1) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
c2 := NewCidV1(DagCBOR, hash) | ||
|
||
if !c1.Equals(c2) { | ||
t.Fatal("cids mismatch") | ||
} | ||
if c1.Prefix() != c2.Prefix() { | ||
t.Fatal("prefixes mismatch") | ||
} | ||
} | ||
|
||
func TestCodecChange(t *testing.T) { | ||
t.Run("Prefix-CidV0", func(t *testing.T) { | ||
p := Prefix{Version: 0, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]} | ||
testCodecChange(t, p) | ||
}) | ||
t.Run("Prefix-CidV1", func(t *testing.T) { | ||
p := Prefix{Version: 1, Codec: DagProtobuf, MhType: mh.SHA2_256, MhLength: mh.DefaultLengths[mh.SHA2_256]} | ||
testCodecChange(t, p) | ||
}) | ||
t.Run("V0Builder", func(t *testing.T) { | ||
testCodecChange(t, V0Builder{}) | ||
}) | ||
t.Run("V1Builder", func(t *testing.T) { | ||
testCodecChange(t, V1Builder{Codec: DagProtobuf, MhType: mh.SHA2_256}) | ||
}) | ||
} | ||
|
||
func testCodecChange(t *testing.T, b Builder) { | ||
data := []byte("this is some test content") | ||
|
||
if b.GetCodec() != DagProtobuf { | ||
t.Fatal("original builder not using Protobuf codec") | ||
} | ||
|
||
b = b.WithCodec(Raw) | ||
c, err := b.Sum(data) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if c.Type() != Raw { | ||
t.Fatal("new cid codec did not change to Raw") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package cid | ||
|
||
import ( | ||
mh "github.com/multiformats/go-multihash" | ||
) | ||
|
||
// NewPrefixV0 returns a CIDv0 prefix with the specified multihash type. | ||
// DEPRECATED: Use V0Builder | ||
func NewPrefixV0(mhType uint64) Prefix { | ||
return Prefix{ | ||
MhType: mhType, | ||
MhLength: mh.DefaultLengths[mhType], | ||
Version: 0, | ||
Codec: DagProtobuf, | ||
} | ||
} | ||
|
||
// NewPrefixV1 returns a CIDv1 prefix with the specified codec and multihash | ||
// type. | ||
// DEPRECATED: Use V1Builder | ||
func NewPrefixV1(codecType uint64, mhType uint64) Prefix { | ||
return Prefix{ | ||
MhType: mhType, | ||
MhLength: mh.DefaultLengths[mhType], | ||
Version: 1, | ||
Codec: codecType, | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should comment that this will auto-upgrade to CIDv1 if necessary. Actually, I wonder if this should return a prefix or just return a builder (V1/V0).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes that is intentional. I don't see any reason why this should return a builder V1/V0.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was just a thought. I don't really see any benefit one way or the other.