-
Notifications
You must be signed in to change notification settings - Fork 6
/
address.scala
236 lines (202 loc) · 7.55 KB
/
address.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
* 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
import org.scalacheck.{Arbitrary, Gen}
import pureconfig.ConfigReader
import faker.syntax.string._
object address {
type BuildingNumber = BuildingNumber.Type
object BuildingNumber extends Newtype[String] { self =>
implicit def buildingNumberArbitrary(implicit
loader: ResourceLoader
): Arbitrary[BuildingNumber] =
Arbitrary(
Gen
.sequence[Seq[String], String](
loader
.loadKey[Seq[String]]("address.building-numbers")
.map(_.interpolatedGen)
)
.flatMap(x => Gen.oneOf(x))
.map(self.apply)
)
}
type City = City.Type
object City extends Newtype[String] { self =>
implicit def cityArbitrary(implicit
loader: ResourceLoader
): Arbitrary[City] =
Arbitrary(
loader
.loadKey[StringGenBuilder]("address.city-builder")
.gen
.map(self.apply)
)
implicit val cityConfigReader: ConfigReader[City] =
ConfigReader[String].map(self.apply)
}
type CityPrefix = CityPrefix.Type
object CityPrefix extends Newtype[String] { self =>
def cityPrefixes(implicit loader: ResourceLoader): Seq[CityPrefix] =
loader.loadKey[Seq[CityPrefix]]("address.city-prefixes")
implicit def cityPrefixArbitrary(implicit
loader: ResourceLoader
): Arbitrary[CityPrefix] =
Arbitrary(Gen.oneOf(cityPrefixes))
implicit val cityPrefixConfigReader: ConfigReader[CityPrefix] =
ConfigReader[String].map(self.apply)
}
type CitySuffix = CitySuffix.Type
object CitySuffix extends Newtype[String] { self =>
def citySuffixes(implicit loader: ResourceLoader): Seq[CitySuffix] =
loader.loadKey[Seq[CitySuffix]]("address.city-suffixes")
implicit def citySuffixArbitrary(implicit
loader: ResourceLoader
): Arbitrary[CitySuffix] =
Arbitrary(Gen.oneOf(citySuffixes))
implicit val citySuffixConfigReader: ConfigReader[CitySuffix] =
ConfigReader[String].map(self.apply)
}
final case class Country private (code: String, name: String)
object Country {
def countries(implicit loader: ResourceLoader): Seq[Country] =
loader.loadKey[Seq[Country]]("address.countries")
implicit def countryArbitrary(implicit
loader: ResourceLoader
): Arbitrary[Country] = Arbitrary(Gen.oneOf(countries))
implicit val countryConfigReader: ConfigReader[Country] =
ConfigReader.forProduct2("code", "name")(Country(_, _))
}
final case class DefaultCountry private (code: String, name: String)
object DefaultCountry {
def defaultCountries(implicit loader: ResourceLoader): Seq[DefaultCountry] =
loader.loadKey[Seq[DefaultCountry]]("address.default-countries")
implicit def defaultCountryArbitrary(implicit
loader: ResourceLoader
): Arbitrary[DefaultCountry] = Arbitrary(Gen.oneOf(defaultCountries))
implicit val defaultCountryConfigReader: ConfigReader[DefaultCountry] =
ConfigReader.forProduct2("code", "name")(DefaultCountry(_, _))
}
type FullAddress = FullAddress.Type
object FullAddress extends Newtype[String] { self =>
implicit def fullAddressArbitrary(implicit
loader: ResourceLoader
): Arbitrary[FullAddress] =
Arbitrary(
loader
.loadKey[StringGenBuilder]("address.full-address-builder")
.gen
.map(self.apply)
)
}
type Latitude = Latitude.Type
object Latitude extends Newtype[String] { self =>
implicit val latitudeArbitrary: Arbitrary[Latitude] = Arbitrary(
Gen
.choose[Double](0, 0.99)
.map(x => "%.8g".format((x * 180) - 90))
.map(self.apply)
)
}
type Longitude = Longitude.Type
object Longitude extends Newtype[String] { self =>
implicit val longitudeArbitrary: Arbitrary[Longitude] = Arbitrary(
Gen
.choose[Double](0, 0.99)
.map(x => "%.8g".format((x * 360) - 180))
.map(self.apply)
)
}
type PostalCode = PostalCode.Type
object PostalCode extends Newtype[String] { self =>
implicit def postalCodeArbitrary(implicit
loader: ResourceLoader
): Arbitrary[PostalCode] =
Arbitrary(
loader
.loadKey[StringGenBuilder]("address.postal-code-builder")
.gen
.map(self.apply)
)
}
type SecondaryAddress = SecondaryAddress.Type
object SecondaryAddress extends Newtype[String] { self =>
implicit def secondaryAddressArbitrary(implicit
loader: ResourceLoader
): Arbitrary[SecondaryAddress] =
Arbitrary(
Gen
.sequence[Seq[String], String](
loader
.loadKey[Seq[String]]("address.secondary-addresses")
.map(_.interpolatedGen)
)
.flatMap(x => Gen.oneOf(x))
.map(self.apply)
)
}
type StreetAddress = StreetAddress.Type
object StreetAddress extends Newtype[String] { self =>
implicit def streetAddressArbitrary(implicit
loader: ResourceLoader
): Arbitrary[StreetAddress] =
Arbitrary(
loader
.loadKey[StringGenBuilder]("address.street-address-builder")
.gen
.map(self.apply)
)
}
type StreetName = StreetName.Type
object StreetName extends Newtype[String] { self =>
implicit def streetNameArbitrary(implicit
loader: ResourceLoader
): Arbitrary[StreetName] =
Arbitrary(
loader
.loadKey[StringGenBuilder]("address.street-name-builder")
.gen
.map(self.apply)
)
}
type StreetPrefix = StreetPrefix.Type
object StreetPrefix extends Newtype[String] { self =>
def streetPrefixes(implicit loader: ResourceLoader): Seq[StreetPrefix] =
loader.loadKey[Seq[StreetPrefix]]("address.street-prefixes")
implicit def streetPrefixArbitrary(implicit
loader: ResourceLoader
): Arbitrary[StreetPrefix] =
Arbitrary(Gen.oneOf(streetPrefixes))
implicit val streetPrefixConfigReader: ConfigReader[StreetPrefix] =
ConfigReader[String].map(self.apply)
}
type StreetSuffix = StreetSuffix.Type
object StreetSuffix extends Newtype[String] { self =>
def streetSuffixes(implicit loader: ResourceLoader): Seq[StreetSuffix] =
loader.loadKey[Seq[StreetSuffix]]("address.street-suffixes")
implicit def streetSuffixArbitrary(implicit
loader: ResourceLoader
): Arbitrary[StreetSuffix] =
Arbitrary(Gen.oneOf(streetSuffixes))
implicit val streetSuffixConfigReader: ConfigReader[StreetSuffix] =
ConfigReader[String].map(self.apply)
}
}