-
Notifications
You must be signed in to change notification settings - Fork 5
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: add createNode() and createLink() factories #20
Conversation
to match DAGNode and DAGLink constructors of old
I think I'm pretty happy with this, it almost does away with the need for |
🤷 I kind of liked that I was passing plain old js objects into The only improvement I would make is maybe not having it require a I think |
k I don't mind either way, as long as we get to strict forms in the end that's all I care about. If you want to back out the change to ipfs/js-ipfs-unixfs#116 I'm fine with that and may back out this change if it's not being used there. |
* @returns {PBNode} | ||
*/ | ||
export function createNode (data, links = []) { | ||
return prepare({ Data: data, Links: links }) |
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 literary meant this:
return prepare({ Data: data, Links: links }) | |
return { Data: data, Links: links } |
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.
The reason for the prepare()
is simply to ensure the data types and shape are correct, do some basic coercion where possible and to throw an error if this won't pass encode()
. It's essentially doing { Data: data, Links: links}
but with checking too so I don't have to duplicate that logic. So the helpers are not just giving you correctly shaped objects but are checking the values you pass it too.
* @returns {PBLink} | ||
*/ | ||
export function createLink (name, size, cid) { | ||
return asLink({ Hash: cid, Name: name, Tsize: size }) |
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.
And this
return asLink({ Hash: cid, Name: name, Tsize: size }) | |
return { Hash: cid, Name: name, Tsize: size } |
* @param {CID} cid | ||
* @returns {PBLink} | ||
*/ | ||
export function createLink (name, size, cid) { |
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.
export function createLink (name, size, cid) { | |
export function createLink ({ name, size, cid }) { |
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.
The idea here was to provide something that had the same function signature as the old PBLink
and PBNode
constructors in ipld-dag-pb.
* @param {PBLink[]} [links=[]] | ||
* @returns {PBNode} | ||
*/ | ||
export function createNode (data, links = []) { |
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.
export function createNode (data, links = []) { | |
export function createNode ({ data, links = [] }) { |
OK, so neither of you are happy with this, I'm not overjoyed myself. Should I just remove them entirely? @Gozala @achingbrain? |
The only extra thing I want out of this module is to not require a dagPb.encode({
Data: Uint8Array.from([0, 1, 2])
}) I would even skip validation on encode entirely as it then guards on all the properties anyway - if the user cares they can call |
To match DAGNode and DAGLink constructors of old
See the README notes for examples of how it would be used, it's really just a shorthand for making the objects but does so safely and reliably.
I also considered moving
prepare
andvalidate
and these new functions on to a@ipld/dag-pb/util
export rather than the main export, but 🤷.