Skip to content
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

Closed
wants to merge 19 commits into from

Conversation

smrz2001
Copy link
Collaborator

fixes #4

@smrz2001 smrz2001 self-assigned this Sep 22, 2021
@alexjg
Copy link
Contributor

alexjg commented Sep 22, 2021

Do the round trip tests cover some code path which wasn't being exercised by the property based round trip testing in dagjose_test.go? If so it might be worth documenting that and maybe seeing if the property tests could be extended to cover the missing code paths?

@smrz2001
Copy link
Collaborator Author

smrz2001 commented Sep 22, 2021

Do the round trip tests cover some code path which wasn't being exercised by the property based round trip testing in dagjose_test.go? If so it might be worth documenting that and maybe seeing if the property tests could be extended to cover the missing code paths?

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.

@smrz2001 smrz2001 marked this pull request as draft September 22, 2021 20:24
@smrz2001
Copy link
Collaborator Author

smrz2001 commented Oct 7, 2021

@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 go-ipld-prime in place (with Eric's help) but I'm still working through a few issues.

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.

@smrz2001 smrz2001 requested a review from alexjg October 7, 2021 23:29
@oed oed assigned decentralgabe and unassigned decentralgabe Oct 8, 2021
@oed oed requested a review from decentralgabe October 8, 2021 07:22
dagjose/dagjose_assembler.go Outdated Show resolved Hide resolved
go.mod Outdated Show resolved Hide resolved
@@ -39,7 +39,7 @@ func (d dagJOSENode) LookupByString(key string) (ipld.Node, error) {
if key == "recipients" {
if d.recipients != nil {
return fluent.MustBuildList(
Copy link
Contributor

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 😄

Copy link
Contributor

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?

dagjose/multicodec.go Outdated Show resolved Hide resolved
Copy link
Contributor

@decentralgabe decentralgabe left a 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.

dagjose/multicodec.go Outdated Show resolved Hide resolved
dagjose/jws_signature_assembler.go Show resolved Hide resolved
Copy link

@Geo25rey Geo25rey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@oed
Copy link
Member

oed commented Oct 14, 2021

Do we have tests that could run in CI for this repo?

@Geo25rey
Copy link

@oed go test ./dagjose from the root of the repo

Comment on lines 19 to 21
func Decode(na datamodel.NodeAssembler, r io.Reader) error {
return cbor.Decode(na, r)
}
Copy link
Contributor

@warpfork warpfork Oct 20, 2021

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:

  1. ignore the caller's na parameter and just use dagjose.NewBuilder regardless;
  2. return an error if the caller's na parameter isn't *dagJOSENodeBuilder;
  3. check if na is *dagJOSENodeBuilder and go straight into it if possible; otherwise make one and use it, and then do Copy into the caller's na 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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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 {
Copy link
Contributor

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.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sweet, added.

@smrz2001 smrz2001 marked this pull request as ready for review October 22, 2021 00:26
// 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)) {
Copy link
Contributor

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.

@smrz2001
Copy link
Collaborator Author

Closing in favor of new PR.

@smrz2001 smrz2001 closed this Nov 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

want encoder and decoder methods
6 participants