Replace DecodeV2 with ExtractEncoding
This commit is contained in:
@@ -69,15 +69,16 @@ outer:
|
||||
}
|
||||
}
|
||||
for _, cidStr := range args[1:] {
|
||||
base, cid, err := c.DecodeV2(cidStr)
|
||||
cid, err := c.Decode(cidStr)
|
||||
if err != nil {
|
||||
fmt.Fprintf(os.Stdout, "!INVALID_CID!\n")
|
||||
errorMsg("%s: %v", cidStr, err)
|
||||
// Don't abort on a bad cid
|
||||
continue
|
||||
}
|
||||
if newBase != -1 {
|
||||
base = newBase
|
||||
base := newBase
|
||||
if newBase == -1 {
|
||||
base, _ = c.ExtractEncoding(cidStr)
|
||||
}
|
||||
if verConv != nil {
|
||||
cid, err = verConv(cid)
|
||||
|
||||
@@ -10,7 +10,7 @@ import (
|
||||
func TestCidConv(t *testing.T) {
|
||||
cidv0 := "QmUNLLsPACCz1vLxQVkXqqLX5R1X345qqfHbsf67hvA3Nn"
|
||||
cidv1 := "zdj7WbTaiJT1fgatdet9Ei9iDB5hdCxkbVyhyh8YTUnXMiwYi"
|
||||
_, cid, err := c.DecodeV2(cidv0)
|
||||
cid, err := c.Decode(cidv0)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -34,7 +34,7 @@ func TestCidConv(t *testing.T) {
|
||||
func TestBadCidConv(t *testing.T) {
|
||||
// this cid is a raw leaf and should not be able to convert to cidv0
|
||||
cidv1 := "zb2rhhzX7uSKrtQ2ZZXFAabKiKFYZrJqKY2KE1cJ8yre2GSWZ"
|
||||
_, cid, err := c.DecodeV2(cidv1)
|
||||
cid, err := c.Decode(cidv1)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
41
cid.go
41
cid.go
@@ -213,34 +213,47 @@ func Parse(v interface{}) (*Cid, error) {
|
||||
// starting with "Qm" are considered CidV0 and treated directly
|
||||
// as B58-encoded multihashes.
|
||||
func Decode(v string) (*Cid, error) {
|
||||
_, cid, err := DecodeV2(v)
|
||||
return cid, err
|
||||
}
|
||||
|
||||
// DecodeV2 is like Decide but also returns the Multibase encoding the
|
||||
// Cid was encoded in. EXPERIMENTAL and interface may change at any time.
|
||||
func DecodeV2(v string) (mbase.Encoding, *Cid, error) {
|
||||
if len(v) < 2 {
|
||||
return 0, nil, ErrCidTooShort
|
||||
return nil, ErrCidTooShort
|
||||
}
|
||||
|
||||
if len(v) == 46 && v[:2] == "Qm" {
|
||||
hash, err := mh.FromB58String(v)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return mbase.Base58BTC, NewCidV0(hash), nil
|
||||
return NewCidV0(hash), nil
|
||||
}
|
||||
|
||||
base, data, err := mbase.Decode(v)
|
||||
_, data, err := mbase.Decode(v)
|
||||
if err != nil {
|
||||
return 0, nil, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
cid, err := Cast(data)
|
||||
return Cast(data)
|
||||
}
|
||||
|
||||
return base, cid, err
|
||||
// Extract the encoding from a Cid. If Decode on the same string did
|
||||
// not return an error neither will this function.
|
||||
func ExtractEncoding(v string) (mbase.Encoding, error) {
|
||||
if len(v) < 2 {
|
||||
return -1, ErrCidTooShort
|
||||
}
|
||||
|
||||
if len(v) == 46 && v[:2] == "Qm" {
|
||||
return mbase.Base58BTC, nil
|
||||
}
|
||||
|
||||
encoding := mbase.Encoding(v[0])
|
||||
|
||||
// check encoding is valid
|
||||
_, err := mbase.NewEncoder(encoding)
|
||||
if err != nil {
|
||||
return -1, err
|
||||
}
|
||||
|
||||
return encoding, nil
|
||||
}
|
||||
|
||||
func uvError(read int) error {
|
||||
|
||||
@@ -55,13 +55,14 @@ func TestFmt(t *testing.T) {
|
||||
}
|
||||
|
||||
func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, result string) {
|
||||
base, cid, err := DecodeV2(cidStr)
|
||||
if newBase != -1 {
|
||||
base = newBase
|
||||
}
|
||||
cid, err := Decode(cidStr)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
base := newBase
|
||||
if newBase == -1 {
|
||||
base, _ = ExtractEncoding(cidStr)
|
||||
}
|
||||
str, err := Format(fmtStr, base, cid)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -70,4 +71,3 @@ func testFmt(t *testing.T, cidStr string, newBase mb.Encoding, fmtStr string, re
|
||||
t.Error(fmt.Sprintf("expected: %s; but got: %s", result, str))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user