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.reference
21 
22 import io.circe.{Decoder, Encoder, Json}
23 import org.make.core.StringValue
24 import spray.json.{JsString, JsValue, JsonFormat}
25 import com.github.plokhotnyuk.jsoniter_scala.core._
26 import org.make.core.Validation.StringWithParsers
27 import org.make.core.technical.ValidatedUtils.ValidatedNecWithUtils
28 
29 final case class Country(value: String) extends StringValue {
30   override def toString: String = value
31 
32   this.value.withMaxLength(3, "country").throwIfInvalid()
33 }
34 
35 object Country {
36 
37   // Make sure countries are always upper case
38   def apply(id: String): Country =
39     new Country(id.toUpperCase)
40 
41   implicit lazy val countryEncoder: Encoder[Country] =
42     (a: Country) => Json.fromString(a.value)
43   implicit lazy val countryDecoder: Decoder[Country] =
44     Decoder.decodeString.map(Country.apply)
45 
46   implicit val CountryFormatter: JsonFormat[Country] = new JsonFormat[Country] {
47     @SuppressWarnings(Array("org.wartremover.warts.Throw"))
48     override def read(json: JsValue): Country = json match {
49       case JsString(s) => Country(s)
50       case other       => throw new IllegalArgumentException(s"Unable to convert $other")
51     }
52 
53     override def write(obj: Country): JsValue = {
54       JsString(obj.value)
55     }
56   }
57 
58   implicit val countryCodec: JsonValueCodec[Country] =
59     StringValue.makeCodec(Country.apply)
60 }
61 
62 final case class Language(value: String) extends StringValue {
63   override def toString: String = value
64 
65   value.withMaxLength(3, "language").throwIfInvalid()
66 }
67 
68 object Language {
69 
70   def apply(id: String): Language =
71     new Language(id.toLowerCase)
72 
73   // Make sure languages are always lower case
74   implicit lazy val LanguageEncoder: Encoder[Language] =
75     (a: Language) => Json.fromString(a.value)
76   implicit lazy val LanguageDecoder: Decoder[Language] =
77     Decoder.decodeString.map(this.apply)
78 
79   implicit val LanguageFormatter: JsonFormat[Language] = new JsonFormat[Language] {
80     @SuppressWarnings(Array("org.wartremover.warts.Throw"))
81     override def read(json: JsValue): Language = json match {
82       case JsString(s) => Language(s)
83       case other       => throw new IllegalArgumentException(s"Unable to convert $other")
84     }
85 
86     override def write(obj: Language): JsValue = {
87       JsString(obj.value)
88     }
89   }
90 
91   implicit val languageCodec: JsonValueCodec[Language] =
92     StringValue.makeCodec(this.apply)
93 }
Line Stmt Id Pos Tree Symbol Tests Code
30 5099 1169 - 1174 Select org.make.core.reference.Country.value org.make.api.technical.tracking.trackingapitest Country.this.value
32 3260 1178 - 1233 Apply org.make.core.technical.ValidatedUtils.ValidatedNecWithUtils.throwIfInvalid org.make.core.operation.operationofquestiontest,org.make.api.makeunittest org.make.core.technical.ValidatedUtils.ValidatedNecWithUtils[org.make.core.Validation.StringWithMaxLength]({ <artifact> val qual$1: org.make.core.Validation.StringWithParsers = org.make.core.Validation.StringWithParsers(this.value); <artifact> val x$1: Int(3) = 3; <artifact> val x$2: String("country") = "country"; <artifact> val x$3: Option[org.make.core.reference.Language] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.withMaxLength$default$3; <artifact> val x$4: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.withMaxLength$default$4; qual$1.withMaxLength(3, "country", x$3, x$4) }).throwIfInvalid()
39 5510 1341 - 1368 Apply org.make.core.reference.Country.<init> org.make.core.operation.operationofquestiontest,org.make.api.makeunittest new Country(id.toUpperCase())
39 1217 1353 - 1367 Apply java.lang.String.toUpperCase org.make.core.operation.operationofquestiontest,org.make.api.makeunittest id.toUpperCase()
46 1756 1625 - 1628 Apply org.make.core.reference.Country.$anon.<init> org.make.core.operation.operationofquestiontest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.makeunittest new $anon()
49 3398 1798 - 1808 Apply org.make.core.reference.Country.apply org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest Country.apply(s)
50 1472 1835 - 1898 Throw <nosymbol> throw new scala.`package`.IllegalArgumentException(("Unable to convert ".+(other): String))
54 4757 1971 - 1980 Select org.make.core.reference.Country.value org.scalatest.testsuite obj.value
54 2875 1962 - 1981 Apply spray.json.JsString.apply org.scalatest.testsuite spray.json.JsString.apply(obj.value)
59 5035 2074 - 2087 Apply org.make.core.reference.Country.apply org.make.core.proposal.indexed.proposaltest Country.apply(id)
59 3113 2052 - 2088 Apply org.make.core.StringValue.makeCodec org.make.core.operation.operationofquestiontest,org.make.api.sessionhistory.sessionhistorycoordinatortest,org.make.api.makeunittest org.make.core.StringValue.makeCodec[org.make.core.reference.Country](((id: String) => Country.apply(id)))
63 1230 2189 - 2194 Select org.make.core.reference.Language.value org.make.api.technical.tracking.trackingapitest,org.scalatest.testsuite,org.make.core.technical.multilingualtest Language.this.value
65 4524 2198 - 2249 Apply org.make.core.technical.ValidatedUtils.ValidatedNecWithUtils.throwIfInvalid 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 org.make.core.technical.ValidatedUtils.ValidatedNecWithUtils[org.make.core.Validation.StringWithMaxLength]({ <artifact> val qual$1: org.make.core.Validation.StringWithParsers = org.make.core.Validation.StringWithParsers(Language.this.value); <artifact> val x$1: Int(3) = 3; <artifact> val x$2: String("language") = "language"; <artifact> val x$3: Option[org.make.core.reference.Language] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.withMaxLength$default$3; <artifact> val x$4: Option[String] @scala.reflect.internal.annotations.uncheckedBounds = qual$1.withMaxLength$default$4; qual$1.withMaxLength(3, "language", x$3, x$4) }).throwIfInvalid()
71 3412 2325 - 2339 Apply java.lang.String.toLowerCase 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 id.toLowerCase()
71 1416 2312 - 2340 Apply org.make.core.reference.Language.<init> org.make.core.operation.operationofquestiontest,org.make.api.idea.ideasearchenginetest,org.make.api.technical.webflow.desertest,org.make.api.avro.avrocompatibilitytest,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 new Language(id.toLowerCase())
79 3326 2648 - 2651 Apply org.make.core.reference.Language.$anon.<init> org.make.api.avro.avrocompatibilitytest,org.make.api.technical.webflow.desertest,org.make.core.operation.operationofquestiontest,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 new $anon()
82 4962 2823 - 2834 Apply org.make.core.reference.Language.apply org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest Language.apply(s)
83 2887 2861 - 2924 Throw <nosymbol> throw new scala.`package`.IllegalArgumentException(("Unable to convert ".+(other): String))
87 2015 2998 - 3007 Select org.make.core.reference.Language.value org.make.api.technical.crm.crmservicecomponenttest,org.make.api.userhistory.userhistorytest obj.value
87 5045 2989 - 3008 Apply spray.json.JsString.apply org.make.api.userhistory.userhistorytest,org.make.api.technical.crm.crmservicecomponenttest spray.json.JsString.apply(obj.value)
92 1353 3103 - 3113 Apply org.make.core.reference.Language.apply org.make.core.proposal.indexed.proposaltest this.apply(id)
92 4567 3081 - 3114 Apply org.make.core.StringValue.makeCodec org.make.core.operation.operationofquestiontest,org.make.api.idea.ideasearchenginetest,org.make.api.technical.webflow.desertest,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 org.make.core.StringValue.makeCodec[org.make.core.reference.Language](((id: String) => this.apply(id)))