From 94e3c1bd9a71f361ecb8a657caa96c83a74909b3 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 7 May 2020 12:19:56 +0200 Subject: [PATCH 01/12] Create compendium mode execution --- .../higherkindness/mu/rpc/srcgen/Model.scala | 6 ++ .../srcgen/compendium/CompendiumClient.scala | 89 +++++++++++++++++++ .../srcgen/compendium/CompendiumError.scala | 23 +++++ .../srcgen/compendium/CompendiumMode.scala | 57 ++++++++++++ .../rpc/srcgen/compendium/ErrorResponse.scala | 11 +++ .../mu/rpc/srcgen/compendium/HttpConfig.scala | 19 ++++ .../rpc/srcgen/compendium/RawProtocol.scala | 27 ++++++ .../mu/rpc/srcgen/SrcGenPlugin.scala | 70 ++++++++++++--- project/ProjectPlugin.scala | 4 + 9 files changed, 293 insertions(+), 13 deletions(-) create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumError.scala create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/ErrorResponse.scala create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala create mode 100644 core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala index 76ca8acb..9049c9d0 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala @@ -74,6 +74,12 @@ object Model { } } + sealed trait ExecutionMode extends Product with Serializable + object ExecutionMode { + case object Compendium extends ExecutionMode + case object MuScala extends ExecutionMode + } + sealed trait IdlType extends Product with Serializable object IdlType { diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala new file mode 100644 index 00000000..6ccb95d2 --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala @@ -0,0 +1,89 @@ +package higherkindness.mu.rpc.srcgen.compendium + +import cats.effect.Sync +import cats.free.Free +import hammock._ +import hammock.circe.implicits._ +import cats.implicits._ + +trait CompendiumClient[F[_]] { + + /** Stores a protocol + * + * @param identifier the protocol identifier + * @param protocol a protocol + * @return the identifier of the protocol + */ + def storeProtocol(identifier: String, protocol: RawProtocol): F[Int] + + /** Retrieve a Protocol by its id + * + * @param identifier the protocol identifier + * @param version optional protocol version number + * @return a protocol + */ + def retrieveProtocol(identifier: String, version: Option[Int]): F[Option[RawProtocol]] + +} + +object CompendiumClient { + + def apply[F[_]]()( + implicit interp: InterpTrans[F], + clientConfig: HttpConfig, + F: Sync[F] + ): CompendiumClient[F] = + new CompendiumClient[F] { + + val baseUrl: String = s"http://${clientConfig.host}:${clientConfig.port}" + val versionParamName: String = "version" + + override def storeProtocol(identifier: String, protocol: RawProtocol): F[Int] = { + val request: Free[HttpF, HttpResponse] = + Hammock.request(Method.POST, uri"$baseUrl/v0/protocol/$identifier", Map(), Some(protocol)) + + for { + status <- request.map(_.status).exec[F] + _ <- status match { + case Status.Created => Sync[F].unit + case Status.OK => Sync[F].unit + case Status.BadRequest => + asError(request, SchemaError) + case Status.InternalServerError => + Sync[F].raiseError(UnknownError(s"Error in compendium server")) + case _ => + Sync[F].raiseError(UnknownError(s"Unknown error with status code $status")) + } + } yield status.code + } + + override def retrieveProtocol( + identifier: String, + version: Option[Int] + ): F[Option[RawProtocol]] = { + val versionParam = version.fold("")(v => s"?$versionParamName=${v.show}") + val uri = uri"$baseUrl/v0/protocol/$identifier$versionParam" + + val request: Free[HttpF, HttpResponse] = Hammock.request(Method.GET, uri, Map()) + + for { + status <- request.map(_.status).exec[F] + out <- status match { + case Status.OK => request.as[RawProtocol].map(Option(_)).exec[F] + case Status.NotFound => Sync[F].pure(None) + case Status.InternalServerError => + Sync[F].raiseError(UnknownError(s"Error in compendium server")) + case _ => + Sync[F].raiseError(UnknownError(s"Unknown error with status code $status")) + } + } yield out + } + private def asError(request: Free[HttpF, HttpResponse], error: String => Exception): F[Unit] = + request + .as[ErrorResponse] + .exec[F] + .flatMap(rsp => Sync[F].raiseError(error(rsp.message))) + + } + +} diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumError.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumError.scala new file mode 100644 index 00000000..e73ae55a --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumError.scala @@ -0,0 +1,23 @@ +/* + * Copyright 2019-2020 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package higherkindness.mu.rpc.srcgen.compendium + +abstract class CompendiumError(error: String) extends Exception(error) + +final case class ProtocolNotFound(msg: String) extends CompendiumError(msg) +final case class SchemaError(msg: String) extends CompendiumError(msg) +final case class UnknownError(msg: String) extends CompendiumError(msg) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala new file mode 100644 index 00000000..8c079866 --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -0,0 +1,57 @@ +package higherkindness.mu.rpc.srcgen.compendium + +import java.io.{File, PrintWriter} +import cats.effect.{Async, Resource, Sync} +import scala.util.Try +import cats.implicits._ +import hammock.asynchttpclient.AsyncHttpClientInterpreter + +final case class ProtocolAndVersion(name: String, version: Option[String]) +final case class FilePrintWriter(file: File, pw: PrintWriter) + +case class CompendiumMode[F[_]: Async]( + protocols: List[ProtocolAndVersion], + fileType: String, + httpConfig: HttpConfig +) { + + implicit val interpreter = AsyncHttpClientInterpreter.instance[F] + implicit val clientConfig = httpConfig + + def run(): F[List[File]] = + protocols.traverse(protocolAndVersion => + for { + protocol <- CompendiumClient() + .retrieveProtocol( + protocolAndVersion.name, + safeInt(protocolAndVersion.version) + ) + file <- protocol match { + case Some(raw) => + writeTempFile(raw.raw, extension = fileType, identifier = protocolAndVersion.name) + case None => + Async[F].raiseError[File]( + ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") + ) + } + } yield file + ) + + private def safeInt(s: Option[String]): Option[Int] = s.flatMap(str => Try(str.toInt).toOption) + + private def writeTempFile( + msg: String, + extension: String, + identifier: String + ): F[File] = + Resource + .make(Sync[F].delay { + val tmpDir = System.getProperty("java.io.tmpdir") + val file = new File(tmpDir + s"/$identifier.$extension") + file.deleteOnExit() + FilePrintWriter(file, new PrintWriter(file)) + }) { fpw: FilePrintWriter => Sync[F].delay(fpw.pw.close()) } + .use((fpw: FilePrintWriter) => Sync[F].delay(fpw.pw.write(msg)).as(fpw)) + .map(_.file) + +} diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/ErrorResponse.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/ErrorResponse.scala new file mode 100644 index 00000000..79e83167 --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/ErrorResponse.scala @@ -0,0 +1,11 @@ +package higherkindness.mu.rpc.srcgen.compendium + +import io.circe.{Decoder, Encoder} +import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} + +final case class ErrorResponse(message: String) + +object ErrorResponse { + implicit val decoder: Decoder[ErrorResponse] = deriveDecoder[ErrorResponse] + implicit val encoder: Encoder[ErrorResponse] = deriveEncoder[ErrorResponse] +} diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala new file mode 100644 index 00000000..2c97afe9 --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala @@ -0,0 +1,19 @@ +/* + * Copyright 2019-2020 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package higherkindness.mu.rpc.srcgen.compendium + +final case class HttpConfig(host: String, port: Int) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala new file mode 100644 index 00000000..823b4149 --- /dev/null +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala @@ -0,0 +1,27 @@ +/* + * Copyright 2019-2020 47 Degrees, LLC. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package higherkindness.mu.rpc.srcgen.compendium + +import io.circe.generic.semiauto.{deriveDecoder, deriveEncoder} +import io.circe.{Decoder, Encoder} + +final case class RawProtocol(raw: String) + +object RawProtocol { + implicit val decoder: Decoder[RawProtocol] = deriveDecoder[RawProtocol] + implicit val encoder: Encoder[RawProtocol] = deriveEncoder[RawProtocol] +} diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index fa3171f7..7c1012b3 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -18,11 +18,13 @@ package higherkindness.mu.rpc.srcgen import java.io.File +import cats.effect.{IO => IOCats} +import higherkindness.mu.rpc.srcgen.Model.ExecutionMode._ import sbt.Keys._ -import sbt._ +import sbt.{Def, settingKey, _} import sbt.io.{Path, PathFinder} - import higherkindness.mu.rpc.srcgen.Model._ +import higherkindness.mu.rpc.srcgen.compendium.{CompendiumMode, HttpConfig, ProtocolAndVersion} import higherkindness.mu.rpc.srcgen.openapi.OpenApiSrcGenerator.HttpImpl object SrcGenPlugin extends AutoPlugin { @@ -105,6 +107,21 @@ object SrcGenPlugin extends AutoPlugin { "By default, the streaming implementation is FS2 Stream." ) + lazy val muSrcGenExecutionMode = settingKey[ExecutionMode]( + "Execution mode of the plugin. If Compendium, it's required a compendium instance where IDL files are saved." + ) + + lazy val muSrcGenCompendiumProtocolIdentifiers: SettingKey[Seq[ProtocolAndVersion]] = + settingKey[Seq[ProtocolAndVersion]]( + "Protocol identifiers (and version) to be retrieved from compendium server" + ) + + lazy val muSrcGenCompendiumServerHost: SettingKey[String] = + settingKey[String]("Host of the compendium server") + + lazy val muSrcGenCompendiumServerPort: SettingKey[Int] = + settingKey[Int]("Port of the compendium server") + } import autoImport._ @@ -141,7 +158,11 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenCompressionType := NoCompressionGen, muSrcGenIdiomaticEndpoints := false, muSrcGenOpenApiHttpImpl := HttpImpl.Http4sV20, - muSrcGenStreamingImplementation := Fs2Stream + muSrcGenStreamingImplementation := Fs2Stream, + muSrcGenExecutionMode := Compendium, //TODO change, use MuScala as default. Compendium for testing + muSrcGenCompendiumProtocolIdentifiers := Nil, + muSrcGenCompendiumServerHost := "localhost", + muSrcGenCompendiumServerPort := 47047 ) lazy val taskSettings: Seq[Def.Setting[_]] = { @@ -159,16 +180,39 @@ object SrcGenPlugin extends AutoPlugin { ) }, Def.task { - muSrcGenSourceDirs.value.toSet.foreach { f: File => - IO.copyDirectory( - f, - muSrcGenIdlTargetDir.value, - CopyOptions( - overwrite = true, - preserveLastModified = true, - preserveExecutable = true - ) - ) + muSrcGenExecutionMode.value match { + case Compendium => { + + CompendiumMode[IOCats]( + muSrcGenCompendiumProtocolIdentifiers.value.toList, + muSrcGenIdlExtension.value, + HttpConfig(muSrcGenCompendiumServerHost.value, muSrcGenCompendiumServerPort.value) + ).run() + .unsafeRunSync() + .foreach(f => + IO.copyDirectory( + f, + muSrcGenIdlTargetDir.value, + CopyOptions( + overwrite = true, + preserveLastModified = true, + preserveExecutable = true + ) + ) + ) + } + case MuScala => + muSrcGenSourceDirs.value.toSet.foreach { f: File => + IO.copyDirectory( + f, + muSrcGenIdlTargetDir.value, + CopyOptions( + overwrite = true, + preserveLastModified = true, + preserveExecutable = true + ) + ) + } } }, Def.task { diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 880b80f9..1dff1228 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -17,6 +17,7 @@ object ProjectPlugin extends AutoPlugin { lazy val V = new { val avrohugger: String = "1.0.0-RC22" val circe: String = "0.13.0" + val hammock = "0.10.0" val monocle: String = "2.0.4" val mu = "0.21.3" val scalacheck: String = "1.14.3" @@ -33,6 +34,9 @@ object ProjectPlugin extends AutoPlugin { "io.higherkindness" %% "skeuomorph" % V.skeuomorph, "com.julianpeeters" %% "avrohugger-core" % V.avrohugger, "io.circe" %% "circe-generic" % V.circe, + "com.pepegar" %% "hammock-core" % V.hammock, + "com.pepegar" %% "hammock-circe" % V.hammock, + "com.pepegar" %% "hammock-asynchttpclient" % V.hammock, "org.scalatest" %% "scalatest" % V.scalatest % Test, "org.scalacheck" %% "scalacheck" % V.scalacheck % Test, "org.scalatestplus" %% "scalatestplus-scalacheck" % V.scalatestplusScheck % Test, From 9b35fa9e4b4a777102148887d37ced167a7c7d90 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Wed, 20 May 2020 11:25:13 +0200 Subject: [PATCH 02/12] Scala fmtr --- project/ProjectPlugin.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index b0d8465c..d01f5ef5 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -37,8 +37,8 @@ object ProjectPlugin extends AutoPlugin { "com.pepegar" %% "hammock-core" % V.hammock, "com.pepegar" %% "hammock-circe" % V.hammock, "com.pepegar" %% "hammock-asynchttpclient" % V.hammock, - "org.scalatest" %% "scalatest" % V.scalatest % Test, - "org.scalacheck" %% "scalacheck" % V.scalacheck % Test, + "org.scalatest" %% "scalatest" % V.scalatest % Test, + "org.scalacheck" %% "scalacheck" % V.scalacheck % Test, "org.scalatestplus" %% "scalatestplus-scalacheck" % V.scalatestplusScheck % Test, "org.slf4j" % "slf4j-nop" % V.slf4j % Test ) From 360a5fd096493b1c57f901640514bcd4359e9b94 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Wed, 20 May 2020 11:28:41 +0200 Subject: [PATCH 03/12] scalafmtrAll --- .../mu/rpc/srcgen/compendium/CompendiumClient.scala | 4 ++-- .../scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 4 +--- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala index 6ccb95d2..e8250fb7 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala @@ -28,8 +28,8 @@ trait CompendiumClient[F[_]] { object CompendiumClient { - def apply[F[_]]()( - implicit interp: InterpTrans[F], + def apply[F[_]]()(implicit + interp: InterpTrans[F], clientConfig: HttpConfig, F: Sync[F] ): CompendiumClient[F] = diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 7c1012b3..045248b8 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -181,8 +181,7 @@ object SrcGenPlugin extends AutoPlugin { }, Def.task { muSrcGenExecutionMode.value match { - case Compendium => { - + case Compendium => CompendiumMode[IOCats]( muSrcGenCompendiumProtocolIdentifiers.value.toList, muSrcGenIdlExtension.value, @@ -200,7 +199,6 @@ object SrcGenPlugin extends AutoPlugin { ) ) ) - } case MuScala => muSrcGenSourceDirs.value.toSet.foreach { f: File => IO.copyDirectory( From e0063257ae6dbeaa8abd6809361567a6d90d7a30 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Wed, 20 May 2020 11:46:19 +0200 Subject: [PATCH 04/12] Test default mode --- .../main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 045248b8..71f7e795 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -159,7 +159,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenIdiomaticEndpoints := false, muSrcGenOpenApiHttpImpl := HttpImpl.Http4sV20, muSrcGenStreamingImplementation := Fs2Stream, - muSrcGenExecutionMode := Compendium, //TODO change, use MuScala as default. Compendium for testing + muSrcGenExecutionMode := MuScala, //TODO change, use MuScala as default. Compendium for testing muSrcGenCompendiumProtocolIdentifiers := Nil, muSrcGenCompendiumServerHost := "localhost", muSrcGenCompendiumServerPort := 47047 From dec76ce64daee1822331582d2596a1c8fa212309 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 21 May 2020 18:15:04 +0200 Subject: [PATCH 05/12] Create directory --- .../rpc/srcgen/compendium/CompendiumMode.scala | 17 ++++++++++++----- .../mu/rpc/srcgen/SrcGenPlugin.scala | 17 +++++------------ .../sbt-test/sbt-mu-srcgen/compendium/build.sbt | 5 +++++ .../compendium/project/plugins.sbt | 1 + .../src/sbt-test/sbt-mu-srcgen/compendium/test | 7 +++++++ 5 files changed, 30 insertions(+), 17 deletions(-) create mode 100644 plugin/src/sbt-test/sbt-mu-srcgen/compendium/build.sbt create mode 100644 plugin/src/sbt-test/sbt-mu-srcgen/compendium/project/plugins.sbt create mode 100644 plugin/src/sbt-test/sbt-mu-srcgen/compendium/test diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala index 8c079866..f191dccf 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -12,7 +12,8 @@ final case class FilePrintWriter(file: File, pw: PrintWriter) case class CompendiumMode[F[_]: Async]( protocols: List[ProtocolAndVersion], fileType: String, - httpConfig: HttpConfig + httpConfig: HttpConfig, + path: String ) { implicit val interpreter = AsyncHttpClientInterpreter.instance[F] @@ -28,7 +29,12 @@ case class CompendiumMode[F[_]: Async]( ) file <- protocol match { case Some(raw) => - writeTempFile(raw.raw, extension = fileType, identifier = protocolAndVersion.name) + writeTempFile( + raw.raw, + extension = fileType, + identifier = protocolAndVersion.name, + path = path + ) case None => Async[F].raiseError[File]( ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") @@ -42,12 +48,13 @@ case class CompendiumMode[F[_]: Async]( private def writeTempFile( msg: String, extension: String, - identifier: String + identifier: String, + path: String ): F[File] = Resource .make(Sync[F].delay { - val tmpDir = System.getProperty("java.io.tmpdir") - val file = new File(tmpDir + s"/$identifier.$extension") + if (!new File(path).exists()) new File(path).mkdirs() + val file = new File(path + s"/$identifier.$extension") file.deleteOnExit() FilePrintWriter(file, new PrintWriter(file)) }) { fpw: FilePrintWriter => Sync[F].delay(fpw.pw.close()) } diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 71f7e795..ab7f57d4 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -185,20 +185,13 @@ object SrcGenPlugin extends AutoPlugin { CompendiumMode[IOCats]( muSrcGenCompendiumProtocolIdentifiers.value.toList, muSrcGenIdlExtension.value, - HttpConfig(muSrcGenCompendiumServerHost.value, muSrcGenCompendiumServerPort.value) + HttpConfig( + muSrcGenCompendiumServerHost.value, + muSrcGenCompendiumServerPort.value + ), + muSrcGenIdlTargetDir.value.getAbsolutePath ).run() .unsafeRunSync() - .foreach(f => - IO.copyDirectory( - f, - muSrcGenIdlTargetDir.value, - CopyOptions( - overwrite = true, - preserveLastModified = true, - preserveExecutable = true - ) - ) - ) case MuScala => muSrcGenSourceDirs.value.toSet.foreach { f: File => IO.copyDirectory( diff --git a/plugin/src/sbt-test/sbt-mu-srcgen/compendium/build.sbt b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/build.sbt new file mode 100644 index 00000000..0be6b6d7 --- /dev/null +++ b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/build.sbt @@ -0,0 +1,5 @@ +version := sys.props("version") + +libraryDependencies ++= Seq( + "io.higherkindness" %% "mu-rpc-server" % sys.props("mu") +) diff --git a/plugin/src/sbt-test/sbt-mu-srcgen/compendium/project/plugins.sbt b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/project/plugins.sbt new file mode 100644 index 00000000..2e452245 --- /dev/null +++ b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("io.higherkindness" %% "sbt-mu-srcgen" % sys.props("version")) diff --git a/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test new file mode 100644 index 00000000..ff57a797 --- /dev/null +++ b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test @@ -0,0 +1,7 @@ +> 'set muSrcGenExecutionMode := higherkindness.mu.rpc.srcgen.Model.ExecutionMode.Compendium' +> 'set muSrcGenIdlType := higherkindness.mu.rpc.srcgen.Model.IdlType.Proto' +> 'set muSrcGenCompendiumProtocolIdentifiers := Seq(higherkindness.mu.rpc.srcgen.compendium.ProtocolAndVersion.apply("shop",None))' +> 'set muSrcGenCompendiumServerPort := 8080' +# > compile +# $ exists target/scala-2.12/resource_managed/main/proto/shop.proto + From c034cee34c01017996340051eafb3c643716fbe4 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 21 May 2020 18:23:41 +0200 Subject: [PATCH 06/12] Change execution mode name --- core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala | 2 +- .../scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala index fdd30505..fd0cd00c 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala @@ -79,7 +79,7 @@ object Model { sealed trait ExecutionMode extends Product with Serializable object ExecutionMode { case object Compendium extends ExecutionMode - case object MuScala extends ExecutionMode + case object Local extends ExecutionMode } sealed trait IdlType extends Product with Serializable diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index ab7f57d4..772d37dc 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -159,7 +159,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenIdiomaticEndpoints := false, muSrcGenOpenApiHttpImpl := HttpImpl.Http4sV20, muSrcGenStreamingImplementation := Fs2Stream, - muSrcGenExecutionMode := MuScala, //TODO change, use MuScala as default. Compendium for testing + muSrcGenExecutionMode := Local, muSrcGenCompendiumProtocolIdentifiers := Nil, muSrcGenCompendiumServerHost := "localhost", muSrcGenCompendiumServerPort := 47047 @@ -192,7 +192,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenIdlTargetDir.value.getAbsolutePath ).run() .unsafeRunSync() - case MuScala => + case Local => muSrcGenSourceDirs.value.toSet.foreach { f: File => IO.copyDirectory( f, From b76e3bb5fe17eb5c148245eb1a3b4d472ac94263 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Tue, 26 May 2020 09:59:52 +0200 Subject: [PATCH 07/12] Use http4s instead of hammock --- .../srcgen/compendium/CompendiumClient.scala | 62 +++++-------------- .../srcgen/compendium/CompendiumMode.scala | 54 +++++++++------- .../rpc/srcgen/compendium/RawProtocol.scala | 1 + .../mu/rpc/srcgen/SrcGenPlugin.scala | 13 ++-- project/ProjectPlugin.scala | 6 +- 5 files changed, 56 insertions(+), 80 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala index e8250fb7..7f1732bd 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala @@ -1,21 +1,13 @@ package higherkindness.mu.rpc.srcgen.compendium import cats.effect.Sync -import cats.free.Free -import hammock._ -import hammock.circe.implicits._ +import org.http4s._ +import org.http4s.circe._ import cats.implicits._ +import org.http4s.client.Client trait CompendiumClient[F[_]] { - /** Stores a protocol - * - * @param identifier the protocol identifier - * @param protocol a protocol - * @return the identifier of the protocol - */ - def storeProtocol(identifier: String, protocol: RawProtocol): F[Int] - /** Retrieve a Protocol by its id * * @param identifier the protocol identifier @@ -28,62 +20,36 @@ trait CompendiumClient[F[_]] { object CompendiumClient { - def apply[F[_]]()(implicit - interp: InterpTrans[F], - clientConfig: HttpConfig, - F: Sync[F] + def apply[F[_]: Sync]( + clientF: Client[F], + clientConfig: HttpConfig ): CompendiumClient[F] = new CompendiumClient[F] { val baseUrl: String = s"http://${clientConfig.host}:${clientConfig.port}" val versionParamName: String = "version" - override def storeProtocol(identifier: String, protocol: RawProtocol): F[Int] = { - val request: Free[HttpF, HttpResponse] = - Hammock.request(Method.POST, uri"$baseUrl/v0/protocol/$identifier", Map(), Some(protocol)) - - for { - status <- request.map(_.status).exec[F] - _ <- status match { - case Status.Created => Sync[F].unit - case Status.OK => Sync[F].unit - case Status.BadRequest => - asError(request, SchemaError) - case Status.InternalServerError => - Sync[F].raiseError(UnknownError(s"Error in compendium server")) - case _ => - Sync[F].raiseError(UnknownError(s"Unknown error with status code $status")) - } - } yield status.code - } - override def retrieveProtocol( identifier: String, version: Option[Int] ): F[Option[RawProtocol]] = { val versionParam = version.fold("")(v => s"?$versionParamName=${v.show}") - val uri = uri"$baseUrl/v0/protocol/$identifier$versionParam" + val uri = s"$baseUrl/v0/protocol/$identifier$versionParam" - val request: Free[HttpF, HttpResponse] = Hammock.request(Method.GET, uri, Map()) + implicit val rawEntityDecoder = jsonOf[F, RawProtocol] - for { - status <- request.map(_.status).exec[F] - out <- status match { - case Status.OK => request.as[RawProtocol].map(Option(_)).exec[F] + clientF.get(uri)(res => + res.status match { + case Status.Ok => res.as[RawProtocol].map(Option(_)) case Status.NotFound => Sync[F].pure(None) case Status.InternalServerError => Sync[F].raiseError(UnknownError(s"Error in compendium server")) case _ => - Sync[F].raiseError(UnknownError(s"Unknown error with status code $status")) + Sync[F].raiseError(UnknownError(s"Unknown error with status code ${res.status.code}")) + } - } yield out + ) } - private def asError(request: Free[HttpF, HttpResponse], error: String => Exception): F[Unit] = - request - .as[ErrorResponse] - .exec[F] - .flatMap(rsp => Sync[F].raiseError(error(rsp.message))) - } } diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala index f191dccf..7287c4cb 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -1,46 +1,52 @@ package higherkindness.mu.rpc.srcgen.compendium import java.io.{File, PrintWriter} -import cats.effect.{Async, Resource, Sync} + +import cats.effect.{Async, ConcurrentEffect, Resource, Sync} + import scala.util.Try import cats.implicits._ -import hammock.asynchttpclient.AsyncHttpClientInterpreter +import org.http4s.client.blaze._ + +import scala.concurrent.ExecutionContext.global final case class ProtocolAndVersion(name: String, version: Option[String]) final case class FilePrintWriter(file: File, pw: PrintWriter) -case class CompendiumMode[F[_]: Async]( +case class CompendiumMode[F[_]: Async: ConcurrentEffect]( protocols: List[ProtocolAndVersion], fileType: String, httpConfig: HttpConfig, path: String ) { - implicit val interpreter = AsyncHttpClientInterpreter.instance[F] - implicit val clientConfig = httpConfig + val httpClient = BlazeClientBuilder[F](global).resource def run(): F[List[File]] = protocols.traverse(protocolAndVersion => - for { - protocol <- CompendiumClient() - .retrieveProtocol( - protocolAndVersion.name, - safeInt(protocolAndVersion.version) - ) - file <- protocol match { - case Some(raw) => - writeTempFile( - raw.raw, - extension = fileType, - identifier = protocolAndVersion.name, - path = path - ) - case None => - Async[F].raiseError[File]( - ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") + httpClient.use(client => { + for { + + protocol <- CompendiumClient(client, httpConfig) + .retrieveProtocol( + protocolAndVersion.name, + safeInt(protocolAndVersion.version) ) - } - } yield file + file <- protocol match { + case Some(raw) => + writeTempFile( + raw.raw, + extension = fileType, + identifier = protocolAndVersion.name, + path = path + ) + case None => + Async[F].raiseError[File]( + ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") + ) + } + } yield file + }) ) private def safeInt(s: Option[String]): Option[Int] = s.flatMap(str => Try(str.toInt).toOption) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala index 823b4149..f6efdbc3 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/RawProtocol.scala @@ -24,4 +24,5 @@ final case class RawProtocol(raw: String) object RawProtocol { implicit val decoder: Decoder[RawProtocol] = deriveDecoder[RawProtocol] implicit val encoder: Encoder[RawProtocol] = deriveEncoder[RawProtocol] + } diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 772d37dc..8bff7ad9 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -18,7 +18,7 @@ package higherkindness.mu.rpc.srcgen import java.io.File -import cats.effect.{IO => IOCats} +import cats.effect.{ContextShift, IO => IOCats} import higherkindness.mu.rpc.srcgen.Model.ExecutionMode._ import sbt.Keys._ import sbt.{Def, settingKey, _} @@ -27,6 +27,8 @@ import higherkindness.mu.rpc.srcgen.Model._ import higherkindness.mu.rpc.srcgen.compendium.{CompendiumMode, HttpConfig, ProtocolAndVersion} import higherkindness.mu.rpc.srcgen.openapi.OpenApiSrcGenerator.HttpImpl +import scala.concurrent.ExecutionContext.global + object SrcGenPlugin extends AutoPlugin { override def trigger: PluginTrigger = allRequirements @@ -108,19 +110,19 @@ object SrcGenPlugin extends AutoPlugin { ) lazy val muSrcGenExecutionMode = settingKey[ExecutionMode]( - "Execution mode of the plugin. If Compendium, it's required a compendium instance where IDL files are saved." + "Execution mode of the plugin. If Compendium, it's required a compendium instance where IDL files are saved. `Local` by default." ) lazy val muSrcGenCompendiumProtocolIdentifiers: SettingKey[Seq[ProtocolAndVersion]] = settingKey[Seq[ProtocolAndVersion]]( - "Protocol identifiers (and version) to be retrieved from compendium server" + "Protocol identifiers (and version) to be retrieved from compendium server. By default is an empty list." ) lazy val muSrcGenCompendiumServerHost: SettingKey[String] = - settingKey[String]("Host of the compendium server") + settingKey[String]("Host of the compendium server. By default, `localhost`.") lazy val muSrcGenCompendiumServerPort: SettingKey[Int] = - settingKey[Int]("Port of the compendium server") + settingKey[Int]("Port of the compendium server. `47047` by default.") } @@ -182,6 +184,7 @@ object SrcGenPlugin extends AutoPlugin { Def.task { muSrcGenExecutionMode.value match { case Compendium => + implicit val cs: ContextShift[IOCats] = IOCats.contextShift(global) CompendiumMode[IOCats]( muSrcGenCompendiumProtocolIdentifiers.value.toList, muSrcGenIdlExtension.value, diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 611e32c3..5803fc3d 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -25,6 +25,7 @@ object ProjectPlugin extends AutoPlugin { val scalatestplusScheck: String = "3.1.0.0-RC2" val skeuomorph: String = "0.0.23" val slf4j: String = "1.7.30" + val http4s: String = "0.21.1" } lazy val srcGenSettings: Seq[Def.Setting[_]] = Seq( @@ -34,9 +35,8 @@ object ProjectPlugin extends AutoPlugin { "io.higherkindness" %% "skeuomorph" % V.skeuomorph, "com.julianpeeters" %% "avrohugger-core" % V.avrohugger, "io.circe" %% "circe-generic" % V.circe, - "com.pepegar" %% "hammock-core" % V.hammock, - "com.pepegar" %% "hammock-circe" % V.hammock, - "com.pepegar" %% "hammock-asynchttpclient" % V.hammock, + "org.http4s" %% "http4s-blaze-client" % V.http4s, + "org.http4s" %% "http4s-circe" % V.http4s, "org.scalatest" %% "scalatest" % V.scalatest % Test, "org.scalacheck" %% "scalacheck" % V.scalacheck % Test, "org.scalatestplus" %% "scalatestplus-scalacheck" % V.scalatestplusScheck % Test, From ad1800792c70b4827ac896af2b643fa027acbb84 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Wed, 27 May 2020 12:42:51 +0200 Subject: [PATCH 08/12] Add suggestions --- .../mu/rpc/srcgen/compendium/CompendiumMode.scala | 12 ++++++------ project/ProjectPlugin.scala | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala index 7287c4cb..2424a4c0 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -2,7 +2,7 @@ package higherkindness.mu.rpc.srcgen.compendium import java.io.{File, PrintWriter} -import cats.effect.{Async, ConcurrentEffect, Resource, Sync} +import cats.effect.{ConcurrentEffect, Resource} import scala.util.Try import cats.implicits._ @@ -13,7 +13,7 @@ import scala.concurrent.ExecutionContext.global final case class ProtocolAndVersion(name: String, version: Option[String]) final case class FilePrintWriter(file: File, pw: PrintWriter) -case class CompendiumMode[F[_]: Async: ConcurrentEffect]( +final case class CompendiumMode[F[_]: ConcurrentEffect]( protocols: List[ProtocolAndVersion], fileType: String, httpConfig: HttpConfig, @@ -41,7 +41,7 @@ case class CompendiumMode[F[_]: Async: ConcurrentEffect]( path = path ) case None => - Async[F].raiseError[File]( + ConcurrentEffect[F].raiseError[File]( ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") ) } @@ -58,13 +58,13 @@ case class CompendiumMode[F[_]: Async: ConcurrentEffect]( path: String ): F[File] = Resource - .make(Sync[F].delay { + .make(ConcurrentEffect[F].delay { if (!new File(path).exists()) new File(path).mkdirs() val file = new File(path + s"/$identifier.$extension") file.deleteOnExit() FilePrintWriter(file, new PrintWriter(file)) - }) { fpw: FilePrintWriter => Sync[F].delay(fpw.pw.close()) } - .use((fpw: FilePrintWriter) => Sync[F].delay(fpw.pw.write(msg)).as(fpw)) + }) { fpw: FilePrintWriter => ConcurrentEffect[F].delay(fpw.pw.close()) } + .use((fpw: FilePrintWriter) => ConcurrentEffect[F].delay(fpw.pw.write(msg)).as(fpw)) .map(_.file) } diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 5803fc3d..10af32ae 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -25,7 +25,7 @@ object ProjectPlugin extends AutoPlugin { val scalatestplusScheck: String = "3.1.0.0-RC2" val skeuomorph: String = "0.0.23" val slf4j: String = "1.7.30" - val http4s: String = "0.21.1" + val http4s: String = "0.21.4" } lazy val srcGenSettings: Seq[Def.Setting[_]] = Seq( From 1ee90f8f719d7626030e9da64a2b67a8341c863c Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 28 May 2020 09:47:02 +0200 Subject: [PATCH 09/12] Use a single connection url for compendium --- .../mu/rpc/srcgen/compendium/CompendiumClient.scala | 9 +++------ .../mu/rpc/srcgen/compendium/CompendiumMode.scala | 1 - .../mu/rpc/srcgen/compendium/HttpConfig.scala | 2 +- .../higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 13 ++++--------- plugin/src/sbt-test/sbt-mu-srcgen/compendium/test | 2 +- 5 files changed, 9 insertions(+), 18 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala index 7f1732bd..51c9f4b7 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala @@ -26,19 +26,16 @@ object CompendiumClient { ): CompendiumClient[F] = new CompendiumClient[F] { - val baseUrl: String = s"http://${clientConfig.host}:${clientConfig.port}" - val versionParamName: String = "version" - override def retrieveProtocol( identifier: String, version: Option[Int] ): F[Option[RawProtocol]] = { - val versionParam = version.fold("")(v => s"?$versionParamName=${v.show}") - val uri = s"$baseUrl/v0/protocol/$identifier$versionParam" + val versionParam = version.fold("")(v => s"?version=${v.show}") + val connectionUrl = s"${clientConfig.serverUrl}/v0/protocol/$identifier$versionParam" implicit val rawEntityDecoder = jsonOf[F, RawProtocol] - clientF.get(uri)(res => + clientF.get(connectionUrl)(res => res.status match { case Status.Ok => res.as[RawProtocol].map(Option(_)) case Status.NotFound => Sync[F].pure(None) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala index 2424a4c0..4598771b 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -26,7 +26,6 @@ final case class CompendiumMode[F[_]: ConcurrentEffect]( protocols.traverse(protocolAndVersion => httpClient.use(client => { for { - protocol <- CompendiumClient(client, httpConfig) .retrieveProtocol( protocolAndVersion.name, diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala index 2c97afe9..81e804ed 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/HttpConfig.scala @@ -16,4 +16,4 @@ package higherkindness.mu.rpc.srcgen.compendium -final case class HttpConfig(host: String, port: Int) +final case class HttpConfig(serverUrl: String) diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 8bff7ad9..72289742 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -118,11 +118,8 @@ object SrcGenPlugin extends AutoPlugin { "Protocol identifiers (and version) to be retrieved from compendium server. By default is an empty list." ) - lazy val muSrcGenCompendiumServerHost: SettingKey[String] = - settingKey[String]("Host of the compendium server. By default, `localhost`.") - - lazy val muSrcGenCompendiumServerPort: SettingKey[Int] = - settingKey[Int]("Port of the compendium server. `47047` by default.") + lazy val muSrcGenCompendiumServerUrl: SettingKey[String] = + settingKey[String]("Url of the compendium server. By default, `http://localhost:47047`.") } @@ -163,8 +160,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenStreamingImplementation := Fs2Stream, muSrcGenExecutionMode := Local, muSrcGenCompendiumProtocolIdentifiers := Nil, - muSrcGenCompendiumServerHost := "localhost", - muSrcGenCompendiumServerPort := 47047 + muSrcGenCompendiumServerUrl := "https://localhost:47047" ) lazy val taskSettings: Seq[Def.Setting[_]] = { @@ -189,8 +185,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenCompendiumProtocolIdentifiers.value.toList, muSrcGenIdlExtension.value, HttpConfig( - muSrcGenCompendiumServerHost.value, - muSrcGenCompendiumServerPort.value + muSrcGenCompendiumServerUrl.value ), muSrcGenIdlTargetDir.value.getAbsolutePath ).run() diff --git a/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test index ff57a797..ecdf572c 100644 --- a/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test +++ b/plugin/src/sbt-test/sbt-mu-srcgen/compendium/test @@ -1,7 +1,7 @@ > 'set muSrcGenExecutionMode := higherkindness.mu.rpc.srcgen.Model.ExecutionMode.Compendium' > 'set muSrcGenIdlType := higherkindness.mu.rpc.srcgen.Model.IdlType.Proto' > 'set muSrcGenCompendiumProtocolIdentifiers := Seq(higherkindness.mu.rpc.srcgen.compendium.ProtocolAndVersion.apply("shop",None))' -> 'set muSrcGenCompendiumServerPort := 8080' +> 'set muSrcGenCompendiumServerUrl := "http://localhost:8080"' # > compile # $ exists target/scala-2.12/resource_managed/main/proto/shop.proto From 8a5fa4e6d0eb96bb0122edeb4cc28d335980338b Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 28 May 2020 10:52:56 +0200 Subject: [PATCH 10/12] Update default value, drop unused library version. --- .../main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 2 +- project/ProjectPlugin.scala | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index 72289742..c710428b 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -119,7 +119,7 @@ object SrcGenPlugin extends AutoPlugin { ) lazy val muSrcGenCompendiumServerUrl: SettingKey[String] = - settingKey[String]("Url of the compendium server. By default, `http://localhost:47047`.") + settingKey[String]("Url of the compendium server. By default, `https://localhost:47047`.") } diff --git a/project/ProjectPlugin.scala b/project/ProjectPlugin.scala index 10af32ae..ab3c1620 100644 --- a/project/ProjectPlugin.scala +++ b/project/ProjectPlugin.scala @@ -17,7 +17,6 @@ object ProjectPlugin extends AutoPlugin { lazy val V = new { val avrohugger: String = "1.0.0-RC22" val circe: String = "0.13.0" - val hammock = "0.10.0" val monocle: String = "2.0.4" val mu = "0.22.1" val scalacheck: String = "1.14.3" From 9b2aa34c6f3c8f114aa2f6c6df9ff34b5781a9b0 Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 28 May 2020 13:00:47 +0200 Subject: [PATCH 11/12] Revert https changes. --- .../scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala index c710428b..d1213ad9 100644 --- a/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala +++ b/plugin/src/main/scala/higherkindness/mu/rpc/srcgen/SrcGenPlugin.scala @@ -119,7 +119,7 @@ object SrcGenPlugin extends AutoPlugin { ) lazy val muSrcGenCompendiumServerUrl: SettingKey[String] = - settingKey[String]("Url of the compendium server. By default, `https://localhost:47047`.") + settingKey[String]("Url of the compendium server. By default, `http://localhost:47047`.") } @@ -160,7 +160,7 @@ object SrcGenPlugin extends AutoPlugin { muSrcGenStreamingImplementation := Fs2Stream, muSrcGenExecutionMode := Local, muSrcGenCompendiumProtocolIdentifiers := Nil, - muSrcGenCompendiumServerUrl := "https://localhost:47047" + muSrcGenCompendiumServerUrl := "http://localhost:47047" ) lazy val taskSettings: Seq[Def.Setting[_]] = { From 41cf29e646623b0974989f7971c9b74eabb8fb8e Mon Sep 17 00:00:00 2001 From: mrtmmr Date: Thu, 28 May 2020 17:34:06 +0200 Subject: [PATCH 12/12] Change raising error --- .../mu/rpc/srcgen/compendium/CompendiumClient.scala | 8 ++------ .../mu/rpc/srcgen/compendium/CompendiumMode.scala | 5 ++--- 2 files changed, 4 insertions(+), 9 deletions(-) diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala index 51c9f4b7..11c1bcef 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumClient.scala @@ -4,7 +4,7 @@ import cats.effect.Sync import org.http4s._ import org.http4s.circe._ import cats.implicits._ -import org.http4s.client.Client +import org.http4s.client.{Client, UnexpectedStatus} trait CompendiumClient[F[_]] { @@ -39,11 +39,7 @@ object CompendiumClient { res.status match { case Status.Ok => res.as[RawProtocol].map(Option(_)) case Status.NotFound => Sync[F].pure(None) - case Status.InternalServerError => - Sync[F].raiseError(UnknownError(s"Error in compendium server")) - case _ => - Sync[F].raiseError(UnknownError(s"Unknown error with status code ${res.status.code}")) - + case s => Sync[F].raiseError(UnexpectedStatus(s)) } ) } diff --git a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala index 4598771b..f61cec67 100644 --- a/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala +++ b/core/src/main/scala/higherkindness/mu/rpc/srcgen/compendium/CompendiumMode.scala @@ -40,9 +40,8 @@ final case class CompendiumMode[F[_]: ConcurrentEffect]( path = path ) case None => - ConcurrentEffect[F].raiseError[File]( - ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") - ) + ProtocolNotFound(s"Protocol ${protocolAndVersion.name} not found in Compendium. ") + .raiseError[F, File] } } yield file })