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

codec/raw: implement the raw codec #150

Merged
merged 1 commit into from
Mar 5, 2021
Merged

codec/raw: implement the raw codec #150

merged 1 commit into from
Mar 5, 2021

Conversation

mvdan
Copy link
Contributor

@mvdan mvdan commented Mar 5, 2021

(see commit message)

@mvdan mvdan requested a review from warpfork March 5, 2021 11:07
@mvdan
Copy link
Contributor Author

mvdan commented Mar 5, 2021

I also could not find a spec doc for it, otherwise I would have linked it.

@mvdan mvdan requested a review from hannahhoward March 5, 2021 11:30
Copy link
Collaborator

@warpfork warpfork left a comment

Choose a reason for hiding this comment

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

I had some nits to pick, but this is holistically pretty good.

codec/raw/codec.go Outdated Show resolved Hide resolved
codec/raw/codec.go Outdated Show resolved Hide resolved
codec/raw/codec.go Outdated Show resolved Hide resolved
It's small, it's simple, and it's already widely used as part of unixfs.
So there's no reason it shouldn't be part of go-ipld-prime.

The codec is tiny, but has three noteworthy parts: the Encode and Decode
funcs, the cidlink multicodec registration, and the Bytes method
shortcut. Each of these has its own dedicated regression test.

I'm also using this commit to showcase the use of quicktest instead of
go-wish. The result is extremely similar, but with less dot-import
magic. For example, if I remove the Bytes shortcut in Decode:

	--- FAIL: TestDecodeBuffer (0.00s)
	    codec_test.go:115:
	        error:
	          got non-nil error
	        got:
	          e"could not decode raw node: must not call Read"
	        stack:
	          /home/mvdan/src/ipld/codec/raw/codec_test.go:115
	            qt.Assert(t, err, qt.IsNil)
@mvdan
Copy link
Contributor Author

mvdan commented Mar 5, 2021

@warpfork ready, I think.

Comment on lines +34 to +37
// To disable the shortcut above, hide the Bytes method by wrapping the buffer
// with an io.Reader:
//
// Decode([...], struct{io.Reader}{buf})
Copy link
Collaborator

Choose a reason for hiding this comment

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

oh wow, TIL. That's a neat idiom.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

one can accomplish it the explicit way, like I did in the test, but this shortcut is much shorter. It's not easy to realise it's possible unless you've seen it before, so... doc :)

Copy link
Collaborator

@warpfork warpfork left a comment

Choose a reason for hiding this comment

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

🚢

@mvdan mvdan merged commit 7e69224 into ipld:master Mar 5, 2021
@mvdan mvdan deleted the codec-raw branch April 7, 2021 13:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants