Skip to content

Commit

Permalink
Fix paths for Windows
Browse files Browse the repository at this point in the history
Updated sbt version, sbt-github-packages plugin, .gitignore and build file.
Removed all Paths.get() calls.
ISOCountryCodesTest moved to tests.
  • Loading branch information
Prometheus3375 authored Aug 27, 2021
2 parents 563bb40 + b33f7b2 commit 830643e
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 31 deletions.
10 changes: 7 additions & 3 deletions .gitignore
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/
8 changes: 5 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@ lazy val commonSettings = (project in file(".")).
settings(
organization := "io.wunderschild",
name := "country-codes",
version := "0.0.3",
version := "0.0.4",
scalaVersion := "2.12.12",
crossScalaVersions := Seq("2.11.12", "2.12.12"),
githubOwner := "wunderschild",
githubRepository := "country-codes")
githubRepository := "country-codes"
)

val jacksonVersion = "2.10.5"

libraryDependencies ++= Seq(
"org.scala-lang" % "scala-reflect" % scalaVersion.value,
"org.scalatest" %% "scalatest" % "3.2.9" % Test,
"com.fasterxml.jackson.core" % "jackson-core" % jacksonVersion,
"com.fasterxml.jackson.core" % "jackson-annotations" % jacksonVersion,
"com.fasterxml.jackson.core" % "jackson-databind" % jacksonVersion,
"com.fasterxml.jackson.module" %% "jackson-module-scala" % jacksonVersion,
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-yaml" % jacksonVersion
)
)
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version = 1.3.8
sbt.version = 1.5.5
2 changes: 1 addition & 1 deletion project/plugins.sbt
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 src/main/scala/io/wunderschild/country_codes/ISOCountryCodes.scala
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)
}
}
9 changes: 9 additions & 0 deletions src/test/scala/ISOCountryCodesTest.scala
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)
}
}

0 comments on commit 830643e

Please sign in to comment.