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

WIP: or combinator improvement #143

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft

Conversation

Gozala
Copy link
Contributor

@Gozala Gozala commented Dec 15, 2021

Attempt to fix #141

I like that this patch gets rid of UnibaseDecoder all together which simplifies things a bit. However I do not like silly composed getter it introduces just so that array reduce will not get confused by the fact that argument is either Decoder<B, P> or CompositeDecoder<P>.

I think it would be better to either

  1. Accept the fact that TS inference is going to be poor and compose things statically (as in manually listing all operands base32.decoder.or(base64.decoder).or(base58.decoder).... Not ideal but I'm guessing number of decoders isn't going to be really big anyway.
  2. Export another function like export const fromTable = (decoders) => new CompositeDecoder(decoders) so that one could simply provide table when needed as opposed to building up decoder with or operator.
    P.S.: I originally left this out, because or works with composed and single base decoders, table on the other hand requires only single base decoders.

@@ -472,7 +506,7 @@ describe('CID', () => {
const hash = await sha256.digest(textEncoder.encode('abc'))
const cid = CID.create(1, 112, hash)

const parsed = CID.parse(cid.toString(base58btc))
const parsed = /** @type {CID} */(CID.parse(cid.toString(base58btc)))
Copy link
Member

Choose a reason for hiding this comment

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

any idea why it's failing to infer this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not failing it's just return type is null|CID this manually refines it to CID, which is something TS allows.

],
"exclude": [
"vendor",
"node_modules"
"node_modules",
"node_modules/multiformats"
Copy link
Member

Choose a reason for hiding this comment

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

this is from the ts-use test installation? or something else?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think this is no longer necessary, I was running into a problem where all imports were picked up from node_modules/multiformats as opposed to from src in the end mapped paths is what fixed that. This is just artifact of my trying to figure out the way.

@rvagg
Copy link
Member

rvagg commented Dec 16, 2021

silly composed getter it introduces

Yeah, that is pretty silly. Although maybe the clean-up in there is worth it, it's quite nice now, aside from that.

I don't mind your fromTable idea either, I reckon that's going to cover the common use-cases of this. And we could copy the pattern if/when we get around to doing codecs because we have js-ipfs doing its own code->codec mapping in multiple places that would be nice to try and simplify with a single call through a composed stack. Your ipnft thing could probably do with it too. Mostly you just want to say "I have some encoders, squish them together so I have one thing please" and having to chain .or() is going to lead to the typical reduce() pattern to make it scale cleanly.

@Gozala
Copy link
Contributor Author

Gozala commented Dec 17, 2021

"I have some encoders, squish them together so I have one thing please" and having to chain .or() is going to lead to the typical reduce() pattern to make it scale cleanly.

I don't know if it's not the long list of things doing .or(a).or(b).or(c)... isn't really that cumbersome IMO, but yeah supporting long lists in some way seems like a good idea.

@Gozala
Copy link
Contributor Author

Gozala commented Dec 17, 2021

I think I'll make few more changes to this pr specifically:

  1. Get rid of .composed getter this introduces.
  2. Introduce fromTable function that take care of composing things.
  3. Introduce some function that turns Decoder<B, P> -> CompositeDecoder<P> so it could be used if needed in place of silly getter. Mostly so that fromTable could be implemented.

@BigLep
Copy link

BigLep commented Jan 28, 2022

@Gozala : what are the next steps here? Is this work being carried forward?

@BigLep BigLep added the need/author-input Needs input from the original author label Jan 28, 2022
@lidel lidel marked this pull request as draft February 4, 2022 15:24
@lidel
Copy link
Member

lidel commented Feb 4, 2022

(question from triage) @Gozala would it be less disruptive if we update the interface instead?

@Gozala
Copy link
Contributor Author

Gozala commented Feb 17, 2022

(question from triage) @Gozala would it be less disruptive if we update the interface instead?

I’m not sure how would you do that without going and changing an implementation to comply with it, unless you ts-ignore it away.

@Gozala : what are the next steps here? Is this work being carried forward?

Sorry I missed that comment. We can either take this as is which is better that what we have and addresses the issue at hand, or I can try to find time to make changes discussed. Those changes could be future improvements however as they would just add more functions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
need/author-input Needs input from the original author
Projects
None yet
Development

Successfully merging this pull request may close these issues.

How to access .or prop of decoders
4 participants