MarshalX / python-libipld

🏎️ Fast Python library to work with IPLD: DAG-CBOR, CID, CAR, multibase
https://pypi.org/project/libipld/
MIT License
17 stars 2 forks source link

encode_dag_cbor doesn't sort keys while encoding dictionaries #22

Closed goeo- closed 8 months ago

goeo- commented 8 months ago
>>> libipld.encode_dag_cbor({"a":1, "b":2})
b'\xa2aa\x01ab\x02'
>>> libipld.encode_dag_cbor({"b":2, "a":1})
b'\xa2ab\x02aa\x01'

interestingly, https://github.com/ipld/libipld/blob/master/dag-cbor/src/encode.rs#L276-L296 libipld seems to implement this, but this is seemingly not used? i've had a similar issue with serde_ipld_dagcbor (linked on libipld's readme) and they seem to also implement it in a function that's never called (??)

goeo- commented 8 months ago

i just noticed that #18 fixed this issue yesterday, however i'll leave the issue open because i think it's weird that you had to sort stuff yourself if libipld is supposed to do it

MarshalX commented 8 months ago

was fixed in #18. can't reproduce. i have unit tests for this case. make sure that you are using the latest version of python package. this fix was included in https://github.com/MarshalX/python-libipld/releases/tag/v1.2.2

to update use this command: pip install -U libipld

image
MarshalX commented 8 months ago

i just noticed that #18 fixed this issue yesterday, however i'll leave the issue open because i think it's weird that you had to sort stuff yourself if libipld is supposed to do it

Missed this comment. Well, I think the upstream lib that I use even allow keys in maps that are not strings xddd. So I don't care about their implementation much. Personally, I fixed these specifications things on my end

Btw I use libipld instead of serde. Serde based on different crate used for cbor. I mean 2 completely different implementations

MarshalX commented 8 months ago

interestingly, https://github.com/ipld/libipld/blob/master/dag-cbor/src/encode.rs#L276-L296 libipld seems to implement this, but this is seemingly not used?

Should be called when you are trying to encode Ipld enum. So you need to build ipld enum first and encode it

let bytes = DagCborCodec.encode(&ipld);
MarshalX commented 8 months ago

closing becuase of inactivity