-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Updated sbt version, sbt-github-packages plugin, .gitignore and build file. Removed all Paths.get() calls. ISOCountryCodesTest moved to tests.
- Loading branch information
Showing
6 changed files
with
44 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
# Project exclude paths | ||
/project/target/ | ||
/target/ | ||
# IDEA | ||
.idea | ||
|
||
# sbt | ||
/.bsp/ | ||
/null/ | ||
target/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
sbt.version = 1.3.8 | ||
sbt.version = 1.5.5 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.2") | ||
addSbtPlugin("com.codecommit" % "sbt-github-packages" % "0.5.3") |
44 changes: 21 additions & 23 deletions
44
src/main/scala/io/wunderschild/country_codes/ISOCountryCodes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,54 @@ | ||
package io.wunderschild.country_codes | ||
|
||
import scala.io.Source | ||
import java.nio.file.Paths | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory | ||
import com.fasterxml.jackson.module.scala.DefaultScalaModule | ||
import CountryHelpers._ | ||
|
||
import scala.io.Source | ||
|
||
object ISOCountryCodes { | ||
private def using[A, B <: {def close(): Unit}] (closeable: B) (f: B => A): A = | ||
try { f(closeable) } finally { closeable.close() } | ||
private def using[A, B <: {def close(): Unit}](closeable: B)(f: B => A): A = { | ||
try { | ||
f(closeable) | ||
} finally { | ||
closeable.close() | ||
} | ||
} | ||
|
||
/** Create lookup table that can be used for fast search of country by name. | ||
* | ||
* @param localization language to be used for the country names | ||
*/ | ||
def apply( | ||
localization: String = "en", | ||
indexedFields: Seq[String] = Seq("officialName", "otherNames", "nationality") | ||
): LookupTable = { | ||
localization: String = "en", | ||
indexedFields: Seq[String] = Seq("officialName", "otherNames", "nationality") | ||
): LookupTable = { | ||
type mT = Map[String, Map[String, Any]] | ||
|
||
val mapper = new ObjectMapper(new YAMLFactory()) | ||
mapper.registerModule(DefaultScalaModule) | ||
|
||
val countriesPath = Paths.get("/countries/").toString | ||
val countriesPath = "/countries" | ||
|
||
val countryDataPaths = using(getClass.getResourceAsStream("/countries/hint")) { stream => | ||
Source.fromInputStream(stream).getLines | ||
.map(country => Paths.get(countriesPath, s"${country}.yaml").toString).toList | ||
Source.fromInputStream(stream).getLines.map( | ||
country => s"${countriesPath}/${country}.yaml" | ||
).toList | ||
} | ||
|
||
def readMap(path: String): mT = using(getClass.getResourceAsStream(path)) { stream => | ||
mapper.readValue(stream, classOf[mT]) | ||
def readMap(path: String): mT = using(getClass.getResourceAsStream(path)) { | ||
stream => mapper.readValue(stream, classOf[mT]) | ||
} | ||
|
||
val localizationPath = Paths.get(s"/localization/${localization.toLowerCase}.yaml").toString | ||
val localizationPath = s"/localization/${localization.toLowerCase}.yaml" | ||
val localizationMap = readMap(localizationPath) | ||
val countryDataMap = countryDataPaths.map{ countryDataPath => | ||
val countryDataMap = countryDataPaths.map { countryDataPath => | ||
readMap(countryDataPath).mapValues(data => { | ||
(data ++ localizationMap(data("alpha2").asInstanceOf[String])).toCaseClass[Country] | ||
(data ++ localizationMap(data("alpha2").asInstanceOf[String])).toCaseClass[Country] | ||
}) | ||
} | ||
|
||
new LookupTable(countryDataMap.flatMap(_.values), indexedFields) | ||
} | ||
} | ||
|
||
object ISOCountryCodesTest { | ||
def main(args: Array[String]): Unit = { | ||
val iso = ISOCountryCodes("ru", Seq("officialName", "otherNames", "nationality")) | ||
require(iso.lookup("Russian").isDefined) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import io.wunderschild.country_codes.ISOCountryCodes | ||
import org.scalatest.funsuite.AnyFunSuite | ||
|
||
class ISOCountryCodesTest extends AnyFunSuite { | ||
test("ISOCountryCodes") { | ||
val iso = ISOCountryCodes("ru", Seq("officialName", "otherNames", "nationality")) | ||
require(iso.lookup("Russian").isDefined) | ||
} | ||
} |