Skip to content

Commit

Permalink
Merge pull request #243 from ua-parser/drop-old-scala
Browse files Browse the repository at this point in the history
Drop support for scala 2.10 and 2.11
  • Loading branch information
thyandrecardoso authored Aug 23, 2024
2 parents 8557c7b + c8d87f1 commit 80fcd07
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 24 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scala.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
runs-on: ubuntu-latest
strategy:
matrix:
scala-version: [2.10.7, 2.11.12, 2.12.19, 2.13.14, 3.1.1]
scala-version: [2.12.19, 2.13.14, 3.1.1]
steps:
- name: Checkout repository and submodules
uses: actions/checkout@v2
Expand Down
17 changes: 10 additions & 7 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,36 @@ import ReleaseTransformations._
name := "uap-scala"
organization := "org.uaparser"

scalaVersion := "2.13.14"
crossScalaVersions := Seq("2.12.19", "2.13.14", "3.1.1")

scalacOptions ++= Seq(
"-Xfatal-warnings",
"-deprecation",
"-encoding", "UTF-8",
"-feature",
"-unchecked"
)

val scala2Flags = Seq(
"-Xlint:adapted-args",
"-Xsource:3",
"-Ywarn-dead-code",
"-Ywarn-numeric-widen",
"-Xfuture"
"-Ywarn-unused:imports"
)

scalacOptions := {
CrossVersion.partialVersion(scalaVersion.value) match {
case Some((3, _)) =>
scalacOptions.value :+ "-language:implicitConversions"
case Some((2, scalaMajor)) if scalaMajor >= 11 =>
scalacOptions.value ++ scala2Flags :+ "-Xlint:adapted-args"
case Some((2, _)) =>
scalacOptions.value ++ scala2Flags
case _ =>
scalacOptions.value ++ scala2Flags :+ "-Yno-adapted-args"
scalacOptions.value
}
}

scalaVersion := "2.13.14"
crossScalaVersions := Seq("2.10.7", "2.11.12", "2.12.19", "2.13.14", "3.1.1")

libraryDependencies += "org.yaml" % "snakeyaml" % "2.2"

libraryDependencies := {
Expand Down
7 changes: 7 additions & 0 deletions src/main/scala-2.12/org/uaparser/scala/scala.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.uaparser

package object scala {
object jdk {
val CollectionConverters = _root_.scala.collection.JavaConverters
}
}
7 changes: 7 additions & 0 deletions src/main/scala-2.13/org/uaparser/scala/scala.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.uaparser

package object scala {
object jdk {
val CollectionConverters = _root_.scala.jdk.CollectionConverters
}
}
7 changes: 7 additions & 0 deletions src/main/scala-3/org/uaparser/scala/scala.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package org.uaparser

package object scala {
object jdk {
val CollectionConverters = _root_.scala.jdk.CollectionConverters
}
}
10 changes: 1 addition & 9 deletions src/main/scala/org/uaparser/scala/Parser.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package org.uaparser.scala

import java.io.InputStream
import java.util.{List => JList, Map => JMap}
import org.uaparser.scala.Device.DeviceParser
import org.uaparser.scala.OS.OSParser
import org.uaparser.scala.UserAgent.UserAgentParser
import org.yaml.snakeyaml.{LoaderOptions, Yaml}
import org.yaml.snakeyaml.constructor.SafeConstructor
import scala.collection.JavaConverters._
import scala.util.Try

case class Parser(userAgentParser: UserAgentParser, osParser: OSParser, deviceParser: DeviceParser)
Expand All @@ -18,11 +14,7 @@ case class Parser(userAgentParser: UserAgentParser, osParser: OSParser, devicePa

object Parser {
def fromInputStream(source: InputStream): Try[Parser] = Try {
val yaml = new Yaml(new SafeConstructor(new LoaderOptions))
val javaConfig = yaml.load[JMap[String, JList[JMap[String, String]]]](source)
val config = javaConfig.asScala.toMap.mapValues(_.asScala.toList.map(_.asScala.toMap.filterNot {
case (_ , value) => value eq null
}))
val config = YamlUtil.loadYamlAsMap(source)
val userAgentParser = UserAgentParser.fromList(config.getOrElse("user_agent_parsers", Nil))
val osParser = OSParser.fromList(config.getOrElse("os_parsers", Nil))
val deviceParser = DeviceParser.fromList(config.getOrElse("device_parsers", Nil))
Expand Down
20 changes: 20 additions & 0 deletions src/main/scala/org/uaparser/scala/YamlUtil.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.uaparser.scala

import java.io.InputStream
import java.util.{List => JList, Map => JMap}
import jdk.CollectionConverters._
import org.yaml.snakeyaml.{LoaderOptions, Yaml}
import org.yaml.snakeyaml.constructor.SafeConstructor

private[scala] object YamlUtil {
def loadYamlAsMap(yamlStream: InputStream, loader: Yaml): Map[String, List[Map[String, String]]] = {
val javaConfig = loader.load[JMap[String, JList[JMap[String, String]]]](yamlStream)
javaConfig.asScala.map { case (k, v) =>
k -> v.asScala.map(_.asScala.filter{case (_, v) => v != null}.toMap).toList
}.toMap
}

def loadYamlAsMap(yamlStream: InputStream): Map[String, List[Map[String, String]]] = {
loadYamlAsMap(yamlStream, new Yaml(new SafeConstructor(new LoaderOptions)))
}
}
2 changes: 1 addition & 1 deletion src/test/scala/org/uaparser/scala/CachingParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package org.uaparser.scala
import java.io.InputStream

class CachingParserSpec extends ParserSpecBase {
val parser = CachingParser.default()
val parser: CachingParser = CachingParser.default()
def createFromStream(stream: InputStream): UserAgentStringParser = CachingParser.fromInputStream(stream).get
}
2 changes: 1 addition & 1 deletion src/test/scala/org/uaparser/scala/ParserSpec.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package org.uaparser.scala
import java.io.InputStream

class ParserSpec extends ParserSpecBase {
val parser = Parser.default
val parser: Parser = Parser.default
def createFromStream(stream: InputStream): UserAgentStringParser = Parser.fromInputStream(stream).get
}
7 changes: 2 additions & 5 deletions src/test/scala/org/uaparser/scala/ParserSpecBase.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import org.specs2.mutable.Specification
import org.yaml.snakeyaml.{LoaderOptions, Yaml}
import java.io.{ByteArrayInputStream, InputStream}
import java.nio.charset.StandardCharsets
import java.util.{List => JList, Map => JMap}

import scala.collection.JavaConverters._

trait ParserSpecBase extends Specification {
sequential
Expand All @@ -24,8 +21,8 @@ trait ParserSpecBase extends Specification {

def readCasesConfig(resource: String): List[Map[String, String]] = {
val stream = this.getClass.getResourceAsStream(resource)
val cases = yaml.load[JMap[String, JList[JMap[String, String]]]](stream)
.asScala.toMap.mapValues(_.asScala.toList.map(_.asScala.toMap))
val cases = YamlUtil.loadYamlAsMap(stream, yaml)

cases.getOrElse("test_cases", List()).filterNot(_.contains("js_ua")).map { config =>
config.filterNot { case (_, value) => value eq null }
}
Expand Down

0 comments on commit 80fcd07

Please sign in to comment.