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

Add PathVar map, emap #7310

Open
wants to merge 5 commits into
base: series/0.23
Choose a base branch
from

Conversation

fredshonorio
Copy link
Contributor

@fredshonorio fredshonorio commented Nov 5, 2023

Follow up from #2405

The motivation is that, as a user, in order to use make your own PathVar you have to make your own full extractor (e.g. LocalDateVar in https://http4s.org/v0.23/docs/dsl.html#handling-path-parameters) or make your own org.http4s.dsl.impl package so that you can access PathVar and its constructor.

I implemented map and emap in PathVar, and added StringVar which can be used as a starting point for creating custom PathVars. @diesalbla suggested a "super-trait PathMatch[A]" but I did not understand what the purpose would be, I'll happily make the adjustment.
As it is this would encourage user-defined PathVars to be vals and not objects, which becomes inconsistent with QueryParams for example. If this is a problem (I really have no opinion) then something like
abstract class CustomPathVar[A](cast: String => Option[A]) extends PathVar(...) would still be flexible enough, though less compositional.

I don't know if any of this is MiMa-approved (I ran mimaFindBinaryIssues, does that do anything?).

@mergify mergify bot added series/0.23 PRs targeting 0.23.x module:dsl labels Nov 5, 2023
@armanbilge
Copy link
Member

I ran mimaFindBinaryIssues

Not sure what that one does. CI runs mimaReportBinaryIssues.

@@ -40,6 +40,7 @@ trait RequestDsl extends Methods with Auth {
val IntVar: impl.IntVar.type
val LongVar: impl.LongVar.type
val UUIDVar: impl.UUIDVar.type
val StringVar: impl.StringVar.type
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 not backwards-compatible: we cannot add an abstract method to an open trait, since what happens to all the existing implementations that are missing this one?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Uh that's annoying. I don't know what could be done in this direction that doesn't end up changing RequestDsl.

Copy link
Member

@armanbilge armanbilge Nov 6, 2023

Choose a reason for hiding this comment

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

So I don't know anything about this corner of http4s and why it's written this way, but this would fix the compat issues:

Suggested change
val StringVar: impl.StringVar.type
lazy val StringVar: impl.StringVar.type = impl.StringVar

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 was lazy and didn't check it, I'll try again later and run the right mima check

@fredshonorio
Copy link
Contributor Author

MiMa no longer complains, and yet this feels less useful now 🥲. Before making this PR I didn't know you could just make an extractor (my fault, hadn't read the doc) so this seemed like a great idea. I'll leave it up to reviewers to decide if it's worth it.

new PathVar[B](str => cast(str).map(f))

def emap[B](f: A => Option[B]): PathVar[B] =
new PathVar[B](str => cast(str).flatMap(a => f(a).toRight(new NoStackTrace {}).toTry))
Copy link
Member

Choose a reason for hiding this comment

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

A little bit of bikeshedding, but this feels like too many allocations for the simple instantiation... Considering the PathVar nature (something that's going to be used for every request on a given path), could we tweak this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module:dsl series/0.23 PRs targeting 0.23.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants