-
Notifications
You must be signed in to change notification settings - Fork 11
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
feat(dagjose): add encoder and decoder methods #13
Conversation
Do the round trip tests cover some code path which wasn't being exercised by the property based round trip testing in |
Apologies @alexjg, I should have put this up as a draft PR. I'm still working some things out before it's ready. The round-trip tests are "borrowed" and I need to clean them up. I'll keep your comment in mind as I do. |
40d11b0
to
fe0aeef
Compare
c0717ca
to
58b8c0d
Compare
@alexjg @warpfork I'm adding you as reviewers even though the PR isn't quite ready yet. I think I have the adapter code between Alex's version and Feel free to look at and comment on the code but please note that there will be more changes coming soon. I will definitely be adding rainy day tests for encoding of invalid DAG-JOSE objects. |
dagjose/jose_node.go
Outdated
@@ -39,7 +39,7 @@ func (d dagJOSENode) LookupByString(key string) (ipld.Node, error) { | |||
if key == "recipients" { | |||
if d.recipients != nil { | |||
return fluent.MustBuildList( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is quite hard to read, I know you didn't do this either 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As the person who did do this I'm sorry you find it hard to read 🙁 . Any chance you would be able to articulate why you find it hard to read and what alternative you would find easier to read?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
overall looks good, mostly nits. the panic is what we should definitely remove.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Do we have tests that could run in CI for this repo? |
@oed |
dagjose/multicodec.go
Outdated
func Decode(na datamodel.NodeAssembler, r io.Reader) error { | ||
return cbor.Decode(na, r) | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To match the strictness that Encode has, by leaning on the enforcements done in the NodeAssembler implementation in this package, this should probably do one of:
- ignore the caller's
na
parameter and just usedagjose.NewBuilder
regardless; - return an error if the caller's
na
parameter isn't*dagJOSENodeBuilder
; - check if
na
is*dagJOSENodeBuilder
and go straight into it if possible; otherwise make one and use it, and then doCopy
into the caller'sna
at the end.
Option 3 is probably the most consistent with how Encode is currently written, and also throws the least flak at the caller, so seems likely to be best.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Excellent, thank you, this is very useful. Will have changes up today.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, please check.
|
||
// Encode walks the given datamodel.Node and serializes it to the given | ||
// io.Writer. Encode fits the codec.Encoder function interface. | ||
func Encode(n datamodel.Node, w io.Writer) error { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend adding a fastpath through here if the n
given by the caller is already using the concrete type from this package. Just a quick simple type assertion should do.
If the node given is already in this package's node implementation, then that data's already been validated, and we don't need to do that again, nor allocation new memory and be copying stuff. Should save a fair amount of time.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sweet, added.
// CBOR is a superset of DAG-JOSE and can be used to decode valid DAG-JOSE | ||
// objects without decoding the CID (as expected by the DAG-JOSE spec: | ||
// https://specs.ipld.io/block-layer/codecs/dag-jose.html). | ||
if reflect.TypeOf(na) == reflect.TypeOf(new(dagJOSENodeBuilder)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need reflect to do this. :) Just specificType, castWasOk := na.(*dagJOSENodeBuilder); castWasOk
should do here.
Closing in favor of new PR. |
fixes #4