-
Notifications
You must be signed in to change notification settings - Fork 49
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
better document that representation-level prototypes build type-level nodes #362
Comments
Are you sure that the repr builder gives you a repr node? At least bindnode does not: go-ipld-prime/node/bindnode/repr.go Lines 516 to 526 in d62ec36
See my TODO there; I wrote down exactly the same concerns that you have :) I will admit that this sharp edge of the builder interface isn't well documented, which is why I wrote the TODO while implementing bindnode. @warpfork's codegen also behaves the same way: Note how the repr assembler/builder produce a So I think we have three options:
I personally think option 1 is the easiest and best option in the short term. If we have strong evidence that a |
One more thought: option 2 adding |
@mvdan I agree that option 1 is the best, but we should definitely document it. It's sufficiently non-obvious that I never even considered when I wrote this up that Representation builders would actually return the typed node, not the representation node. But actually it makes sense -- when would I ever really want to build a representation from scratch? (I mean, who knows but also, I can get back to a representation node if I really need it, as you say) |
FWIW, I really have a lot of questions in hindsight if my initial choices about this were sensible. (If one draws the data phase flow on a whiteboard, most things have nice closed cycles with arrows back and forth... except the build method on reprs jumping directly into the type phase. It's glaringly asymmetric on the graph.) I think if we did it again, today, with no code inertia, I'd probably choose option 3. Of course, we have code inertia. :) And I agree that in this scenario it's probably pretty sizable. Documenting the current behavior more loudly seems like the practical choice. I'd note that what we do here is a golang, and specifically go-ipld-prime choice. If we made other IPLD libraries (and those people somehow see this issue, heh), I might recommend them to consider different choice. If we make externally facing serial APIs for operations like "take this object and match it against a schema", I might also at this point suggest such an API have a separate, explicit boolean field for whether to shift from repr level to type, and vice versa. But again, both of those are different scenarios than what this issue discusses -- I just say this for completeness, if this topic is visited again in the future from either of those angles. |
Alongside a short example on how to get to the repr-level node. Fixes #362.
Let's say I have a node
n
which satisfies schema.TypedNode. How do I encode this to CBOR and decode it back to the same kind of schema.TypedNodeLet's say I do:
I believe this will not encode
n
as expected -- it will encode according to the in memory node interface presented byn
, rather than it's representation, which according to the spec describes how it maps to the IPLD data model and hence how its serialized.Ok so instead I do:
I believe I now have the right data serialized.
But how to get get it back out and into another instance the same kind of node as n. I can get the node prototype by doing:
But if I do:
Well this doesn't work as np.NewBuilder returns a builder for the in memory representation presumably, which also presumably will fail to deserialize should I use say
representation tuple
on a struct.Ok, so I can get the representation prototype and decode with it as so:
But if I do:
I get a representation node and now I am stuck, cause I have no method to get back from the representation to schema.TypedNode
I wonder if need to make the following changes to the schema types:
The text was updated successfully, but these errors were encountered: