1 /*
2  *  Make.org Core API
3  *  Copyright (C) 2018 Make.org
4  *
5  * This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9  *
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14  *
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  *
18  */
19 
20 package org.make.core.technical
21 
22 import scala.annotation.tailrec
23 import cats.Monad
24 import cats.data.ValidatedNec
25 import cats.implicits._
26 import org.make.core.{ValidationError, ValidationFailedError}
27 import cats.data.Validated._
28 
29 object ValidatedUtils {
30 
31   implicit class ValidatedNecWithUtils[T](value: ValidatedNec[ValidationError, T]) {
32 
33     def toValidationEither: Either[ValidationFailedError, T] =
34       value.leftMap(ValidationFailedError(_)).toEither
35 
36     @SuppressWarnings(Array("org.wartremover.warts.TryPartial"))
37     def throwIfInvalid(): T =
38       this.toValidationEither.toTry.get
39   }
40 
41   type ValidatedPartial[A] = ValidatedNec[ValidationError, A]
42 
43   implicit def validatedMonad: Monad[ValidatedPartial] = new Monad[ValidatedPartial] {
44 
45     override def pure[A](x: A): ValidatedPartial[A] =
46       x.validNec
47 
48     override def flatMap[A, B](fa: ValidatedPartial[A])(f: A => ValidatedPartial[B]): ValidatedPartial[B] =
49       fa match {
50         case Invalid(e) => Invalid(e)
51         case Valid(a)   => f(a)
52       }
53 
54     @tailrec
55     override def tailRecM[A, B](a: A)(f: A => ValidatedPartial[Either[A, B]]): ValidatedPartial[B] =
56       f(a) match {
57         case e: Invalid[_]      => e
58         case Valid(Left(nextA)) => tailRecM(nextA)(f)
59         case Valid(Right(b))    => b.validNec
60       }
61   }
62 }
Line Stmt Id Pos Tree Symbol Tests Code
34 3612 1168 - 1192 Apply org.make.core.ValidationFailedError.apply org.make.core.validationtest org.make.core.ValidationFailedError.apply(x$1)
34 2633 1154 - 1202 Select cats.data.Validated.toEither org.make.api.avro.avrocompatibilitytest,org.make.core.operation.operationofquestiontest,org.make.api.idea.ideasearchenginetest,org.make.api.technical.webflow.desertest,org.make.api.sequence.sequencecacheactortest,org.make.api.technical.streamutilstest,org.make.api.technical.retryablefuturetest,org.make.api.user.persistentuserservicecomponenttest,org.make.api.technical.directives.clientdirectivestest,org.make.api.crmtemplates.crmtemplatesservicetest,org.make.api.sessionhistory.sessionhistorycoordinatortest ValidatedNecWithUtils.this.value.leftMap[org.make.core.ValidationFailedError](((x$1: cats.data.NonEmptyChain[org.make.core.ValidationError]) => org.make.core.ValidationFailedError.apply(x$1))).toEither
38 3896 1305 - 1338 Select scala.util.Try.get org.make.core.operation.operationofquestiontest,org.make.api.technical.webflow.desertest,org.make.api.idea.ideasearchenginetest,org.make.api.avro.avrocompatibilitytest,org.make.api.sequence.sequencecacheactortest,org.make.api.technical.streamutilstest,org.make.api.technical.retryablefuturetest,org.make.api.user.persistentuserservicecomponenttest,org.make.api.technical.directives.clientdirectivestest,org.make.api.crmtemplates.crmtemplatesservicetest,org.make.api.sessionhistory.sessionhistorycoordinatortest this.toValidationEither.toTry(scala.this.<:<.refl[org.make.core.ValidationFailedError]).get
38 652 1329 - 1329 TypeApply scala.<:<.refl org.make.core.operation.operationofquestiontest,org.make.api.technical.webflow.desertest,org.make.api.avro.avrocompatibilitytest,org.make.api.idea.ideasearchenginetest,org.make.api.technical.streamutilstest,org.make.api.sequence.sequencecacheactortest,org.make.api.technical.retryablefuturetest,org.make.api.user.persistentuserservicecomponenttest,org.make.api.technical.directives.clientdirectivestest,org.make.api.crmtemplates.crmtemplatesservicetest,org.make.api.sessionhistory.sessionhistorycoordinatortest scala.this.<:<.refl[org.make.core.ValidationFailedError]
43 661 1464 - 1467 Apply org.make.core.technical.ValidatedUtils.$anon.<init> org.make.api.user.adminuserapitest,org.make.core.validationtest new $anon()
46 1928 1555 - 1565 TypeApply cats.syntax.ValidatedIdOpsBinCompat0.validNec cats.implicits.catsSyntaxValidatedIdBinCompat0[A](x).validNec[Nothing]
50 5117 1719 - 1729 Apply cats.data.Validated.Invalid.apply cats.data.Validated.Invalid.apply[cats.data.NonEmptyChain[org.make.core.ValidationError]](e)
51 4281 1757 - 1761 Apply scala.Function1.apply org.make.api.user.adminuserapitest,org.make.core.validationtest f.apply(a)
56 2150 1891 - 1895 Apply scala.Function1.apply f.apply(a)
57 5539 1939 - 1940 TypeApply scala.Any.asInstanceOf e.asInstanceOf[cats.data.Validated[cats.data.NonEmptyChain[org.make.core.ValidationError],B]]
58 3619 1976 - 1994 Apply org.make.core.technical.ValidatedUtils.$anon.tailRecM $anon.this.tailRecM[A, B](nextA)(f)
59 2431 2030 - 2040 TypeApply cats.syntax.ValidatedIdOpsBinCompat0.validNec cats.implicits.catsSyntaxValidatedIdBinCompat0[B](b).validNec[Nothing]