Skip to content

Commit

Permalink
Make useIdiomaticEndpoints default to true
Browse files Browse the repository at this point in the history
Depends on higherkindness/skeuomorph#342.

Also cleans up the Model by removing unused classes, and updates the tests and documentation.
  • Loading branch information
L-Lavigne committed Oct 9, 2020
1 parent 7929aa3 commit cb7a167
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 199 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ lazy val core = project
libraryDependencies ++= Seq(
"io.higherkindness" %% "mu-rpc-service" % muV,
"com.github.julien-truffaut" %% "monocle-core" % "2.1.0",
"io.higherkindness" %% "skeuomorph" % "0.0.25",
"io.higherkindness" %% "skeuomorph" % "0.0.26",
"com.julianpeeters" %% "avrohugger-core" % "1.0.0-RC22",
"io.circe" %% "circe-generic" % "0.13.0",
"org.http4s" %% "http4s-blaze-client" % "0.21.7",
Expand Down
76 changes: 3 additions & 73 deletions core/src/main/scala/higherkindness/mu/rpc/srcgen/Model.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,74 +16,15 @@

package higherkindness.mu.rpc.srcgen

import higherkindness.mu.rpc.protocol.{SerializationType => SerType, _}
import shapeless.tag
import shapeless.tag.@@

object Model {

import Toolbox.u._

implicit private class StringOps(private val s: String) extends AnyVal {
def trimAll: String = s.replaceAll("\\s", "")
}

final case class RpcDefinitions(
outputName: String,
outputPackage: Option[String],
options: Seq[RpcOption],
messages: Seq[RpcMessage],
services: Seq[RpcService]
)

final case class RpcOption(name: String, value: String)

final case class RpcMessage(name: String, params: Seq[ValDef]) {

// Workaround for `Term.Param` using referential equality; needed mostly for unit testing
override def equals(other: Any): Boolean =
other match {
case that: RpcMessage =>
this.name == that.name && this.params.map(_.toString.trimAll) == that.params.map(
_.toString.trimAll
)
case _ => false
}
}

final case class RpcService(
serializationType: SerType,
name: String,
requests: Seq[RpcRequest]
)

final case class RpcRequest(
name: String,
requestType: Tree,
responseType: Tree,
streamingType: Option[StreamingType] = None
) {

// Workaround for `Type` using referential equality; needed mostly for unit testing
override def equals(other: Any): Boolean =
other match {
case that: RpcRequest =>
this.name == that.name &&
this.requestType.toString.trimAll == that.requestType.toString.trimAll &&
this.responseType.toString.trimAll == that.responseType.toString.trimAll &&
this.streamingType == that.streamingType
case _ => false
}
}

sealed trait ExecutionMode extends Product with Serializable
object ExecutionMode {
case object Compendium extends ExecutionMode
case object Local extends ExecutionMode
}

sealed trait IdlType extends Product with Serializable

object IdlType {
case object Proto extends IdlType
case object Avro extends IdlType
Expand All @@ -92,7 +33,6 @@ object Model {
}

sealed trait SerializationType extends Product with Serializable

object SerializationType {
case object Protobuf extends SerializationType
case object Avro extends SerializationType
Expand Down Expand Up @@ -131,19 +71,9 @@ object Model {
case object ScalaBigDecimalGen extends BigDecimalTypeGen
case object ScalaBigDecimalTaggedGen extends BigDecimalTypeGen

sealed abstract class CompressionTypeGen(val value: String) extends Product with Serializable
case object GzipGen extends CompressionTypeGen("Gzip")
case object NoCompressionGen extends CompressionTypeGen("Identity")

trait UseIdiomaticEndpointsTag
type UseIdiomaticEndpoints = Boolean @@ UseIdiomaticEndpointsTag

object UseIdiomaticEndpoints {
val trueV = UseIdiomaticEndpoints(true)

def apply(v: Boolean): UseIdiomaticEndpoints =
tag[UseIdiomaticEndpointsTag][Boolean](v)
}
sealed abstract class CompressionTypeGen extends Product with Serializable
case object GzipGen extends CompressionTypeGen
case object NoCompressionGen extends CompressionTypeGen

sealed trait StreamingImplementation
case object Fs2Stream extends StreamingImplementation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,46 @@ package higherkindness.mu.rpc.srcgen

import java.io.File
import java.nio.file.Path

import higherkindness.mu.rpc.srcgen.Model.{
BigDecimalTypeGen,
CompressionTypeGen,
MarshallersImport,
StreamingImplementation,
UseIdiomaticEndpoints
}
import higherkindness.mu.rpc.srcgen.Model._
import higherkindness.mu.rpc.srcgen.avro.AvroSrcGenerator
import higherkindness.mu.rpc.srcgen.openapi.OpenApiSrcGenerator
import higherkindness.mu.rpc.srcgen.openapi.OpenApiSrcGenerator.HttpImpl
import higherkindness.mu.rpc.srcgen.proto.ProtoSrcGenerator
import higherkindness.skeuomorph.mu.CompressionType

object SrcGenApplication {

def apply(
marshallersImports: List[MarshallersImport],
bigDecimalTypeGen: BigDecimalTypeGen,
compressionType: CompressionTypeGen,
useIdiomaticEndpoints: UseIdiomaticEndpoints,
compressionTypeGen: CompressionTypeGen,
useIdiomaticEndpoints: Boolean,
streamingImplementation: StreamingImplementation,
idlTargetDir: File,
resourcesBasePath: Path,
httpImpl: HttpImpl
): GeneratorApplication[SrcGenerator] =
): GeneratorApplication[SrcGenerator] = {
val compressionType: CompressionType = compressionTypeGen match {
case GzipGen => CompressionType.Gzip
case NoCompressionGen => CompressionType.Identity
}
new GeneratorApplication(
ProtoSrcGenerator
.build(compressionType, useIdiomaticEndpoints, streamingImplementation, idlTargetDir),
ProtoSrcGenerator(
streamingImplementation,
idlTargetDir,
compressionType,
useIdiomaticEndpoints
),
AvroSrcGenerator(
marshallersImports,
bigDecimalTypeGen,
compressionType,
useIdiomaticEndpoints
),
OpenApiSrcGenerator(httpImpl, resourcesBasePath)
OpenApiSrcGenerator(
httpImpl,
resourcesBasePath
)
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@

package higherkindness.mu.rpc.srcgen.avro

import java.io.File

import scala.collection.JavaConverters._
import scala.util.Right
import avrohugger.Generator
import avrohugger.format.Standard
import avrohugger.types._
import cats.implicits._
import higherkindness.mu.rpc.srcgen.Model._
import higherkindness.mu.rpc.srcgen._
import higherkindness.skeuomorph.mu.CompressionType
import java.io.File
import org.apache.avro._
import cats.implicits._
import scala.collection.JavaConverters._
import scala.util.Right

final case class AvroSrcGenerator(
marshallersImports: List[MarshallersImport],
bigDecimalTypeGen: BigDecimalTypeGen,
compressionTypeGen: CompressionTypeGen,
useIdiomaticEndpoints: UseIdiomaticEndpoints
compressionType: CompressionType = CompressionType.Identity,
useIdiomaticEndpoints: Boolean = true
) extends SrcGenerator {

private val avroBigDecimal: AvroScalaDecimalType = bigDecimalTypeGen match {
Expand Down Expand Up @@ -103,6 +103,7 @@ final case class AvroSrcGenerator(
})
.map(generateFrom(_, serializationType))

//TODO: implement this using higherkindness.skeuomorph.mu.Protocol.fromAvroProtocol, as is done in ProtoSrcGenerator
def generateFrom(
protocol: Protocol,
serializationType: SerializationType
Expand Down Expand Up @@ -130,16 +131,12 @@ final case class AvroSrcGenerator(

val messageLines = (schemaLines.tail :+ "").toList

val extraParams =
s"compressionType = ${compressionTypeGen.value}" +:
(if (useIdiomaticEndpoints) {
List(
s"""namespace = Some("${protocol.getNamespace}")""",
"methodNameStyle = Capitalize"
)
} else Nil)

val serviceParams = (serializationType.toString +: extraParams).mkString(",")
val serviceParams = Seq(
serializationType.toString,
s"compressionType = $compressionType",
if (useIdiomaticEndpoints) s"""namespace = Some("${protocol.getNamespace}")"""
else "namespace = None"
).mkString(", ")

val requestLines = protocol.getMessages.asScala.toList.flatTraverse { case (name, message) =>
val comment = Option(message.getDoc).map(doc => s" /** $doc */").toList
Expand All @@ -153,7 +150,7 @@ final case class AvroSrcGenerator(
if (requests.isEmpty) List.empty
else {
List(
s"@service(${serviceParams}) trait ${protocol.getName}[F[_]] {",
s"@service($serviceParams) trait ${protocol.getName}[F[_]] {",
""
) ++ requests :+ "}"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,17 @@
package higherkindness.mu.rpc.srcgen.proto

import java.io.File

import scala.meta._
import scala.util.control.NoStackTrace

import cats.effect.{IO, Sync}
import cats.syntax.flatMap._
import cats.data.Validated._

import higherkindness.droste.data.Mu
import higherkindness.droste.data.Mu._
import higherkindness.mu.rpc.srcgen.Model._
import higherkindness.mu.rpc.srcgen.Model.{
CompressionTypeGen,
GzipGen,
NoCompressionGen,
StreamingImplementation,
UseIdiomaticEndpoints
}
import higherkindness.mu.rpc.srcgen._
import higherkindness.skeuomorph.mu.{CompressionType, MuF}
import higherkindness.skeuomorph.{mu => skeuomorph}
import higherkindness.skeuomorph.protobuf.ParseProto.{parseProto, ProtoSource}
import higherkindness.skeuomorph.protobuf.{ProtobufF, Protocol}

Expand All @@ -46,11 +37,11 @@ object ProtoSrcGenerator {
override def toString: String = s"ProtoBufSrcGenException: $message"
}

def build(
compressionTypeGen: CompressionTypeGen,
useIdiomaticEndpoints: UseIdiomaticEndpoints,
def apply(
streamingImplementation: StreamingImplementation,
idlTargetDir: File
idlTargetDir: File = new File("."),
compressionType: CompressionType = CompressionType.Identity,
useIdiomaticEndpoints: Boolean = true
): SrcGenerator =
new SrcGenerator {

Expand All @@ -70,24 +61,11 @@ object ProtoSrcGenerator {
case MonixObservable => { case (_, a) => t"_root_.monix.reactive.Observable[$a]" }
}

val skeuomorphCompression: CompressionType = compressionTypeGen match {
case GzipGen => CompressionType.Gzip
case NoCompressionGen => CompressionType.Identity
}

val transformToMuProtocol: Protocol[Mu[ProtobufF]] => higherkindness.skeuomorph.mu.Protocol[
Mu[
MuF
]
] =
higherkindness.skeuomorph.mu.Protocol
.fromProtobufProto(skeuomorphCompression, useIdiomaticEndpoints)
val transformToMuProtocol: Protocol[Mu[ProtobufF]] => skeuomorph.Protocol[Mu[MuF]] =
skeuomorph.Protocol.fromProtobufProto(compressionType, useIdiomaticEndpoints)

val generateScalaSource: higherkindness.skeuomorph.mu.Protocol[Mu[MuF]] => Either[
String,
String
] =
higherkindness.skeuomorph.mu.codegen.protocol(_, streamCtor).map(_.syntax)
val generateScalaSource: skeuomorph.Protocol[Mu[MuF]] => Either[String, String] =
skeuomorph.codegen.protocol(_, streamCtor).map(_.syntax)

val splitLines: String => List[String] = _.split("\n").toList

Expand Down
Loading

0 comments on commit cb7a167

Please sign in to comment.