-
Notifications
You must be signed in to change notification settings - Fork 399
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#46: authentication composition functions
- Loading branch information
Showing
9 changed files
with
237 additions
and
5 deletions.
There are no files selected for viewing
13 changes: 13 additions & 0 deletions
13
core/src/main/boilerplate/tapir/typelevel/ReplaceFirstInTuple.scala.template
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,13 @@ | ||
package tapir.typelevel | ||
|
||
/** | ||
* Tuples with the first element replaced: | ||
* IK = (I, A, B, C, ...) | ||
* JK = (J, A, B, B, ...) | ||
*/ | ||
trait ReplaceFirstInTuple[I, J, IK, JK] | ||
|
||
object ReplaceFirstInTuple { | ||
[1..21#implicit def replaceFirstInTuple1[I, J, [#A1#]]: ReplaceFirstInTuple[I, J, (I, [#A1#]), (J, [#A1#])] = null# | ||
] | ||
} |
24 changes: 24 additions & 0 deletions
24
core/src/main/scala/tapir/typelevel/ReplaceFirstInFn.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,24 @@ | ||
package tapir.typelevel | ||
|
||
/** | ||
* Replace the first parameter of a function from I to J. | ||
* FN_IK[R] = (IK as args) => R | ||
* FN_JK[R] = (JK as args) => R | ||
* IK = (I, A, B, C, ...) | ||
* JK = (J, A, B, B, ...) | ||
*/ | ||
trait ReplaceFirstInFn[I, FN_IK[_], J, FN_JK[_]] { | ||
def paramsAsArgsIk: ParamsAsArgs.Aux[_, FN_IK] | ||
def paramsAsArgsJk: ParamsAsArgs.Aux[_, FN_JK] | ||
} | ||
|
||
object ReplaceFirstInFn { | ||
implicit def replaceFirst[FN_IK[_], I, IK, J, JK, FN_JK[_]](implicit | ||
p1: ParamsAsArgs.Aux[IK, FN_IK], | ||
r: ReplaceFirstInTuple[I, J, IK, JK], | ||
p2: ParamsAsArgs.Aux[JK, FN_JK]): ReplaceFirstInFn[I, FN_IK, J, FN_JK] = | ||
new ReplaceFirstInFn[I, FN_IK, J, FN_JK] { | ||
override def paramsAsArgsIk: ParamsAsArgs.Aux[_, FN_IK] = p1 | ||
override def paramsAsArgsJk: ParamsAsArgs.Aux[_, FN_JK] = p2 | ||
} | ||
} |
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
62 changes: 62 additions & 0 deletions
62
server/akka-http-server/src/test/scala/tapir/server/akkahttp/RichToFutureFunctionTest.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,62 @@ | ||
package tapir.server.akkahttp | ||
|
||
import org.scalatest.concurrent.ScalaFutures | ||
import org.scalatest.{FunSuite, Matchers} | ||
|
||
import scala.concurrent.Future | ||
import scala.concurrent.ExecutionContext.Implicits.global | ||
|
||
class RichToFutureFunctionTest extends FunSuite with Matchers with ScalaFutures { | ||
|
||
case class Error(r: String) | ||
case class User(u: String) | ||
case class Result(r: String) | ||
|
||
test("should compose functions when both succeed") { | ||
// given | ||
def f1(p: String): Future[Either[Error, User]] = Future { | ||
Right(User(p)) | ||
} | ||
def f2(u: User, i: Int, s: String): Future[Either[Error, Result]] = Future { | ||
Right(Result(List(u.toString, i.toString, s).mkString(","))) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").futureValue | ||
|
||
// then | ||
result shouldBe Right(Result("User(john),10,x")) | ||
} | ||
|
||
test("should return error if first fails") { | ||
// given | ||
def f1(p: String): Future[Either[Error, User]] = Future { | ||
Left(Error("e1")) | ||
} | ||
def f2(u: User, i: Int, s: String): Future[Either[Error, Result]] = Future { | ||
Right(Result(List(u.toString, i.toString, s).mkString(","))) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").futureValue | ||
|
||
// then | ||
result shouldBe Left(Error("e1")) | ||
} | ||
|
||
test("should return error if second fails") { | ||
// given | ||
def f1(p: String): Future[Either[Error, User]] = Future { | ||
Right(User(p)) | ||
} | ||
def f2(u: User, i: Int, s: String): Future[Either[Error, Result]] = Future { | ||
Left(Error("e2")) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").futureValue | ||
|
||
// then | ||
result shouldBe Left(Error("e2")) | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
server/http4s-server/src/test/scala/tapir/server/http4s/RichToMonadFunctionTest.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,59 @@ | ||
package tapir.server.http4s | ||
|
||
import cats.effect.IO | ||
import org.scalatest.{FunSuite, Matchers} | ||
|
||
class RichToMonadFunctionTest extends FunSuite with Matchers { | ||
|
||
case class Error(r: String) | ||
case class User(u: String) | ||
case class Result(r: String) | ||
|
||
test("should compose functions when both succeed") { | ||
// given | ||
def f1(p: String): IO[Either[Error, User]] = IO { | ||
Right(User(p)) | ||
} | ||
def f2(u: User, i: Int, s: String): IO[Either[Error, Result]] = IO { | ||
Right(Result(List(u.toString, i.toString, s).mkString(","))) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").unsafeRunSync() | ||
|
||
// then | ||
result shouldBe Right(Result("User(john),10,x")) | ||
} | ||
|
||
test("should return error if first fails") { | ||
// given | ||
def f1(p: String): IO[Either[Error, User]] = IO { | ||
Left(Error("e1")) | ||
} | ||
def f2(u: User, i: Int, s: String): IO[Either[Error, Result]] = IO { | ||
Right(Result(List(u.toString, i.toString, s).mkString(","))) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").unsafeRunSync() | ||
|
||
// then | ||
result shouldBe Left(Error("e1")) | ||
} | ||
|
||
test("should return error if second fails") { | ||
// given | ||
def f1(p: String): IO[Either[Error, User]] = IO { | ||
Right(User(p)) | ||
} | ||
def f2(u: User, i: Int, s: String): IO[Either[Error, Result]] = IO { | ||
Left(Error("e2")) | ||
} | ||
|
||
// when | ||
val result = (f1 _).composeRight(f2 _).apply("john", 10, "x").unsafeRunSync() | ||
|
||
// then | ||
result shouldBe Left(Error("e2")) | ||
} | ||
} |