ipfs / go-ipld-format

IPLD Node and Resolver interfaces in Go
https://github.com/ipld/ipld
MIT License
64 stars 26 forks source link

Add a way to encode to-and-from abstract objects #18

Open Stebalien opened 7 years ago

Stebalien commented 7 years ago

That is, something like:

type Node interface {
    Unmarshal(v interface{}) error
    // ...
}
type BlockEncoderFunc func(prefix cid.Prefix, o interface{}) (Node, error)

type BlockEncoder map[uint64]BlockEncoderFunc

// ...

func Encode(o interface{}, prefix cid.Prefix) (Node, error) {
    if node, ok := node.(Node); ok && node.Cid().Prefix() == prefix {
        return node, nil
    }
    return DefaultBlockEncoder[prefix.Codec](prefix, o)
}
// ...

This way, we can call Encode(Decode(some_block), some_prefix) to re-encode a block in a new format.

Stebalien commented 7 years ago

Related: https://github.com/polydawn/refmt