Add feature parity with go-cid implementation #61
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Summary
This PR implements comprehensive feature parity with
go-cid, adding 13 major features organized by priority.Features Added
Phase 1: Critical Features (P0)
1. JSON Marshaling (IPLD Format)
BaseCID.to_json_dict()- Convert CID to IPLD JSON format:{"/": "<cid-string>"}BaseCID.from_json_dict()- Parse CID from IPLD JSON formatCIDJSONEncoder- Custom JSON encoder for CID objectsjsonmodule2. Prefix Operations
Prefixclass - Create and manage CID metadata (version, codec, multihash type/length)Prefix.sum()- Hash data and create CID from prefixPrefix.to_bytes()/Prefix.from_bytes()- Serialize/deserialize prefixBaseCID.prefix()- Extract prefix from existing CIDPrefix.v0()andPrefix.v1()Phase 2: High Priority Features (P1)
3. /ipfs/ Path Parsing
parse_ipfs_path()- Extract CID from /ipfs/ pathsfrom_string()/ipfs/Qm...,https://ipfs.io/ipfs/Qm...,http://localhost:8080/ipfs/Qm...4. Extract Encoding
extract_encoding()- Extract multibase encoding from CID string without fully parsing5. Trailing Bytes Validation
from_bytes_strict()- Parse CID from bytes, validating no trailing bytesPhase 3: Medium Priority Features (P2)
6. Builder Pattern
V0Builder- Fluent API for constructing CIDv0V1Builder- Fluent API for constructing CIDv1Builder.sum()- Hash data and create CIDBuilder.with_codec()- Chain codec changes7. Set Operations
CIDSetclass - Manage collections of unique CIDsadd(),has(),remove(),visit(),for_each()__len__,__contains__,__iter__)__hash__implementation on BaseCID (also added)8. Defined() Check
BaseCID.defined()- Check if CID is defined (not a zero-value/undefined CID)9. Stream Parsing
from_reader()- Parse CID from reader/streamPhase 4: Low Priority Features (P3)
10. MustParse()
must_parse()- Parse CID, always raising exception on error11. Binary/Text Marshaling
BaseCID.to_bytes()- Serialize to bytes (alias for buffer)BaseCID.to_text()- Serialize to text (UTF-8 encoded string)BaseCID.from_text()- Deserialize from text12. KeyString()
BaseCID.key_string()- Return binary representation as string for use as map keys13. Loggable()
BaseCID.loggable()- Return dict for logging purposesImplementation Details
New Modules
cid/prefix.py- Prefix class and operationscid/builder.py- Builder pattern implementationcid/set.py- CIDSet classModified Modules
cid/cid.py- Added multiple new methods and helper functionscid/__init__.py- Updated exportsdocs/api_reference.rst- Added documentation for all new featuresdocs/usage.rst- Added comprehensive usage examplestests/test_cid.py- Added JSON marshaling teststests/test_new_features.py- New test file with 46 teststests/test_prefix.py- New test file for prefix operationsTesting
Documentation
Breaking Changes
None - all new features are additive and backward compatible.
Checklist
make pr(linting, type checking, tests)make docsCloses #60