Skip to content

Encode, EncodeName: drop error from function signature #164

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ func main() {
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")

// Create a new multihash with it.
mHashBuf, _ := multihash.EncodeName(buf, "sha1")
mHashBuf := multihash.EncodeName(buf, "sha1")
// Print the multihash as hex string
fmt.Printf("hex: %s\n", hex.EncodeToString(mHashBuf))

Expand Down
8 changes: 3 additions & 5 deletions multihash.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,21 +243,19 @@ func Decode(buf []byte) (*DecodedMultihash, error) {

// Encode a hash digest along with the specified function code.
// Note: the length is derived from the length of the digest itself.
//
// The error return is legacy; it is always nil.
func Encode(buf []byte, code uint64) ([]byte, error) {
func Encode(buf []byte, code uint64) []byte {
// FUTURE: this function always causes heap allocs... but when used, this value is almost always going to be appended to another buffer (either as part of CID creation, or etc) -- should this whole function be rethought and alternatives offered?
newBuf := make([]byte, varint.UvarintSize(code)+varint.UvarintSize(uint64(len(buf)))+len(buf))
n := varint.PutUvarint(newBuf, code)
n += varint.PutUvarint(newBuf[n:], uint64(len(buf)))

copy(newBuf[n:], buf)
return newBuf, nil
return newBuf
}

// EncodeName is like Encode() but providing a string name
// instead of a numeric code. See Names for allowed values.
func EncodeName(buf []byte, name string) ([]byte, error) {
func EncodeName(buf []byte, name string) []byte {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should be returning an error if the name isn't known. Otherwise, we just default to 0 which is definitely wrong.

return Encode(buf, Names[name])
}

Expand Down
16 changes: 4 additions & 12 deletions multihash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,22 +94,14 @@ func TestEncode(t *testing.T) {

nb := append(pre[:n], ob...)

encC, err := Encode(ob, tc.code)
if err != nil {
t.Error(err)
continue
}
encC := Encode(ob, tc.code)

if !bytes.Equal(encC, nb) {
t.Error("encoded byte mismatch: ", encC, nb)
t.Error(hex.Dump(nb))
}

encN, err := EncodeName(ob, tc.name)
if err != nil {
t.Error(err)
continue
}
encN := EncodeName(ob, tc.name)

if !bytes.Equal(encN, nb) {
t.Error("encoded byte mismatch: ", encN, nb)
Expand All @@ -128,7 +120,7 @@ func TestEncode(t *testing.T) {
func ExampleEncodeName() {
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
mhbuf := EncodeName(buf, "sha1")
mhhex := hex.EncodeToString(mhbuf)
fmt.Printf("hex: %v\n", mhhex)

Expand Down Expand Up @@ -217,7 +209,7 @@ func TestTable(t *testing.T) {
func ExampleDecode() {
// ignores errors for simplicity - don't do that at home.
buf, _ := hex.DecodeString("0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33")
mhbuf, _ := EncodeName(buf, "sha1")
mhbuf := EncodeName(buf, "sha1")
o, _ := Decode(mhbuf)
mhhex := hex.EncodeToString(o.Digest)
fmt.Printf("obj: %v 0x%x %d %s\n", o.Name, o.Code, o.Length, mhhex)
Expand Down
2 changes: 1 addition & 1 deletion sum.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,5 +71,5 @@ func encodeHash(hasher hash.Hash, code uint64, length int) (Multihash, error) {

// Put the multihash metainfo bytes at the front of the buffer.
// FUTURE: try to improve allocations here. Encode does several which are probably avoidable, but it's the shape of the Encode method arguments that forces this.
return Encode(sum, code)
return Encode(sum, code), nil
}