-
-
Notifications
You must be signed in to change notification settings - Fork 97
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
[Draft] oneOf
support for query strings
#1018
Draft
GrafBlutwurst
wants to merge
2
commits into
endpoints4s:master
Choose a base branch
from
rivero-ag:feature-enum-query-string
base: master
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
133 changes: 133 additions & 0 deletions
133
json-schema/json-schema/src/main/scala/endpoints4s/Alternative.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
package endpoints4s | ||
|
||
import AlternativeTypes._ | ||
|
||
//Note this is mostly for discussion for potential syntax support in endpoints4s to deal with nested either. It can be thrown out of the MR as it has no dependencies | ||
|
||
//Anologous to Tupler but for Eithers | ||
//An either is fully qualified by its inject and fold. So to give users convenient albeit slightly odd syntax that folds and inject up to some arity (currently 4) | ||
//This can be thrown out entirely (probably tupler as well) when we're fully on scala 3 and no longer offer 2 support (so a long while from now) | ||
trait Alternative[A,B] { | ||
type Clauses[_] | ||
|
||
//These are userfriendly methods that can be used. As of right now it's very painful to deal with endpoints with lots of either nesting coming from `orElse`. This way a user doesn't need to have the sealed trait of the correct arity in scope | ||
def inject: Clauses[Either[A,B]] | ||
def fold[Result](either:Either[A,B])(clauses: Clauses[Result]):Result | ||
|
||
} | ||
|
||
object AlternativeTypes { | ||
//Alternatively this could be a trait but I think it would be more annoying at usage site, requiring creation of anonymous instances with `new` or having an apply in which case it boils down to the same thing really. | ||
case class Clauses2[A, B, Target]( | ||
first: A => Target, | ||
second: B => Target | ||
) | ||
case class Clauses3[A, B, C, Target]( | ||
first: A => Target, | ||
second: B => Target, | ||
third: C => Target, | ||
) | ||
case class Clauses4[A, B, C, D, Target]( | ||
first: A => Target, | ||
second: B => Target, | ||
third: C => Target, | ||
fourth: D => Target, | ||
) | ||
|
||
//Very common fold Operations can be offered here like Embedding into Right Chained either form e.g. Either[A,Either[B,Either[C,D]] or merge to a common super type (sealed trait injeciton) | ||
} | ||
|
||
trait Alternative2 { | ||
type Aux[A,B, Clauses0[_]] = Alternative[A,B] { | ||
type Clauses[Target] = Clauses0[Target] | ||
} | ||
|
||
//We have no partial type application, this is what kind projector does under the hood. If we want to offer `Alternative` to our users we'll have to think on how to do that | ||
implicit def alternative2[A,B]: Aux[A,B, ({type L[T] = Clauses2[A,B,T]})#L] = new Alternative[A,B] { | ||
type Clauses[Target] = Clauses2[A,B,Target] | ||
//type SealedTraitRepr = Either2[A,B] | ||
|
||
override def inject:Clauses[Either[A,B]] = Clauses2( | ||
first = a => Left(a), | ||
second = b => Right(b), | ||
) | ||
|
||
override def fold[Result](either:Either[A,B])(clauses: Clauses[Result]):Result = either.fold(clauses.first, clauses.second) | ||
|
||
} | ||
} | ||
|
||
trait Alternative3 extends Alternative2 { | ||
|
||
implicit def alternative1Or2[A,B,C]: Aux[A,Either[B,C], ({type L[T] = Clauses3[A,B,C,T]})#L] = new Alternative[A,Either[B,C]] { | ||
type Clauses[Target] = Clauses3[A,B,C,Target] | ||
|
||
override def inject:Clauses[Either[A,Either[B,C]]] = Clauses3( | ||
first = a => Left(a), | ||
second = b => Right(Left(b)), | ||
third = c => Right(Right(c)) | ||
) | ||
|
||
override def fold[Result](either:Either[A,Either[B,C]])(clauses: Clauses[Result]):Result = either.fold(clauses.first, _.fold(clauses.second, clauses.third)) | ||
|
||
} | ||
|
||
implicit def alternative2Or1[A,B,C]: Aux[Either[A,B],C, ({type L[T] = Clauses3[A,B,C,T]})#L] = new Alternative[Either[A,B],C] { | ||
type Clauses[Target] = Clauses3[A,B,C,Target] | ||
|
||
override def inject:Clauses[Either[Either[A,B],C]] = Clauses3( | ||
first = a => Left(Left(a)), | ||
second = b => Left(Right(b)), | ||
third = c => Right(c) | ||
) | ||
|
||
override def fold[Result](either:Either[Either[A,B],C])(clauses: Clauses[Result]):Result = either.fold(_.fold(clauses.first, clauses.second), clauses.third) | ||
|
||
} | ||
} | ||
|
||
//I didn't implement all layouts yet. 1or3 is the most common arising from chained `orElse` | ||
trait Alternative4 extends Alternative3 { | ||
|
||
implicit def alternative2Or2[A,B,C,D]: Aux[Either[A,B],Either[C,D], ({type L[T] = Clauses4[A,B,C,D,T]})#L] = new Alternative[Either[A,B],Either[C,D]] { | ||
type Clauses[Target] = Clauses4[A,B,C,D,Target] | ||
|
||
override def inject:Clauses[Either[Either[A,B],Either[C,D]]] = Clauses4( | ||
first = a => Left(Left(a)), | ||
second = b => Left(Right(b)), | ||
third = c => Right(Left(c)), | ||
fourth = d => Right(Right(d)) | ||
) | ||
|
||
override def fold[Result](either:Either[Either[A,B],Either[C,D]])(clauses: Clauses[Result]):Result = either.fold(_.fold(clauses.first, clauses.second), _.fold(clauses.third, clauses.fourth)) | ||
|
||
} | ||
|
||
|
||
implicit def alternative1Or3[A,B,C,D]: Aux[A,Either[B,Either[C,D]], ({type L[T] = Clauses4[A,B,C,D,T]})#L] = new Alternative[A,Either[B,Either[C,D]]] { | ||
type Clauses[Target] = Clauses4[A,B,C,D,Target] | ||
|
||
override def inject:Clauses[Either[A,Either[B,Either[C,D]]]] = Clauses4( | ||
first = a => Left(a), | ||
second = b => Right(Left(b)), | ||
third = c => Right(Right(Left(c))), | ||
fourth = d => Right(Right(Right(d))) | ||
) | ||
|
||
override def fold[Result](either:Either[A,Either[B,Either[C,D]]])(clauses: Clauses[Result]):Result = either.fold( | ||
clauses.first, | ||
_.fold( | ||
clauses.second, | ||
_.fold( | ||
clauses.third, | ||
clauses.fourth, | ||
) | ||
) | ||
) | ||
|
||
} | ||
|
||
} | ||
|
||
object Alternative extends Alternative4 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -83,6 +83,32 @@ trait Urls extends algebra.Urls { | |
) | ||
type QueryStringParam[A] = DocumentedQueryStringParam[A] | ||
|
||
//TODO: This is kinda ugly. But discriminated schemas make little sense on query strings? | ||
//Stil maybe this should be done better, but I haven't yet fully understood the openapi schema to figure out how to deal with nesting here | ||
private def unpackSchema[A](qsp:QueryStringParam[A]):List[Schema] = qsp.schema match { | ||
case oneOf : Schema.OneOf => oneOf.alternatives match { | ||
case enumerated: Schema.EnumeratedAlternatives => enumerated.alternatives | ||
case _ => List(qsp.schema) | ||
} | ||
case _ => List(qsp.schema) | ||
} | ||
|
||
def oneOfQueryStringParam[A,B](qspa: QueryStringParam[A], qspb: QueryStringParam[B]): QueryStringParam[Either[A,B]] = { | ||
DocumentedQueryStringParam[Either[A,B]]( | ||
schema = Schema.OneOf( | ||
alternatives = Schema.EnumeratedAlternatives( | ||
unpackSchema(qspa) ::: unpackSchema(qspb) | ||
), | ||
description = None, //TODO: What to put here? | ||
example = None, //TODO: What to put here? | ||
title = None,//TODO: What to put here? | ||
), | ||
isRequired = qspa.isRequired || qspb.isRequired, //TODO: This feels wrong? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here too, this is a signal that the signature of |
||
encoder = (either:Either[A,B]) => either.fold(qspa.encoder.encode, qspb.encoder.encode), | ||
|
||
) | ||
} | ||
|
||
implicit def optionalQueryStringParam[A](implicit | ||
param: QueryStringParam[A] | ||
): QueryStringParam[Option[A]] = | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Indeed, this is a signal that tells us that something is wrong with our approach. Most likely we need an intermediate structure somewhere to avoid this.