Skip to content

Commit

Permalink
Reformat with scalafmt 3.7.15
Browse files Browse the repository at this point in the history
Executed command: scalafmt --non-interactive
  • Loading branch information
scala-steward committed Oct 25, 2023
1 parent c247eeb commit 3f67868
Show file tree
Hide file tree
Showing 11 changed files with 70 additions and 87 deletions.
4 changes: 2 additions & 2 deletions examples/fullapp/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ val grpcVersion = "1.50.1"
lazy val protos = crossProject(JSPlatform, JVMPlatform)
.in(file("protos"))
.settings(
Compile / PB.targets := Seq(
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
Compile / PB.targets := Seq(
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
scalapb.zio_grpc.ZioCodeGenerator -> (Compile / sourceManaged).value
),
Compile / PB.protoSources := Seq(
Expand Down
2 changes: 1 addition & 1 deletion examples/fullapp/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ val zioGrpcVersion = "0.6.0-rc6"

libraryDependencies ++= Seq(
"com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % zioGrpcVersion,
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.7"
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.7"
)

// For Scala.js:
Expand Down
8 changes: 4 additions & 4 deletions examples/fullapp/server/src/main/scala/ExampleServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ object RequestContext {

def fromMetadata(md: SafeMetadata): UIO[RequestContext] = for {
maybeValue <- md.get(ServeErrorKey)
value = maybeValue.getOrElse("") match {
case "1" | "true" => true
case _ => false
}
value = maybeValue.getOrElse("") match {
case "1" | "true" => true
case _ => false
}
} yield RequestContext(value)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ trait UserRepo {
case class UserRepoImpl() extends UserRepo {
def findUser(name: String): ZIO[Any, StatusException, User] = name match {
case "john" => ZIO.succeed(User("John"))
case _ =>
case _ =>
ZIO.fail(Status.UNAUTHENTICATED.withDescription("No access!").asException)
}
}
Expand Down Expand Up @@ -80,17 +80,16 @@ object GreeterServiceWithMetadata {
): IO[StatusException, User] =
for {
name <- rc.metadata
.get(UserKey)
.someOrFail(
Status.UNAUTHENTICATED
.withDescription("No user-key header provided")
.asException
)
.get(UserKey)
.someOrFail(
Status.UNAUTHENTICATED
.withDescription("No user-key header provided")
.asException
)
user <- userRepo.findUser(name)
} yield user

val layer
: ZLayer[UserRepo with GreetingsRepo, Nothing, ZGreeter[RequestContext]] =
val layer: ZLayer[UserRepo with GreetingsRepo, Nothing, ZGreeter[RequestContext]] =
ZLayer.fromFunction((userRepo: UserRepo, greetingsRepo: GreetingsRepo) =>
GreeterImpl(greetingsRepo).transformContextZIO(findUser(userRepo, _))
)
Expand Down
4 changes: 2 additions & 2 deletions examples/helloworld/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ resolvers ++= Resolver.sonatypeOssRepos("snapshots")
val grpcVersion = "1.50.1"

Compile / PB.targets := Seq(
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
scalapb.zio_grpc.ZioCodeGenerator -> (Compile / sourceManaged).value
)

libraryDependencies ++= Seq(
"io.grpc" % "grpc-netty" % grpcVersion,
"io.grpc" % "grpc-netty" % grpcVersion,
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion
)

Expand Down
2 changes: 1 addition & 1 deletion examples/helloworld/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ val zioGrpcVersion = "0.6.0-rc6"

libraryDependencies ++= Seq(
"com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % zioGrpcVersion,
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.10"
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.10"
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,7 @@ package zio_grpc.examples.helloworld

import io.grpc.examples.helloworld.helloworld.ZioHelloworld.GreeterClient
import io.grpc.examples.helloworld.helloworld.HelloRequest
import io.grpc.{
CallOptions,
ManagedChannelBuilder,
Metadata,
MethodDescriptor,
StatusException
}
import io.grpc.{CallOptions, ManagedChannelBuilder, Metadata, MethodDescriptor, StatusException}
import zio.Console._
import scalapb.zio_grpc.{SafeMetadata, ZClientInterceptor, ZManagedChannel}
import zio._
Expand All @@ -22,7 +16,7 @@ object HelloWorldClientMetadata extends zio.ZIOAppDefault {
def userToMetadata(user: User): UIO[SafeMetadata] =
for {
metadata <- SafeMetadata.make
_ <- metadata.put(UserKey, user.name)
_ <- metadata.put(UserKey, user.name)
} yield metadata

// An effect that fetches a User from the environment and transforms it to
Expand All @@ -45,7 +39,7 @@ object HelloWorldClientMetadata extends zio.ZIOAppDefault {
GreeterClient
.withMetadataZIO(userToMetadata(User("user1")))
.sayHello(HelloRequest("World"))
_ <- printLine(r1.message).orDie
_ <- printLine(r1.message).orDie
} yield ()

// Option 2: through a managed client
Expand All @@ -62,12 +56,12 @@ object HelloWorldClientMetadata extends zio.ZIOAppDefault {
r1 <-
client
.sayHello(HelloRequest("World"))
_ <- printLine(r1.message)
_ <- printLine(r1.message)
r2 <-
client
.withMetadataZIO(userToMetadata(User("user2")))
.sayHello(HelloRequest("World"))
_ <- printLine(r2.message)
_ <- printLine(r2.message)
} yield ()
}
}
Expand Down
6 changes: 3 additions & 3 deletions examples/routeguide/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ resolvers ++= Resolver.sonatypeOssRepos("snapshots")
val grpcVersion = "1.50.1"

Compile / PB.targets := Seq(
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
scalapb.gen(grpc = true) -> (Compile / sourceManaged).value,
scalapb.zio_grpc.ZioCodeGenerator -> (Compile / sourceManaged).value
)

libraryDependencies ++= Seq(
"io.grpc" % "grpc-netty" % grpcVersion,
"io.grpc" % "grpc-netty" % grpcVersion,
"com.thesamet.scalapb" %% "scalapb-runtime-grpc" % scalapb.compiler.Version.scalapbVersion,
"com.thesamet.scalapb" %% "scalapb-json4s" % "0.12.0"
"com.thesamet.scalapb" %% "scalapb-json4s" % "0.12.0"
)

run / fork := true
2 changes: 1 addition & 1 deletion examples/routeguide/project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ val zioGrpcVersion = "0.6.0-rc6"

libraryDependencies ++= Seq(
"com.thesamet.scalapb.zio-grpc" %% "zio-grpc-codegen" % zioGrpcVersion,
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.10"
"com.thesamet.scalapb" %% "compilerplugin" % "0.11.10"
)
Original file line number Diff line number Diff line change
Expand Up @@ -37,30 +37,27 @@ object RouteGuideClientApp extends ZIOAppDefault {

val features = RouteGuideServer.featuresDatabase.feature

/** Sends numPoints randomly chosen points from [[features]] with a variable
* delay in between. Prints the statistics when they are sent from the
* server.
/** Sends numPoints randomly chosen points from [[features]] with a variable delay in between. Prints the statistics
* when they are sent from the server.
*/
// start: recordRoute
def recordRoute(numPoints: Int) =
for {
summary <- RouteGuideClient.recordRoute(
ZStream
.repeatZIO(
nextIntBetween(0, features.size).map(features(_).getLocation)
)
.tap(p =>
printLine(s"Visiting (${p.latitude}, ${p.longitude})").orDie
)
.schedule(Schedule.spaced(300.millis))
.take(numPoints)
)
_ <- printLine(
s"Finished trip with ${summary.pointCount} points. " +
s"Passed ${summary.featureCount} features. " +
s"Travelled ${summary.distance} meters. " +
s"It took ${summary.elapsedTime} seconds."
)
ZStream
.repeatZIO(
nextIntBetween(0, features.size).map(features(_).getLocation)
)
.tap(p => printLine(s"Visiting (${p.latitude}, ${p.longitude})").orDie)
.schedule(Schedule.spaced(300.millis))
.take(numPoints)
)
_ <- printLine(
s"Finished trip with ${summary.pointCount} points. " +
s"Passed ${summary.featureCount} features. " +
s"Travelled ${summary.distance} meters. " +
s"It took ${summary.elapsedTime} seconds."
)
} yield ()
// end: recordRoute

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ class RouteGuideService(
routeNotesRef: Ref[Map[Point, List[RouteNote]]]
) extends ZioRouteGuide.RouteGuide {

/** Gets the [[io.grpc.examples.routeguide.route_guide.Feature]] at the
* requested [[Point]]. If no feature at that location exists, a NOT FOUND
* error is returned.
/** Gets the [[io.grpc.examples.routeguide.route_guide.Feature]] at the requested [[Point]]. If no feature at that
* location exists, a NOT FOUND error is returned.
*
* @param request
* the requested location for the feature.
Expand All @@ -31,8 +30,7 @@ class RouteGuideService(
.orElseFail(Status.NOT_FOUND.asException())
// end: getFeature

/** Streams all features contained within the given bounding {@link
* Rectangle}.
/** Streams all features contained within the given bounding {@link Rectangle}.
*
* @param request
* the bounding rectangle for the requested features.
Expand All @@ -41,9 +39,9 @@ class RouteGuideService(
def listFeatures(
request: Rectangle
): ZStream[Any, StatusException, Feature] = {
val left = request.getLo.longitude min request.getHi.longitude
val right = request.getLo.longitude max request.getHi.longitude
val top = request.getLo.latitude max request.getHi.latitude
val left = request.getLo.longitude min request.getHi.longitude
val right = request.getLo.longitude max request.getHi.longitude
val top = request.getLo.latitude max request.getHi.latitude
val bottom = request.getLo.latitude min request.getHi.latitude

ZStream.fromIterable(
Expand All @@ -56,9 +54,8 @@ class RouteGuideService(
}
// end: listFeatures

/** Gets a stream of points, and responds with statistics about the "trip":
* number of points, number of known features visited, total distance
* traveled, and total time spent.
/** Gets a stream of points, and responds with statistics about the "trip": number of points, number of known features
* visited, total distance traveled, and total time spent.
*
* @param request
* a stream of points to process
Expand All @@ -69,18 +66,16 @@ class RouteGuideService(
): ZIO[Any, StatusException, RouteSummary] =
// Zips each element with the previous element, initially accompanied by None.
request.zipWithPrevious
.runFold(RouteSummary()) {
case (summary, (maybePrevPoint, currentPoint)) =>
// Compute the next status based on the current status.
summary.copy(
pointCount = summary.pointCount + 1,
featureCount =
summary.featureCount + (if (findFeature(currentPoint).isDefined) 1
else 0),
distance = summary.distance + maybePrevPoint
.map(calcDistance(_, currentPoint))
.getOrElse(0)
)
.runFold(RouteSummary()) { case (summary, (maybePrevPoint, currentPoint)) =>
// Compute the next status based on the current status.
summary.copy(
pointCount = summary.pointCount + 1,
featureCount = summary.featureCount + (if (findFeature(currentPoint).isDefined) 1
else 0),
distance = summary.distance + maybePrevPoint
.map(calcDistance(_, currentPoint))
.getOrElse(0)
)
}
.timed // returns a new effect that times the execution
.map { case (duration, summary) =>
Expand Down Expand Up @@ -113,16 +108,14 @@ class RouteGuideService(
* @param location
* the location to check
* @return
* A non-empty option if a feature is defined at that point, None
* otherwise.
* A non-empty option if a feature is defined at that point, None otherwise.
*/
// start: findFeature
def findFeature(point: Point): Option[Feature] =
features.find(f => f.getLocation == point && f.name.nonEmpty)
// end: findFeature

/** Calculate the distance between two points using the "haversine" formula.
* The formula is based on
/** Calculate the distance between two points using the "haversine" formula. The formula is based on
* http://mathforum.org/library/drmath/view/51879.html.
*
* @param start
Expand All @@ -133,14 +126,14 @@ class RouteGuideService(
* The distance between the points in meters
*/
def calcDistance(start: Point, end: Point): Int = {
val r = 6371000 // earth radius in meters
val r = 6371000 // earth radius in meters
val CoordFactor: Double = 1e7
val lat1 = toRadians(start.latitude) / CoordFactor
val lat2 = toRadians(end.latitude) / CoordFactor
val lon1 = toRadians(start.longitude) / CoordFactor
val lon2 = toRadians(end.longitude) / CoordFactor
val deltaLat = lat2 - lat1
val deltaLon = lon2 - lon1
val lat1 = toRadians(start.latitude) / CoordFactor
val lat2 = toRadians(end.latitude) / CoordFactor
val lon1 = toRadians(start.longitude) / CoordFactor
val lon2 = toRadians(end.longitude) / CoordFactor
val deltaLat = lat2 - lat1
val deltaLon = lon2 - lon1

val a = sin(deltaLat / 2) * sin(deltaLat / 2)
+cos(lat1) * cos(lat2) * sin(deltaLon / 2) * sin(deltaLon / 2)
Expand Down

0 comments on commit 3f67868

Please sign in to comment.