Skip to content

Commit fdb1d35

Browse files
author
QuadStingray
committed
fix: compatibility for scala 2.12 in LuceneQueryConverter
1 parent 8ffa9f3 commit fdb1d35

File tree

5 files changed

+26
-18
lines changed

5 files changed

+26
-18
lines changed

.github/workflows/main_test_release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ jobs:
3535
- name: Run tests
3636
run: |
3737
timedatectl
38-
sbt test
38+
sbt +test
3939
release:
4040
needs: test
4141
runs-on: ubuntu-latest

.github/workflows/other_test.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,4 @@ jobs:
3535
- name: Run tests
3636
run: |
3737
timedatectl
38-
sbt test
38+
sbt +test

build.sbt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ developers := List(
4040

4141
licenses += ("Apache-2.0", url("https://www.apache.org/licenses/LICENSE-2.0.html"))
4242

43-
crossScalaVersions := Seq("2.13.10", "2.12.15")
43+
crossScalaVersions := Seq("2.13.10", "2.12.17")
4444

4545
scalaVersion := crossScalaVersions.value.head
4646

src/main/scala/dev/mongocamp/driver/mongodb/lucene/LuceneQueryConverter.scala

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -143,18 +143,27 @@ object LuceneQueryConverter extends LazyLogging {
143143
private def generateRegexQuery(pattern: String, options: String): Map[String, String] = {
144144
Map("$regex" -> pattern, "$options" -> options)
145145
}
146-
private def checkAndConvertValue(s: String): Serializable = {
147-
try {
148-
if (s.toDoubleOption.getOrElse("").toString.equals(s)) {
149-
s.toDouble
150-
}
151-
else if (s.toLongOption.getOrElse("").toString.equals(s)) {
152-
s.toLong
146+
private def checkAndConvertValue(s: String): Any = {
147+
148+
def checkOrReturn[A <: Any](f: () => A): Option[A] = {
149+
try {
150+
val value = f()
151+
if (value.toString.equals(s)) {
152+
Option(value)
153+
}
154+
else {
155+
None
156+
}
153157
}
154-
else if (s.toBooleanOption.getOrElse("").toString.equals(s)) {
155-
s.toBoolean
158+
catch {
159+
case e: Exception => None
156160
}
157-
else {
161+
}
162+
163+
try {
164+
val convertedValue: Option[Any] =
165+
(List() ++ checkOrReturn(() => s.toDouble) ++ checkOrReturn(() => s.toLong) ++ checkOrReturn(() => s.toBoolean)).headOption
166+
convertedValue.getOrElse({
158167
val parsedOptions: Option[Date] = datePatters
159168
.map(pattern => {
160169
try {
@@ -169,7 +178,7 @@ object LuceneQueryConverter extends LazyLogging {
169178
.find(_.nonEmpty)
170179
.flatten
171180
parsedOptions.getOrElse(s)
172-
}
181+
})
173182
}
174183
catch {
175184
case _: Exception =>

src/test/scala/dev/mongocamp/driver/mongodb/gridfs/GridFSDatabaseSpec.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,14 @@ class GridFSDatabaseSpec extends Specification with GridfsDatabaseFunctions with
3636
ImageFilesDAO.renameFile(oid, "test.png").result()
3737
findImage(oid).getFilename must be equalTo "test.png"
3838

39-
val downloadPath = "/tmp/download_" + fileName
40-
val result: Long = downloadImage(oid, downloadPath)
39+
val downloadedFile = File.newTemporaryFile(suffix = fileName)
40+
val result: Long = downloadImage(oid, downloadedFile.toString())
4141

4242
result must not be equalTo(-1)
4343

44-
val downloadedFile = File(downloadPath)
4544
downloadedFile.exists must beTrue
4645

47-
val downloadBytes = File(downloadPath).bytes.toList
46+
val downloadBytes = downloadedFile.bytes.toList
4847

4948
downloadBytes.size must be equalTo uploadBytes.size
5049

0 commit comments

Comments
 (0)