Skip to content

Commit f0bb0d2

Browse files
committed
feat: new version of scala-distances
BREAKING CHANGE: now returns elevationProfile and does not return/save all subpaths anymore
1 parent 5a1f21e commit f0bb0d2

File tree

15 files changed

+172
-56
lines changed

15 files changed

+172
-56
lines changed

.gitlab-ci.yml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ include:
55
compile scala-distances:
66
extends:
77
- .sbt-compile-cross
8-
tags: []
98

109
unused-dependencies:
1110
extends: .sbt-unused-dependencies
@@ -14,12 +13,9 @@ test scala-distances:
1413
extends:
1514
- .sbt-test-cross
1615
- .redis
17-
tags: []
1816

1917
version:
2018
extends: .version
21-
tags: []
2219

2320
publish on sonatype:
2421
extends: .sonatype-publish
25-
tags: []

build.sbt

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,28 @@ import CompileFlags._
22
import sbt.Keys.crossScalaVersions
33
import DependenciesScopesHandler._
44
import Dependencies._
5+
import PublishSettings.localCacheSettings
56

67
lazy val scala212 = "2.12.13"
78
lazy val scala213 = "2.13.10"
89
lazy val supportedScalaVersions = List(scala213, scala212)
910

10-
Global / onChangedBuildSource := ReloadOnSourceChanges
11-
ThisBuild / organization := "com.colisweb"
12-
ThisBuild / scalaVersion := scala213
13-
ThisBuild / crossScalaVersions := supportedScalaVersions
14-
ThisBuild / scalafmtOnCompile := true
15-
ThisBuild / scalafmtCheck := true
16-
ThisBuild / scalafmtSbtCheck := true
17-
ThisBuild / scalacOptions ++= crossScalacOptions(scalaVersion.value)
1811

19-
ThisBuild / pushRemoteCacheTo := Some(
20-
MavenCache("local-cache", baseDirectory.value / sys.env.getOrElse("CACHE_PATH", "sbt-cache"))
21-
)
12+
Global / onChangedBuildSource := ReloadOnSourceChanges
13+
14+
inThisBuild {
15+
List(
16+
organization := "com.colisweb",
17+
scalaVersion := scala213,
18+
crossScalaVersions := supportedScalaVersions,
19+
scalafmtOnCompile := true,
20+
scalafmtCheck := true,
21+
scalafmtSbtCheck := true,
22+
Test / fork := true,
23+
scalacOptions ++= crossScalacOptions(scalaVersion.value),
24+
localCacheSettings
25+
)
26+
}
2227
//// Main projects
2328

2429
lazy val root = Project(id = "scala-distances", base = file("."))
@@ -29,12 +34,9 @@ lazy val root = Project(id = "scala-distances", base = file("."))
2934

