Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove @message annotation from generated case classes #219

Merged
merged 1 commit into from
Jan 27, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 3 additions & 9 deletions src/main/scala/higherkindness/skeuomorph/FileUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,21 +25,15 @@ object FileUtils {
def fileHandle[F[_]: Sync](name: String): Resource[F, File] =
Resource.make(
Sync[F].delay(new File(name))
)(
file => Sync[F].delay(file.deleteOnExit())
)
)(file => Sync[F].delay(file.deleteOnExit()))

def fileOutputStream[F[_]: Sync](file: File): Resource[F, FileOutputStream] =
Resource.make(
Sync[F].delay(new FileOutputStream(file))
)(
fos => Sync[F].delay(fos.close())
)
)(fos => Sync[F].delay(fos.close()))

def fileInputStream[F[_]: Sync](name: String): Resource[F, InputStream] =
Resource.make(
Sync[F].delay(Files.newInputStream(Paths.get(name), StandardOpenOption.DELETE_ON_CLOSE))
)(
is => Sync[F].delay(is.close())
)
)(is => Sync[F].delay(is.close()))
}
3 changes: 2 additions & 1 deletion src/main/scala/higherkindness/skeuomorph/avro/Protocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,8 @@ object Protocol {
none[String],
Nil,
none[String],
fields.map(f => Field(f.name, Nil, none[String], none[Order], f.tpe)))
fields.map(f => Field(f.name, Nil, none[String], none[Order], f.tpe))
)
}

def fromFreesFProtocol[T, U](protocol: mu.Protocol[T])(implicit T: Basis[MuF, T], U: Basis[AvroF, U]): Protocol[U] = {
Expand Down
14 changes: 8 additions & 6 deletions src/main/scala/higherkindness/skeuomorph/avro/schema.scala
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,15 @@ object AvroF {
namespace: Option[String],
aliases: List[String],
doc: Option[String],
fields: List[Field[A]])
extends AvroF[A]
fields: List[Field[A]]
) extends AvroF[A]
final case class TEnum[A](
name: String,
namespace: Option[String],
aliases: List[String],
doc: Option[String],
symbols: List[String])
extends AvroF[A]
symbols: List[String]
) extends AvroF[A]
final case class TUnion[A](options: NonEmptyList[A]) extends AvroF[A]
final case class TFixed[A](name: String, namespace: Option[String], aliases: List[String], size: Int) extends AvroF[A]

