refactor: rename CID decode to CIDFromString

This commit is contained in:
Derrick Hammer 2024-01-09 08:23:35 -05:00
parent 58cc6153bd
commit ed2a47fca3
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
5 changed files with 11 additions and 11 deletions

View File

@ -64,7 +64,7 @@ func (cid *CID) ToBytes() []byte {
return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.fullBytes) return utils.ConcatBytes(cid.getPrefixBytes(), cid.Hash.fullBytes)
} }
func Decode(cid string) (*CID, error) { func CIDFromString(cid string) (*CID, error) {
decodedBytes, err := MultibaseDecodeString(cid) decodedBytes, err := MultibaseDecodeString(cid)
if err != nil { if err != nil {
return nil, err return nil, err

View File

@ -342,7 +342,7 @@ func TestDecode(t *testing.T) {
} }
for _, tt := range tests { for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) { t.Run(tt.name, func(t *testing.T) {
got, err := Decode(tt.args.cid) got, err := CIDFromString(tt.args.cid)
if (err != nil) != tt.wantErr { if (err != nil) != tt.wantErr {
t.Errorf("DecodeCID() error = %v, wantErr %v", err, tt.wantErr) t.Errorf("DecodeCID() error = %v, wantErr %v", err, tt.wantErr)
return return

View File

@ -36,25 +36,25 @@ func TestDecodeString(t *testing.T) {
{ {
name: "TestValidMultibase_z", name: "TestValidMultibase_z",
args: args{data: testdata.MediaBase58CID}, args: args{data: testdata.MediaBase58CID},
wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.Decode("zabc") wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.CIDFromString("zabc")
wantErr: false, wantErr: false,
}, },
{ {
name: "TestValidMultibase_f", name: "TestValidMultibase_f",
args: args{data: testdata.MediaBase16CID}, args: args{data: testdata.MediaBase16CID},
wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.Decode("fxyz") wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.CIDFromString("fxyz")
wantErr: false, wantErr: false,
}, },
{ {
name: "TestValidMultibase_u", name: "TestValidMultibase_u",
args: args{data: testdata.MediaBase64CID}, args: args{data: testdata.MediaBase64CID},
wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.Decode("uhello") wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.CIDFromString("uhello")
wantErr: false, wantErr: false,
}, },
{ {
name: "TestValidMultibase_b", name: "TestValidMultibase_b",
args: args{data: testdata.MediaBase32CID}, args: args{data: testdata.MediaBase32CID},
wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.Decode("bworld") wantBytes: testdata.MediaCIDBytes, // Adjust this based on the expected output of multibase.CIDFromString("bworld")
wantErr: false, wantErr: false,
}, },
/* { /* {

View File

@ -105,7 +105,7 @@ func (em *ExtraMetadata) decodeItem(pair keyValue) error {
if metadataKey == int(types.MetadataExtensionUpdateCID) { if metadataKey == int(types.MetadataExtensionUpdateCID) {
// Convert string to CID bytes for MetadataExtensionUpdateCID // Convert string to CID bytes for MetadataExtensionUpdateCID
cid, err := encoding.Decode(pair.Value.(string)) cid, err := encoding.CIDFromString(pair.Value.(string))
if err != nil { if err != nil {
return err return err
} }

View File

@ -38,7 +38,7 @@ func NewAnnouncePeers() *AnnouncePeers {
} }
func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error { func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
// Decode the number of peers. // CIDFromString the number of peers.
numPeers, err := dec.DecodeInt() numPeers, err := dec.DecodeInt()
if err != nil { if err != nil {
return err return err
@ -49,7 +49,7 @@ func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
// Loop through each peer. // Loop through each peer.
for i := 0; i < numPeers; i++ { for i := 0; i < numPeers; i++ {
// Decode peer ID. // CIDFromString peer ID.
peerIdBytes, err := dec.DecodeBytes() peerIdBytes, err := dec.DecodeBytes()
if err != nil { if err != nil {
return err return err
@ -62,13 +62,13 @@ func (a *AnnouncePeers) DecodeMessage(dec *msgpack.Decoder) error {
return err return err
} }
// Decode the number of connection URIs for this peer. // CIDFromString the number of connection URIs for this peer.
numUris, err := dec.DecodeInt() numUris, err := dec.DecodeInt()
if err != nil { if err != nil {
return err return err
} }
// Decode each connection URI for this peer. // CIDFromString each connection URI for this peer.
for j := 0; j < numUris; j++ { for j := 0; j < numUris; j++ {
uriStr, err := dec.DecodeString() uriStr, err := dec.DecodeString()
if err != nil { if err != nil {