Skip to content

Commit edd05a3

Browse files
Changes Xor to Either
1 parent e54565a commit edd05a3

File tree

4 files changed

+26
-26
lines changed

4 files changed

+26
-26
lines changed

client/shared/src/main/scala/org/scalaexercises/evaluator/EvaluatorClient.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
package org.scalaexercises.evaluator
77

8-
import cats.data.XorT
8+
import cats.data.EitherT
99
import cats.~>
1010
import cats.implicits._
1111
import org.scalaexercises.evaluator.EvaluatorResponses.{EvalIO, EvaluationException, EvaluationResponse, EvaluationResult}
@@ -25,15 +25,17 @@ object EvaluatorClient {
2525
def apply(url: String, authKey: String) =
2626
new EvaluatorClient(url, authKey)
2727

28-
implicit class EvaluationIOSyntaxXOR[A](
28+
implicit class EvaluationIOSyntaxEither[A](
2929
evalIO: EvalIO[EvaluationResponse[A]]) {
3030

3131
def exec(
3232
implicit I: (EvaluatorOp ~> Future)): Future[EvaluationResponse[A]] =
3333
evalIO foldMap I
3434

35-
def liftEvaluator: XorT[EvalIO, EvaluationException, EvaluationResult[A]] =
36-
XorT[EvalIO, EvaluationException, EvaluationResult[A]](evalIO)
35+
def liftEvaluator: EitherT[EvalIO,
36+
EvaluationException,
37+
EvaluationResult[A]] =
38+
EitherT[EvalIO, EvaluationException, EvaluationResult[A]](evalIO)
3739

3840
}
3941
}

client/shared/src/main/scala/org/scalaexercises/evaluator/EvaluatorResponses.scala

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ package org.scalaexercises.evaluator
77

88
import cats.data.Xor
99
import cats.free.Free
10-
import cats.syntax.xor._
10+
import cats.implicits._
1111
import io.circe.Decoder
1212
import io.circe.parser._
1313
import io.circe.generic.auto._
@@ -23,7 +23,7 @@ object EvaluatorResponses {
2323

2424
type EvalIO[A] = Free[EvaluatorOp, A]
2525

26-
type EvaluationResponse[A] = EvaluationException Xor EvaluationResult[A]
26+
type EvaluationResponse[A] = Either[EvaluationException, EvaluationResult[A]]
2727

2828
case class EvaluationResult[A](result: A,
2929
statusCode: Int,
@@ -44,18 +44,17 @@ object EvaluatorResponses {
4444
implicit D: Decoder[A]): Future[EvaluationResponse[A]] =
4545
futureResponse map {
4646
case r if isSuccess(r.statusCode)
47-
decode[A](r.body).fold(
48-
e
49-
JsonParsingException(e.getMessage, r.body)
50-
.left[EvaluationResult[A]],
51-
result
52-
Xor.Right(
47+
decode[A](r.body) match {
48+
case Xor.Left(e) =>
49+
Either.left(JsonParsingException(e.getMessage, r.body))
50+
case Xor.Right(result) =>
51+
Either.right(
5352
EvaluationResult(result, r.statusCode, r.headers.toLowerCase))
54-
)
53+
}
5554
case r
56-
UnexpectedException(
57-
s"Failed invoking get with status : ${r.statusCode}, body : \n ${r.body}")
58-
.left[EvaluationResult[A]]
55+
Either.left(
56+
UnexpectedException(
57+
s"Failed i(nvoking get with status : ${r.statusCode}, body : \n ${r.body}"))
5958
}
6059

6160
private[this] def isSuccess(statusCode: Int) =

server/src/test/scala/org/scalaexercises/evaluator/EvalEndpointSpec.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class EvalEndpointSpec extends FunSpec with Matchers {
4848
)
4949
)
5050
).putHeaders(authHeader))
51-
.unsafePerformSync
51+
.run
5252

5353
def verifyEvalResponse(
5454
response: Response,
@@ -58,7 +58,7 @@ class EvalEndpointSpec extends FunSpec with Matchers {
5858
) = {
5959

6060
response.status should be(expectedStatus)
61-
val evalResponse = response.as[EvalResponse].unsafePerformSync
61+
val evalResponse = response.as[EvalResponse].run
6262
evalResponse.value should be(expectedValue)
6363
evalResponse.msg should be(expectedMessage)
6464
}

server/src/test/scala/org/scalaexercises/evaluator/EvaluatorSpec.scala

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ class EvaluatorSpec extends FunSpec with Matchers {
1717

1818
describe("evaluation") {
1919
it("can evaluate simple expressions") {
20-
val result: EvalResult[Int] =
21-
evaluator.eval("{ 41 + 1 }").unsafePerformSync
20+
val result: EvalResult[Int] = evaluator.eval("{ 41 + 1 }").run
2221

2322
result should matchPattern {
2423
case EvalSuccess(_, 42, _)
@@ -27,7 +26,7 @@ class EvaluatorSpec extends FunSpec with Matchers {
2726

2827
it("fails with a timeout when takes longer than the configured timeout") {
2928
val result: EvalResult[Int] =
30-
evaluator.eval("{ while(true) {}; 123 }").unsafePerformSync
29+
evaluator.eval("{ while(true) {}; 123 }").run
3130

3231
result should matchPattern {
3332
case Timeout(_)
@@ -52,7 +51,7 @@ Eval.now(42).value
5251
remotes = remotes,
5352
dependencies = dependencies
5453
)
55-
.unsafePerformSync
54+
.run
5655

5756
result should matchPattern {
5857
case EvalSuccess(_, 42, _) =>
@@ -79,14 +78,14 @@ Eval.now(42).value
7978
remotes = remotes,
8079
dependencies = dependencies1
8180
)
82-
.unsafePerformSync
81+
.run
8382
val result2: EvalResult[Int] = evaluator
8483
.eval(
8584
code,
8685
remotes = remotes,
8786
dependencies = dependencies2
8887
)
89-
.unsafePerformSync
88+
.run
9089

9190
result1 should matchPattern {
9291
case EvalSuccess(_, 42, _) =>
@@ -113,7 +112,7 @@ Asserts.scalaTestAsserts(true)
113112
remotes = remotes,
114113
dependencies = dependencies
115114
)
116-
.unsafePerformSync
115+
.run
117116

118117
result should matchPattern {
119118
case EvalSuccess(_, (), _) =>
@@ -137,7 +136,7 @@ Asserts.scalaTestAsserts(false)
137136
remotes = remotes,
138137
dependencies = dependencies
139138
)
140-
.unsafePerformSync
139+
.run
141140

142141
result should matchPattern {
143142
case EvalRuntimeError(

0 commit comments

Comments
 (0)