Skip to content

Commit

Permalink
merkeldag: change CidVersion from a struct to a uint64
Browse files Browse the repository at this point in the history
Slightly less safe, but more idiomatic.

License: MIT
Signed-off-by: Kevin Atkinson <k@kevina.org>
  • Loading branch information
kevina committed Mar 8, 2017
1 parent 419cd6b commit 44b6c80
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 14 deletions.
21 changes: 8 additions & 13 deletions merkledag/cidversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,20 @@ import (
"fmt"
)

// CidVersion is a a cid version number that is guaranteed to be valid
type CidVersion struct {
version uint64
}
// CidVersion is a a cid version number. Do not set directly, instead
// use NewCidVersion to create. It is safe to panic if it is invalid.
type CidVersion uint64

var (
CID0 = CidVersion{0}
CID1 = CidVersion{1}
const (
CID0 CidVersion = 0
CID1 CidVersion = 1
)

func NewCidVersion(version int) (CidVersion, error) {
switch version {
case 0, 1:
return CidVersion{uint64(version)}, nil
return CidVersion(version), nil
default:
return CidVersion{}, fmt.Errorf("unknown CID version: %d", version)
return CidVersion(0), fmt.Errorf("unknown CID version: %d", version)
}
}

func (v CidVersion) Version() uint64 {
return v.version
}
2 changes: 1 addition & 1 deletion merkledag/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ var v1CidPrefix = cid.Prefix{
}

func (n *ProtoNode) SetCidVersion(v CidVersion) {
switch v := v.Version(); v {
switch v {
case 0:
n.Prefix = v0CidPrefix
case 1:
Expand Down

0 comments on commit 44b6c80

Please sign in to comment.