Skip to content

Commit 548365f

Browse files
rvaggJorropo
authored andcommitted
fix: bring back, but deprecate CodecToStr and Codecs
use go-multicodec as the source of truth
1 parent 5da2800 commit 548365f

File tree

4 files changed

+60
-0
lines changed

4 files changed

+60
-0
lines changed

cid.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ import (
3030
"strings"
3131

3232
mbase "github.com/multiformats/go-multibase"
33+
"github.com/multiformats/go-multicodec"
3334
mh "github.com/multiformats/go-multihash"
3435
varint "github.com/multiformats/go-varint"
3536
)
@@ -83,6 +84,25 @@ const (
8384
FilCommitmentSealed = 0xf102
8485
)
8586

87+
// Codecs maps the name of a codec to its type
88+
// Deprecated: modern code should use consts from go-multicodec instead:
89+
// <https://github.com/multiformats/go-multicodec>
90+
var Codecs map[string]uint64
91+
92+
// CodecToStr maps the numeric codec to its name
93+
// Deprecated: modern code should use consts from go-multicodec instead:
94+
// <https://github.com/multiformats/go-multicodec>
95+
var CodecToStr map[uint64]string
96+
97+
func init() {
98+
Codecs = make(map[string]uint64)
99+
CodecToStr = make(map[uint64]string)
100+
for _, code := range multicodec.KnownCodes() {
101+
Codecs[code.String()] = uint64(code)
102+
CodecToStr[uint64(code)] = code.String()
103+
}
104+
}
105+
86106
// tryNewCidV0 tries to convert a multihash into a CIDv0 CID and returns an
87107
// error on failure.
88108
func tryNewCidV0(mhash mh.Multihash) (Cid, error) {

cid_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,43 @@ func assertEqual(t *testing.T, a, b Cid) {
2929
}
3030
}
3131

32+
func TestTable(t *testing.T) {
33+
// test some known codecs, hard-wired here to make them a fixture that would
34+
// need to be updated if they change elsewhere
35+
for k, v := range map[uint64]string{
36+
Raw: "raw",
37+
DagProtobuf: "dag-pb",
38+
DagCBOR: "dag-cbor",
39+
DagJSON: "dag-json",
40+
Libp2pKey: "libp2p-key",
41+
GitRaw: "git-raw",
42+
EthBlock: "eth-block",
43+
EthBlockList: "eth-block-list",
44+
EthTxTrie: "eth-tx-trie",
45+
EthTx: "eth-tx",
46+
EthTxReceiptTrie: "eth-tx-receipt-trie",
47+
EthTxReceipt: "eth-tx-receipt",
48+
EthStateTrie: "eth-state-trie",
49+
EthAccountSnapshot: "eth-account-snapshot",
50+
EthStorageTrie: "eth-storage-trie",
51+
BitcoinBlock: "bitcoin-block",
52+
BitcoinTx: "bitcoin-tx",
53+
ZcashBlock: "zcash-block",
54+
ZcashTx: "zcash-tx",
55+
DecredBlock: "decred-block",
56+
DecredTx: "decred-tx",
57+
DashBlock: "dash-block",
58+
DashTx: "dash-tx",
59+
FilCommitmentUnsealed: "fil-commitment-unsealed",
60+
FilCommitmentSealed: "fil-commitment-sealed",
61+
DagJOSE: "dag-jose",
62+
} {
63+
if Codecs[v] != k {
64+
t.Errorf("Table mismatch: 0x%x %s", k, v)
65+
}
66+
}
67+
}
68+
3269
func TestPrefixSum(t *testing.T) {
3370
// Test creating CIDs both manually and with Prefix.
3471
// Tests: https://github.com/ipfs/go-cid/issues/83

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ module github.com/ipfs/go-cid
22

33
require (
44
github.com/multiformats/go-multibase v0.0.3
5+
github.com/multiformats/go-multicodec v0.5.0
56
github.com/multiformats/go-multihash v0.0.15
67
github.com/multiformats/go-varint v0.0.6
78
)

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ github.com/multiformats/go-base36 v0.1.0 h1:JR6TyF7JjGd3m6FbLU2cOxhC0Li8z8dLNGQ8
1313
github.com/multiformats/go-base36 v0.1.0/go.mod h1:kFGE83c6s80PklsHO9sRn2NCoffoRdUUOENyW/Vv6sM=
1414
github.com/multiformats/go-multibase v0.0.3 h1:l/B6bJDQjvQ5G52jw4QGSYeOTZoAwIO77RblWplfIqk=
1515
github.com/multiformats/go-multibase v0.0.3/go.mod h1:5+1R4eQrT3PkYZ24C3W2Ue2tPwIdYQD509ZjSb5y9Oc=
16+
github.com/multiformats/go-multicodec v0.5.0 h1:EgU6cBe/D7WRwQb1KmnBvU7lrcFGMggZVTPtOW9dDHs=
17+
github.com/multiformats/go-multicodec v0.5.0/go.mod h1:DiY2HFaEp5EhEXb/iYzVAunmyX/aSFMxq2KMKfWEues=
1618
github.com/multiformats/go-multihash v0.0.15 h1:hWOPdrNqDjwHDx82vsYGSDZNyktOJJ2dzZJzFkOV1jM=
1719
github.com/multiformats/go-multihash v0.0.15/go.mod h1:D6aZrWNLFTV/ynMpKsNtB40mJzmCl4jb1alC0OvHiHg=
1820
github.com/multiformats/go-varint v0.0.6 h1:gk85QWKxh3TazbLxED/NlDVv8+q+ReFJk7Y2W/KhfNY=

0 commit comments

Comments
 (0)