Expand Down Expand Up @@ -153,13 +153,15 @@ object AvroF {
namespace: Option[String],
aliases: List[String],
doc: Option[String],
fields: List[Field[A]]): AvroF[A] = TRecord(name, namespace, aliases, doc, fields)
fields: List[Field[A]]
): AvroF[A] = TRecord(name, namespace, aliases, doc, fields)
def enum[A](
name: String,
namespace: Option[String],
aliases: List[String],
doc: Option[String],
symbols: List[String]): AvroF[A] = TEnum(name, namespace, aliases, doc, symbols)
symbols: List[String]
): AvroF[A] = TEnum(name, namespace, aliases, doc, symbols)
def union[A](options: NonEmptyList[A]): AvroF[A] = TUnion(options)
def fixed[A](name: String, namespace: Option[String], aliases: List[String], size: Int): AvroF[A] =
TFixed(name, namespace, aliases, size)
Expand Down
14 changes: 9 additions & 5 deletions src/main/scala/higherkindness/skeuomorph/mu/codegen.scala
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ object codegen {
nestedCoproducts.traverse(_.as[Stat])
).mapN {
case (args, prods, coprods) =>
val caseClass = q"@message final case class ${Type.Name(name)}(..$args)"
val caseClass = q"final case class ${Type.Name(name)}(..$args)"
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here is the change.

if (prods.nonEmpty || coprods.nonEmpty) {
q"""
$caseClass
Expand All @@ -226,7 +226,8 @@ object codegen {
}

def service[T](srv: Service[T], streamCtor: (Type, Type) => Type.Apply)(
implicit T: Basis[MuF, T]): Either[String, Stat] = {
implicit T: Basis[MuF, T]
): Either[String, Stat] = {
val serializationType = Term.Name(srv.serializationType.toString)
val compressionType = Term.Name(srv.compressionType.toString)

Expand All @@ -249,14 +250,16 @@ object codegen {
}

def operation[T](op: Service.Operation[T], streamCtor: (Type, Type) => Type.Apply)(
implicit T: Basis[MuF, T]): Either[String, Decl.Def] =
implicit T: Basis[MuF, T]
): Either[String, Decl.Def] =
for {
reqType <- requestType(op.request, streamCtor)
respType <- responseType(op.response, streamCtor)
} yield q"def ${Term.Name(op.name)}(req: $reqType): $respType"

def requestType[T](opType: Service.OperationType[T], streamCtor: (Type, Type) => Type.Apply)(
implicit T: Basis[MuF, T]): Either[String, Type] =
implicit T: Basis[MuF, T]
): Either[String, Type] =
for {
tree <- schema(opType.tpe)
tpe <- tree.as[Type]
Expand All @@ -268,7 +271,8 @@ object codegen {
}

def responseType[T](opType: Service.OperationType[T], streamCtor: (Type, Type) => Type.Apply)(
implicit T: Basis[MuF, T]): Either[String, Type.Apply] =
implicit T: Basis[MuF, T]
): Either[String, Type.Apply] =
for {
tree <- schema(opType.tpe)
tpe <- tree.as[Type]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ object Comparison extends ComparisonInstances {
.getOrElse(Result.mismatch(UnionMemberRemoved(p)))
}
.toList
.combineAll)
.combineAll
)

case MatchInList(res, rep) =>
val firstMatch = res.find(Result.isMatch)
Expand Down Expand Up @@ -171,10 +172,12 @@ object Comparison extends ComparisonInstances {
.map {
case (item, idx) =>
path / Alternative(idx) -> (List(item.some), i2.toList.map(_.some)).tupled.map(p =>
(path / Alternative(idx), p._1, p._2))
(path / Alternative(idx), p._1, p._2)
)
}
.toList
.toMap)
.toMap
)
case (TSum(n, f), TSum(n2, f2)) if (n === n2 && f.forall(f2.toSet)) => same
case (TProduct(n, f, np, nc), TProduct(n2, f2, np2, nc2)) if (n === n2) =>
val fields = zipFields(path / Name(n), f, f2)
Expand Down Expand Up @@ -206,7 +209,8 @@ object Comparison extends ComparisonInstances {
case (TOption(i1), TEither(r1, r2)) =>
MatchInList(
Vector((path, i1.some, r1.some), (path, i1.some, r2.some)),
Reporter.promotedToEither(path, reader))
Reporter.promotedToEither(path, reader)
)

case (TEither(l1, r1), TCoproduct(rs)) =>
AlignUnionMembers(
Expand Down
35 changes: 20 additions & 15 deletions src/main/scala/higherkindness/skeuomorph/mu/protocol.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ object Protocol {
* create a [[higherkindness.skeuomorph.mu.Protocol]] from a [[higherkindness.skeuomorph.avro.Protocol]]
*/
def fromAvroProtocol[T, U](compressionType: CompressionType, useIdiomaticEndpoints: Boolean)(
proto: avro.Protocol[T])(implicit T: Basis[AvroF, T], U: Basis[MuF, U]): Protocol[U] = {
proto: avro.Protocol[T]
)(implicit T: Basis[AvroF, T], U: Basis[MuF, U]): Protocol[U] = {

val toMu: T => U = scheme.cata(transformAvro[U].algebra)
val toOperation: avro.Protocol.Message[T] => Service.Operation[U] =
Expand All @@ -63,7 +64,7 @@ object Protocol {
msg.name,
request = OperationType(toMu(msg.request), false),
response = OperationType(toMu(msg.response), false)
)
)

Protocol(
proto.name,
Expand All @@ -76,21 +77,24 @@ object Protocol {
SerializationType.Avro,
compressionType,
IdiomaticEndpoints(proto.namespace, useIdiomaticEndpoints),
proto.messages.map(toOperation))),
proto.messages.map(toOperation)
)
),
Nil
)
}

def fromProtobufProto[T, U](compressionType: CompressionType, useIdiomaticEndpoints: Boolean)(
protocol: protobuf.Protocol[T])(implicit T: Basis[ProtobufF, T], U: Basis[MuF, U]): Protocol[U] = {
protocol: protobuf.Protocol[T]
)(implicit T: Basis[ProtobufF, T], U: Basis[MuF, U]): Protocol[U] = {
val toMu: T => U = scheme.cata(transformProto[U].algebra)
val toOperation: protobuf.Protocol.Operation[T] => Service.Operation[U] =
msg =>
Service.Operation(
name = msg.name,
request = OperationType(toMu(msg.request), msg.requestStreaming),
response = OperationType(toMu(msg.response), msg.responseStreaming)
)
)

val toImports: DependentImport[T] => DependentImport[U] =
imp => DependentImport(imp.pkg, imp.protocol, toMu(imp.tpe))
Expand All @@ -101,15 +105,15 @@ object Protocol {
options = protocol.options,
declarations = protocol.declarations.map(toMu),
services = protocol.services
.map(
s =>
new Service[U](
s.name,
SerializationType.Protobuf,
compressionType,
IdiomaticEndpoints(Option(protocol.pkg), useIdiomaticEndpoints),
s.operations.map(toOperation)
)),
.map(s =>
new Service[U](
s.name,
SerializationType.Protobuf,
compressionType,
IdiomaticEndpoints(Option(protocol.pkg), useIdiomaticEndpoints),
s.operations.map(toOperation)
)
),
imports = protocol.imports.map(toImports)
)

Expand All @@ -122,7 +126,8 @@ final case class Service[T](
serializationType: SerializationType,
compressionType: CompressionType,
idiomaticEndpoints: IdiomaticEndpoints,
operations: List[Service.Operation[T]])
operations: List[Service.Operation[T]]
)
object Service {
final case class OperationType[T](tpe: T, stream: Boolean)
final case class Operation[T](name: String, request: OperationType[T], response: OperationType[T])
Expand Down
Loading