From 3f67868763acba87d094e0415684147ce207b386 Mon Sep 17 00:00:00 2001 From: Scala Steward Date: Wed, 25 Oct 2023 04:40:45 +0000 Subject: [PATCH] Reformat with scalafmt 3.7.15 Executed command: scalafmt --non-interactive --- examples/fullapp/build.sbt | 4 +- examples/fullapp/project/plugins.sbt | 2 +- .../server/src/main/scala/ExampleServer.scala | 8 +-- .../scala/ExampleServerWithMetadata.scala | 17 +++--- examples/helloworld/build.sbt | 4 +- examples/helloworld/project/plugins.sbt | 2 +- .../helloworld/HelloWorldClientMetadata.scala | 16 ++--- examples/routeguide/build.sbt | 6 +- examples/routeguide/project/plugins.sbt | 2 +- .../routeguide/RouteGuideClientApp.scala | 35 +++++------ .../routeguide/RouteGuideServer.scala | 61 ++++++++----------- 11 files changed, 70 insertions(+), 87 deletions(-) diff --git a/examples/fullapp/build.sbt b/examples/fullapp/build.sbt index af12cf361..7ccd53b2d 100644 --- a/examples/fullapp/build.sbt +++ b/examples/fullapp/build.sbt @@ -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( diff --git a/examples/fullapp/project/plugins.sbt b/examples/fullapp/project/plugins.sbt index 77d8bd039..2cf89cb09 100644 --- a/examples/fullapp/project/plugins.sbt +++ b/examples/fullapp/project/plugins.sbt @@ -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: diff --git a/examples/fullapp/server/src/main/scala/ExampleServer.scala b/examples/fullapp/server/src/main/scala/ExampleServer.scala index 7c056a9ff..8c34c4af0 100644 --- a/examples/fullapp/server/src/main/scala/ExampleServer.scala +++ b/examples/fullapp/server/src/main/scala/ExampleServer.scala @@ -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) } diff --git a/examples/fullapp/server/src/main/scala/ExampleServerWithMetadata.scala b/examples/fullapp/server/src/main/scala/ExampleServerWithMetadata.scala index aa11924c4..ab96147de 100644 --- a/examples/fullapp/server/src/main/scala/ExampleServerWithMetadata.scala +++ b/examples/fullapp/server/src/main/scala/ExampleServerWithMetadata.scala @@ -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) } } @@ -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, _)) ) diff --git a/examples/helloworld/build.sbt b/examples/helloworld/build.sbt index 37dac1276..3efccc407 100644 --- a/examples/helloworld/build.sbt +++ b/examples/helloworld/build.sbt @@ -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 ) diff --git a/examples/helloworld/project/plugins.sbt b/examples/helloworld/project/plugins.sbt index 51e34ccab..b03ef113f 100644 --- a/examples/helloworld/project/plugins.sbt +++ b/examples/helloworld/project/plugins.sbt @@ -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" ) diff --git a/examples/helloworld/src/main/scala/zio_grpc/examples/helloworld/HelloWorldClientMetadata.scala b/examples/helloworld/src/main/scala/zio_grpc/examples/helloworld/HelloWorldClientMetadata.scala index b27bb200a..7e0fc101b 100644 --- a/examples/helloworld/src/main/scala/zio_grpc/examples/helloworld/HelloWorldClientMetadata.scala +++ b/examples/helloworld/src/main/scala/zio_grpc/examples/helloworld/HelloWorldClientMetadata.scala @@ -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._ @@ -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 @@ -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 @@ -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 () } } diff --git a/examples/routeguide/build.sbt b/examples/routeguide/build.sbt index 2447eaeac..eca423f57 100644 --- a/examples/routeguide/build.sbt +++ b/examples/routeguide/build.sbt @@ -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 diff --git a/examples/routeguide/project/plugins.sbt b/examples/routeguide/project/plugins.sbt index 51e34ccab..b03ef113f 100644 --- a/examples/routeguide/project/plugins.sbt +++ b/examples/routeguide/project/plugins.sbt @@ -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" ) diff --git a/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideClientApp.scala b/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideClientApp.scala index 0200e94fb..ce452b665 100644 --- a/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideClientApp.scala +++ b/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideClientApp.scala @@ -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 diff --git a/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideServer.scala b/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideServer.scala index 2e73c6a55..14eb7235c 100644 --- a/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideServer.scala +++ b/examples/routeguide/src/main/scala/zio_grpc/examples/routeguide/RouteGuideServer.scala @@ -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. @@ -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. @@ -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( @@ -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 @@ -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) => @@ -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 @@ -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)