3035
lazy val core = project
3136
.settings(moduleName := "scala-distances-core")
37+
.settings(libraryDependencies ++= compileDependencies(cats, scalaCache, squants))
3238
.settings(
33-
libraryDependencies ++= compileDependencies(
34-
cats,
35-
scalaCache,
36-
squants
37-
) ++ testDependencies(
39+
libraryDependencies ++= testDependencies(
3840
monix,
3941
mockitoScalaScalatest,
4042
scalacheck,
@@ -49,13 +51,7 @@ lazy val `google-provider` = project
4951
.in(file("providers/google"))
5052
.settings(moduleName := "scala-distances-provider-google")
5153
.settings(
52-
libraryDependencies ++= compileDependencies(
53-
catsEffect,
54-
enumeratum,
55-
googleMaps,
56-
loggingInterceptor,
57-
refined
58-
)
54+
libraryDependencies ++= compileDependencies(catsEffect, enumeratum, googleMaps, loggingInterceptor, refined)
5955
)
6056
.dependsOn(core)
6157

@@ -96,10 +92,9 @@ lazy val `caffeine-cache` = project
9692
lazy val tests = project
9793
.settings(noPublishSettings)
9894
.dependsOn(core % "test->test;compile->compile", `google-provider`, `here-provider`, `redis-cache`, `caffeine-cache`)
99-
.settings(libraryDependencies ++= testDependencies(pureconfig, refinedPureconfig, scalaCacheCatsEffect))
95+
.settings(libraryDependencies ++= testDependencies(pureconfig, refinedPureconfig, scalaCacheCatsEffect, approvals))
10096

101-
/** Copied from Cats
102-
*/
97+
/** Copied from Cats */
10398
lazy val noPublishSettings = Seq(
10499
publish := {},
105100
publishLocal := {},

core/src/main/scala/com/colisweb/distances/bird/HaversineDistanceApi.scala

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ package com.colisweb.distances.bird
22

33
import cats.Applicative
44
import com.colisweb.distances.DistanceApi
5-
import com.colisweb.distances.model.path.DirectedPath
65
import com.colisweb.distances.model.{FixedSpeedTransportation, OriginDestination, PathResult}
76

87
class HaversineDistanceApi[F[_]: Applicative, P: OriginDestination: FixedSpeedTransportation]
98
extends DistanceApi[F, P] {
109
import com.colisweb.distances.model.syntax._
1110

1211
override def distance(path: P): F[PathResult] = {
13-
val distanceInKilometers = Haversine.distanceInKm(path.origin, path.destination)
14-
val timeInSeconds = DurationFromSpeed.durationForDistance(distanceInKilometers, path.speed)
15-
Applicative[F].pure(
16-
PathResult(distanceInKilometers, timeInSeconds, List(DirectedPath(path.origin, path.destination)))
17-
)
12+
val distanceInKilometers = Haversine.distanceInKm(path.origin, path.destination)
13+
val timeInSeconds = DurationFromSpeed.durationForDistance(distanceInKilometers, path.speed)
14+
val rollingResistanceCoefficient = 0.0125
15+
// we don't know the elevation so only the distance is taken into account
16+
val elevationProfile = distanceInKilometers * 1000 * rollingResistanceCoefficient
17+
Applicative[F].pure(PathResult(distanceInKilometers, timeInSeconds, Some(elevationProfile)))
1818
}
1919
}
Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
package com.colisweb.distances.model
22

3-
import com.colisweb.distances.model.path.DirectedPath
4-
5-
final case class PathResult(distance: DistanceInKm, duration: DurationInSeconds, paths: List[DirectedPath])
3+
final case class PathResult(
4+
distance: DistanceInKm,
5+
duration: DurationInSeconds,
6+
elevationProfile: Option[Double] = None
7+
) {
8+
val speedInKmPerHour: SpeedInKmH = distance / (duration / 3600)
9+
val speedInMetersPerSecond: Double = distance * 1000 / duration
10+
}

core/src/test/scala/com/colisweb/distances/cache/CacheSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ class CacheSpec extends AnyWordSpec with Matchers {
1919
}
2020

2121
private val path: DirectedPath = DirectedPath(Point(0, 0), Point(0, 0))
22-
private val pathResult: PathResult = PathResult(1, 1, List(path))
22+
private val pathResult: PathResult = PathResult(1, 1)
2323

2424
private val distanceMap: Distances[Try, DirectedPath] =
2525
FromMapDistances[Try]

core/src/test/scala/com/colisweb/distances/correction/CorrectPastDepartureTimeSpec.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class CorrectPastDepartureTimeSpec
3535

3636
override protected def beforeEach(): Unit = {
3737
Mockito.reset(base)
38-
base.distance(any[DirectedPathWithModeAt]) returns PathResult(0d, 0, Nil)
38+
base.distance(any[DirectedPathWithModeAt]) returns PathResult(0d, 0)
3939
()
4040
}
4141

core/src/test/scala/com/colisweb/distances/generator/Generators.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ object Generators {
6565
path <- genPathBetween
6666
distanceAndDuration <- genDistanceAndDurationUnrelated
6767
(distance, duration) = distanceAndDuration
68-
} yield (path, PathResult(distance, duration, List(DirectedPath(path.origin, path.destination))))
68+
} yield (path, PathResult(distance, duration))
6969

7070
def genPathSimpleAndDistanceUnrelatedSet: Gen[Map[DirectedPath, PathResult]] =
7171
Gen.listOf(genPathSimpleAndDistanceUnrelated).map(_.toMap)

project/Dependencies.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import sbt._
22

33
object Versions {
4+
final val approvals = "1.3.1"
45
final val cats = "2.9.0"
56
final val catsEffect = "2.3.3"
67
final val circe = "0.14.2"
@@ -15,14 +16,15 @@ object Versions {
1516
final val requests = "0.7.1"
1617
final val scalaCache = "0.28.0"
1718
final val scalacheck = "1.17.0"
18-
final val scalaCompat = "2.8.1"
19+
final val scalaCompat = "2.9.0"
1920
final val scalatest = "3.2.13"
2021
final val scalatestPlus = "3.1.0.0-RC2"
2122
final val squants = "1.8.3"
2223
}
2324

2425
object Dependencies {
2526

27+
final val approvals = "com.colisweb" %% "approvals-scala" % Versions.approvals
2628
final val cats = "org.typelevel" %% "cats-core" % Versions.cats
2729
final val catsEffect = "org.typelevel" %% "cats-effect" % Versions.catsEffect
2830
final val circe = "io.circe" %% "circe-core" % Versions.circe

project/PublishSettings.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../scala-common/scala/PublishSettings.scala

providers/google/src/main/scala/com/colisweb/distances/providers/google/GoogleDistanceDirectionsProvider.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class GoogleDistanceDirectionsProvider[F[_]](
3535
bestRoute = chooseBestRoute(response.routes.toList)
3636
distancesAndDuration = extractResponse(bestRoute)
3737
(distance, duration) = distancesAndDuration
38-
} yield PathResult(distance, duration, Nil)
38+
} yield PathResult(distance, duration)
3939

4040
}
4141

0 commit comments

Comments
 (0)