-
Notifications
You must be signed in to change notification settings - Fork 6
/
StateLike.scala
179 lines (158 loc) · 5.75 KB
/
StateLike.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
/*
* Copyright (c) 2020 etspaceman
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of
* this software and associated documentation files (the "Software"), to deal in
* the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
* the Software, and to permit persons to whom the Software is furnished to do so,
* subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
* IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package faker.states
import org.scalacheck.{Arbitrary, Gen}
import pureconfig.ConfigReader
import pureconfig.error.CannotParse
import faker.syntax.string._
import faker.{ResourceLoader, StringGenBuilder}
sealed trait StateLike {
def abbr: String
def name: String
def postalCodeGen: Gen[String]
}
object StateLike {
implicit val stateLikeConfigReader: ConfigReader[StateLike] =
ConfigReader.fromCursor(c =>
for {
objC <- c.asObjectCursor
tpeC <- objC.atKey("type")
tpe <- tpeC.asString
res <- tpe match {
case "us-state" => ConfigReader[UsState].from(c)
case "state" => ConfigReader[State].from(c)
case "province" => ConfigReader[Province].from(c)
case "district" => ConfigReader[District].from(c)
case "region" => ConfigReader[Region].from(c)
case "prefecture" => ConfigReader[Prefecture].from(c)
case "republic" => ConfigReader[Republic].from(c)
case "county" => ConfigReader[County].from(c)
case x =>
ConfigReader.Result.fail(
CannotParse(
s"StateLike ConfigReader could not parse type field value '$x'",
c.origin
)
)
}
} yield res
)
def states(implicit loader: ResourceLoader): Seq[StateLike] =
loader.loadKey[Seq[StateLike]]("address.states")
implicit def stateArbitrary(implicit
loader: ResourceLoader
): Arbitrary[StateLike] = Arbitrary(Gen.oneOf(states))
}
final case class UsState private (
abbr: String,
name: String,
minZip: Int,
maxZip: Int
) extends StateLike {
val zipCodes: Seq[String] =
minZip.until(maxZip + 1).map(x => "%05d".format(x))
private val zip5Gen: Gen[String] = Gen.oneOf(zipCodes)
private val zip9Gen: Gen[String] = for {
zip5 <- zip5Gen
zip4 <- "####".interpolatedGen
} yield s"$zip5-$zip4"
override val postalCodeGen: Gen[String] =
Gen.frequency(1 -> zip5Gen, 1 -> zip9Gen)
}
object UsState {
implicit val usStateConfigReader: ConfigReader[UsState] =
ConfigReader.forProduct4("abbr", "name", "min-zip", "max-zip")(
UsState(_, _, _, _)
)
}
final case class State(
abbr: String,
name: String,
postalCodeBuilder: StringGenBuilder
) extends StateLike {
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object State {
implicit val stateConfigReader: ConfigReader[State] =
ConfigReader.forProduct3("abbr", "name", "postal-code-builder")(
State(_, _, _)
)
}
final case class Province(
abbr: String,
name: String,
postalCodeBuilder: StringGenBuilder
) extends StateLike {
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object Province {
implicit val provinceConfigReader: ConfigReader[Province] =
ConfigReader.forProduct3("abbr", "name", "postal-code-builder")(
Province(_, _, _)
)
}
final case class District(name: String, postalCodeBuilder: StringGenBuilder)
extends StateLike {
override val abbr: String = name
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object District {
implicit val districtConfigReader: ConfigReader[District] =
ConfigReader.forProduct2("name", "postal-code-builder")(
District(_, _)
)
}
final case class Region(name: String, postalCodeBuilder: StringGenBuilder)
extends StateLike {
override val abbr: String = name
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object Region {
implicit val regionConfigReader: ConfigReader[Region] =
ConfigReader.forProduct2("name", "postal-code-builder")(Region(_, _))
}
final case class Prefecture(name: String, postalCodeBuilder: StringGenBuilder)
extends StateLike {
override val abbr: String = name
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object Prefecture {
implicit val prefectureConfigReader: ConfigReader[Prefecture] =
ConfigReader.forProduct2("name", "postal-code-builder")(Prefecture(_, _))
}
final case class Republic(name: String, postalCodeBuilder: StringGenBuilder)
extends StateLike {
override val abbr: String = name
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object Republic {
implicit val republicConfigReader: ConfigReader[Republic] =
ConfigReader.forProduct2("name", "postal-code-builder")(Republic(_, _))
}
final case class County(name: String, postalCodeBuilder: StringGenBuilder)
extends StateLike {
override val abbr: String = name
override val postalCodeGen: Gen[String] = postalCodeBuilder.gen
}
object County {
implicit val countyConfigReader: ConfigReader[County] =
ConfigReader.forProduct2("name", "postal-code-builder")(County(_, _))
}