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
21 
22 import java.time.{LocalDate, ZoneId, ZonedDateTime}
23 import akka.http.scaladsl.unmarshalling.Unmarshaller
24 import akka.stream.Materializer
25 import enumeratum.values.{StringEnum, StringEnumEntry}
26 import enumeratum.{Enum, EnumEntry}
27 import org.make.core.demographics.DemographicsCardId
28 import org.make.core.idea.IdeaId
29 import org.make.core.operation.OperationId
30 import org.make.core.personality.PersonalityRoleId
31 import org.make.core.proposal.{ProposalId, ProposalKeywordKey}
32 import org.make.core.question.QuestionId
33 import org.make.core.reference.{Country, LabelId, Language}
34 import org.make.core.tag.{TagId, TagTypeId}
35 import org.make.core.user.UserId
36 
37 import scala.concurrent.{ExecutionContext, Future}
38 import org.make.core.technical.Pagination._
39 import org.make.core.widget.SourceId
40 
41 @SuppressWarnings(Array("org.wartremover.warts.Throw"))
42 trait ParameterExtractors {
43 
44   implicit def eitherNoneOrT[T](
45     implicit unmarshaller: Unmarshaller[String, T]
46   ): Unmarshaller[String, Either[None.type, T]] = {
47 
48     Unmarshaller.identityUnmarshaller.transform {
49       implicit executionContext: ExecutionContext => implicit materializer: Materializer =>
50         _.flatMap {
51           case "None" => Future.successful(Left(None))
52           case other  => unmarshaller(other).map(Right(_))
53         }
54     }
55   }
56 
57   implicit val proposalKeywordKeyFromStringUnmarshaller: Unmarshaller[String, ProposalKeywordKey] =
58     Unmarshaller.strict[String, ProposalKeywordKey](ProposalKeywordKey.apply)
59 
60   implicit val localDateFromStringUnmarshaller: Unmarshaller[String, LocalDate] =
61     Unmarshaller.strict[String, LocalDate] { string =>
62       LocalDate.parse(string)
63     }
64 
65   implicit val zonedDateTimeFromStringUnmarshaller: Unmarshaller[String, ZonedDateTime] =
66     Unmarshaller.strict[String, ZonedDateTime] {
67       case value if value.contains('T') => ZonedDateTime.parse(value)
68       case value                        => LocalDate.parse(value).atStartOfDay(ZoneId.systemDefault())
69     }
70 
71   implicit val languageFromStringUnmarshaller: Unmarshaller[String, Language] =
72     Unmarshaller.strict[String, Language] { string =>
73       Language(string.toLowerCase())
74     }
75 
76   implicit val countryFromStringUnmarshaller: Unmarshaller[String, Country] =
77     Unmarshaller.strict[String, Country] { string =>
78       Country(string.toUpperCase())
79     }
80 
81   implicit val proposalIdFromStringUnmarshaller: Unmarshaller[String, ProposalId] =
82     Unmarshaller.strict[String, ProposalId] { string =>
83       ProposalId(string)
84     }
85 
86   implicit val userIdFromStringUnmarshaller: Unmarshaller[String, UserId] =
87     Unmarshaller.strict[String, UserId] { string =>
88       UserId(string)
89     }
90 
91   implicit val labelIdFromStringUnmarshaller: Unmarshaller[String, LabelId] =
92     Unmarshaller.strict[String, LabelId] { string =>
93       LabelId(string)
94     }
95 
96   implicit val tagIdFromStringUnmarshaller: Unmarshaller[String, TagId] =
97     Unmarshaller.strict[String, TagId] { string =>
98       TagId(string)
99     }
100 
101   implicit val tagTypeIdFromStringUnmarshaller: Unmarshaller[String, TagTypeId] =
102     Unmarshaller.strict[String, TagTypeId] { string =>
103       TagTypeId(string)
104     }
105 
106   implicit val operationIdFromStringUnmarshaller: Unmarshaller[String, OperationId] =
107     Unmarshaller.strict[String, OperationId] { string =>
108       OperationId(string)
109     }
110 
111   implicit val questionIdFromStringUnmarshaller: Unmarshaller[String, QuestionId] =
112     Unmarshaller.strict[String, QuestionId] { string =>
113       QuestionId(string)
114     }
115 
116   implicit val sourceIdFromStringUnmarshaller: Unmarshaller[String, SourceId] =
117     Unmarshaller.strict[String, SourceId] { string =>
118       SourceId(string)
119     }
120 
121   implicit val ideaIdFromStringUnmarshaller: Unmarshaller[String, IdeaId] =
122     Unmarshaller.strict[String, IdeaId] { string =>
123       IdeaId(string)
124     }
125 
126   implicit val personalityRoleIdFromStringUnmarshaller: Unmarshaller[String, PersonalityRoleId] =
127     Unmarshaller.strict[String, PersonalityRoleId] { role =>
128       PersonalityRoleId(role)
129     }
130 
131   implicit val demographicsCardIdFromStringUnmarshaller: Unmarshaller[String, DemographicsCardId] =
132     Unmarshaller.strict[String, DemographicsCardId] { role =>
133       DemographicsCardId(role)
134     }
135 
136   implicit def enumeratumEnumUnmarshaller[A <: EnumEntry](implicit basicEnum: Enum[A]): Unmarshaller[String, A] =
137     Unmarshaller.strict(basicEnum.withNameInsensitiveEither(_).fold(e => throw new Exception(e), identity))
138 
139   implicit def enumeratumStringEnumUnmarshaller[A <: StringEnumEntry](
140     implicit stringEnum: StringEnum[A]
141   ): Unmarshaller[String, A] =
142     Unmarshaller.strict(s => stringEnum.withValueEither(s).fold(e => throw new Exception(e), identity))
143 
144   implicit val startFromIntUnmarshaller: Unmarshaller[String, Offset] =
145     Unmarshaller.intFromStringUnmarshaller.map(Offset.apply)
146   implicit val endFromIntUnmarshaller: Unmarshaller[String, End] = Unmarshaller.intFromStringUnmarshaller.map(End.apply)
147   implicit val limitFromIntUnmarshaller: Unmarshaller[String, Limit] =
148     Unmarshaller.intFromStringUnmarshaller.map(Limit.apply)
149 
150 }
Line Stmt Id Pos Tree Symbol Tests Code
48 3154 1777 - 2064 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.transform akka.http.scaladsl.unmarshalling.Unmarshaller.identityUnmarshaller[String].transform[Either[None.type,T]](((implicit executionContext: scala.concurrent.ExecutionContext) => ((implicit materializer: akka.stream.Materializer) => ((x$1: scala.concurrent.Future[String]) => x$1.flatMap[Either[None.type,T]](((x0$1: String) => x0$1 match { case "None" => scala.concurrent.Future.successful[scala.util.Left[None.type,Nothing]](scala.`package`.Left.apply[None.type, Nothing](scala.None)) case (other @ _) => unmarshaller.apply(other)(executionContext, materializer).map[scala.util.Right[None.type,T]](((x$2: T) => scala.`package`.Right.apply[None.type, T](x$2)))(executionContext) }))(executionContext)))))
50 4274 1923 - 2058 ApplyToImplicitArgs scala.concurrent.Future.flatMap x$1.flatMap[Either[None.type,T]](((x0$1: String) => x0$1 match { case "None" => scala.concurrent.Future.successful[scala.util.Left[None.type,Nothing]](scala.`package`.Left.apply[None.type, Nothing](scala.None)) case (other @ _) => unmarshaller.apply(other)(executionContext, materializer).map[scala.util.Right[None.type,T]](((x$2: T) => scala.`package`.Right.apply[None.type, T](x$2)))(executionContext) }))(executionContext)
58 1357 2222 - 2246 Apply org.make.core.proposal.ProposalKeywordKey.apply org.make.core.proposal.ProposalKeywordKey.apply(value)
58 4571 2174 - 2247 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.proposal.ProposalKeywordKey](((value: String) => org.make.core.proposal.ProposalKeywordKey.apply(value)))
61 656 2335 - 2421 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, java.time.LocalDate](((string: String) => java.time.LocalDate.parse(string)))
62 2563 2392 - 2415 Apply java.time.LocalDate.parse java.time.LocalDate.parse(string)
66 2154 2517 - 2740 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, java.time.ZonedDateTime](((x0$1: String) => x0$1 match { case (value @ _) if scala.Predef.augmentString(value).contains('T') => java.time.ZonedDateTime.parse(value) case (value @ _) => java.time.LocalDate.parse(value).atStartOfDay(java.time.ZoneId.systemDefault()) }))
67 3004 2605 - 2631 Apply java.time.ZonedDateTime.parse java.time.ZonedDateTime.parse(value)
67 4800 2582 - 2601 Apply scala.collection.StringOps.contains scala.Predef.augmentString(value).contains('T')
68 938 2711 - 2733 Apply java.time.ZoneId.systemDefault java.time.ZoneId.systemDefault()
68 4206 2675 - 2734 Apply java.time.LocalDate.atStartOfDay java.time.LocalDate.parse(value).atStartOfDay(java.time.ZoneId.systemDefault())
72 2575 2826 - 2918 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.reference.Language](((string: String) => org.make.core.reference.Language.apply(string.toLowerCase())))
73 4512 2882 - 2912 Apply org.make.core.reference.Language.apply org.make.api.question.questionapitest org.make.core.reference.Language.apply(string.toLowerCase())
73 1193 2891 - 2911 Apply java.lang.String.toLowerCase org.make.api.question.questionapitest string.toLowerCase()
77 2704 3002 - 3092 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.reference.Country](((string: String) => org.make.core.reference.Country.apply(string.toUpperCase())))
78 507 3065 - 3085 Apply java.lang.String.toUpperCase org.make.api.question.questionapitest string.toUpperCase()
78 4744 3057 - 3086 Apply org.make.core.reference.Country.apply org.make.api.question.questionapitest org.make.core.reference.Country.apply(string.toUpperCase())
82 4217 3182 - 3264 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.proposal.ProposalId](((string: String) => org.make.core.proposal.ProposalId.apply(string)))
83 887 3240 - 3258 Apply org.make.core.proposal.ProposalId.apply org.make.core.proposal.ProposalId.apply(string)
87 1126 3346 - 3420 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.user.UserId](((string: String) => org.make.core.user.UserId.apply(string)))
88 2165 3400 - 3414 Apply org.make.core.user.UserId.apply org.make.api.organisation.organisationapitest org.make.core.user.UserId.apply(string)
92 2520 3504 - 3580 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.reference.LabelId](((string: String) => org.make.core.reference.LabelId.apply(string)))
93 4350 3559 - 3574 Apply org.make.core.reference.LabelId.apply org.make.core.reference.LabelId.apply(string)
97 4749 3660 - 3732 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.tag.TagId](((string: String) => org.make.core.tag.TagId.apply(string)))
98 449 3713 - 3726 Apply org.make.core.tag.TagId.apply org.make.core.tag.TagId.apply(string)
102 727 3820 - 3900 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.tag.TagTypeId](((string: String) => org.make.core.tag.TagTypeId.apply(string)))
103 2993 3877 - 3894 Apply org.make.core.tag.TagTypeId.apply org.make.core.tag.TagTypeId.apply(string)
107 2104 3992 - 4076 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.operation.OperationId](((string: String) => org.make.core.operation.OperationId.apply(string)))
108 4100 4051 - 4070 Apply org.make.core.operation.OperationId.apply org.make.core.operation.OperationId.apply(string)
112 4623 4166 - 4248 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.question.QuestionId](((string: String) => org.make.core.question.QuestionId.apply(string)))
113 1351 4224 - 4242 Apply org.make.core.question.QuestionId.apply org.make.core.question.QuestionId.apply(string)
117 459 4334 - 4412 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.widget.SourceId](((string: String) => org.make.core.widget.SourceId.apply(string)))
118 2376 4390 - 4406 Apply org.make.core.widget.SourceId.apply org.make.api.widget.adminwidgetapitest org.make.core.widget.SourceId.apply(string)
122 3000 4494 - 4568 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.idea.IdeaId](((string: String) => org.make.core.idea.IdeaId.apply(string)))
123 3769 4548 - 4562 Apply org.make.core.idea.IdeaId.apply org.make.core.idea.IdeaId.apply(string)
127 4202 4672 - 4764 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.personality.PersonalityRoleId](((role: String) => org.make.core.personality.PersonalityRoleId.apply(role)))
128 736 4735 - 4758 Apply org.make.core.personality.PersonalityRoleId.apply org.make.core.personality.PersonalityRoleId.apply(role)
132 1296 4870 - 4964 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, org.make.core.demographics.DemographicsCardId](((role: String) => org.make.core.demographics.DemographicsCardId.apply(role)))
133 2114 4934 - 4958 Apply org.make.core.demographics.DemographicsCardId.apply org.make.core.demographics.DemographicsCardId.apply(role)
137 4632 5153 - 5175 Throw <nosymbol> org.make.api.question.questionapitest throw new scala.`package`.Exception(e)
137 576 5104 - 5186 Apply scala.util.Either.fold org.make.api.tag.moderationtagapitest basicEnum.withNameInsensitiveEither(x$3).fold[A](((e: enumeratum.NoSuchMember[A]) => throw new scala.`package`.Exception(e)), ((x: A) => scala.Predef.identity[A](x)))
137 3782 5084 - 5187 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, A](((x$3: String) => basicEnum.withNameInsensitiveEither(x$3).fold[A](((e: enumeratum.NoSuchMember[A]) => throw new scala.`package`.Exception(e)), ((x: A) => scala.Predef.identity[A](x)))))
137 2570 5177 - 5185 Apply scala.Predef.identity org.make.api.tag.moderationtagapitest scala.Predef.identity[A](x)
142 4213 5359 - 5432 Apply scala.util.Either.fold stringEnum.withValueEither(s).fold[A](((e: enumeratum.values.NoSuchMember[String,enumeratum.values.ValueEnumEntry[String]]) => throw new scala.`package`.Exception(e)), ((x: A) => scala.Predef.identity[A](x)))
142 1009 5423 - 5431 Apply scala.Predef.identity scala.Predef.identity[A](x)
142 2237 5334 - 5433 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.strict org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.strict[String, A](((s: String) => stringEnum.withValueEither(s).fold[A](((e: enumeratum.values.NoSuchMember[String,enumeratum.values.ValueEnumEntry[String]]) => throw new scala.`package`.Exception(e)), ((x: A) => scala.Predef.identity[A](x)))))
142 2946 5399 - 5421 Throw <nosymbol> org.make.api.question.questionapitest throw new scala.`package`.Exception(e)
145 1305 5554 - 5566 Apply org.make.core.technical.Pagination.Offset.apply org.make.core.technical.Pagination.Offset.apply(offset)
145 4639 5511 - 5567 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.map org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.intFromStringUnmarshaller.map[org.make.core.technical.Pagination.Offset](((offset: Int) => org.make.core.technical.Pagination.Offset.apply(offset)))
146 588 5635 - 5688 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.map org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.intFromStringUnmarshaller.map[org.make.core.technical.Pagination.End](((end: Int) => org.make.core.technical.Pagination.End.apply(end)))
146 2518 5678 - 5687 Apply org.make.core.technical.Pagination.End.apply org.make.core.technical.Pagination.End.apply(end)
148 3753 5807 - 5818 Apply org.make.core.technical.Pagination.Limit.apply org.make.api.question.questionapitest org.make.core.technical.Pagination.Limit.apply(limit)
148 2955 5764 - 5819 Apply akka.http.scaladsl.unmarshalling.Unmarshaller.map org.make.api.user.adminuserapitest akka.http.scaladsl.unmarshalling.Unmarshaller.intFromStringUnmarshaller.map[org.make.core.technical.Pagination.Limit](((limit: Int) => org.make.core.technical.Pagination.Limit.apply(limit)))