Skip to content

Fix go-ipld-prime encoding function #5

Description

@makew0rld

// invalidEncode returns true if the provided CBOR cannot be encoded
// after being decoded by a neutral decoder.
// The second return value is the error.
func invalidEncode(b []byte) (bool, string) {
// TODO: decode using general CBOR library
// This allows for only encoding to be tested rather than decode-strictness as well
//
// var data any
// if err := cbor.Unmarshal(b, &data); err != nil {
// return nil, err
// }
// return ipld.Marshal(dagcbor.Encode, &data, nil)
// Currently we can't decode using a different library
// Because ipld.Marshal requires knowing the full type/schema of whatever it's marshalling
// It's really annoying but I guess it's to ensure it's only encoding things that already
// conform to the IPLD schema.
// So for now we decode with the dag-cbor decoder
// In practice this still sort of works because the dag-cbor decoder is permissive,
// in line with the dag-cbor spec.
// "DAG-CBOR decoders may relax strictness requirements by default"
// https://ipld.io/specs/codecs/dag-cbor/spec/#decode-strictness
_, err := roundtrip(b)
if err == nil {
return false, ""
}
return true, err.Error()
}

This contributes to bugs like #4 but also "tagged object (datetime)", where the encoding test for a tagged datetime fails, because the dag-cbor decoder stores it as a string (a separate bug of a non-strict decoder), and then the encoder just encodes the string without error.

serde_ipld_dagcbor also fails that test but for a different reason.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions