-
Notifications
You must be signed in to change notification settings - Fork 15
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
Frees & Avro schema fixes #13
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Few suggestions.
)): _*) | ||
) | ||
val withNamespace = namespace.fold(base) { n => | ||
base deepMerge Json.obj("namespace" -> Json.fromString(n)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this particular case, would deepMerge
be the same as:
base.mapObject( _.add ("namespace", Json.fromString(n) ) )
src/main/scala/avro/schema.scala
Outdated
"name" -> Json.fromString(name), | ||
"fields" -> Json.arr( | ||
fields.map( | ||
f => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This 4-line lambda should go to an auxiliary function def fieldToObj
?
"type" -> Json.fromString("map"), | ||
"values" -> values | ||
) | ||
case TRecord(name, namespace, aliases, doc, fields) => |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you are into a bit of Origami-like programming, you could write this case as follows:
- From each of
namespace
,aliases
, anddoc
, compute anOption[( String, Json) ]
:
val namespaceOF = namespace.map( n => "namespace" -> Json.fromString(n) _"
val aliasesOF = if (aliases.isEmpty) None else Some( "aliases" -> Json.arr(aliases.map(Json.fromString): _*) )
val docOF = doc.map( f => "doc" -> Json.fromString(f) )
- Add a function
addField(js: Json, of: Option[(String, Json)])
, which adds a field to a Json object. - Get the result as
List(namespaceOF, aliasesOF, docOF).foldLeft(base)(addField)
An alternative, but I would not say if it is cleaner. At your taste.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another alternative is that you start with the list of fields of base
, with type
, name
, fields
; and you make namespaceOf
or aliasesOf
into lists (empty or single), and you concatenate those lists, and apply the Json.obj
constructor at the end
build.sbt
Outdated
@@ -59,7 +59,8 @@ lazy val commonSettings = Seq( | |||
ThisBuild / scalacOptions -= "-Xplugin-require:macroparadise", | |||
libraryDependencies ++= Seq( | |||
%%("cats-core"), | |||
"io.higherkindness" %% "droste-core" % "0.3.1", | |||
"io.higherkindness" %% "droste-core" % "0.5.0", | |||
"io.higherkindness" %% "droste-macros" % "0.5.0", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Odd formatting here, we might want to apply the scalafmt syntax also for the sbt files.. makes sense?
case object AvroWithSchema extends SerializationType | ||
} | ||
|
||
case class Protocol[T]( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final?
} | ||
} | ||
|
||
case class Service[T](name: String, serializationType: SerializationType, operations: List[Service.Operation[T]]) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final case class?
src/main/scala/avro/Protocol.scala
Outdated
"request" -> Json.arr( | ||
Json.obj( | ||
"name" -> Json.fromString("arg"), //TODO: is this doable? | ||
"type" -> scheme.cata(AvroF.toJson).apply(message.request) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd recommend caching/saving scheme.cata(AvroF.toJson)
to a val (or lazy val) and then calling it here.
src/main/scala/avro/Protocol.scala
Outdated
"type" -> scheme.cata(AvroF.toJson).apply(message.request) | ||
) | ||
), | ||
"response" -> scheme.cata(AvroF.toJson).apply(message.response) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same as above.
case FreesF.TOption(value) => tUnion(NonEmptyList(tNull[T]().embed, List(value))) | ||
case FreesF.TList(value) => tArray(value) | ||
case FreesF.TMap(value) => tMap(value) | ||
case FreesF.TGeneric(_, _) => ??? // WAT |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the long term, you could do a TransM
through Option
to account for this failure scenario.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah, it's in my plans, but I need to do some changes to droste's traverse derivation, since its a bit far from flawless so far... (I need traverse to be able to use M
version of recursion schemes, and I don't want to implement by hand now :))
|
||
case class Service[T](name: String, serializationType: SerializationType, operations: List[Service.Operation[T]]) | ||
object Service { | ||
case class Operation[T](name: String, request: T, response: T) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
final
:)
@@ -40,46 +40,49 @@ object FreesF { | |||
case class TOption[A](value: A) extends FreesF[A] | |||
case class TList[A](value: A) extends FreesF[A] | |||
case class TMap[A](value: A) extends FreesF[A] | |||
case class TGeneric[A](generic: A, params: List[A]) extends FreesF[A] | |||
case class TRequired[A](value: A) extends FreesF[A] | |||
case class TCoproduct[A](invariants: NonEmptyList[A]) extends FreesF[A] | |||
case class TSum[A](name: String, fields: List[String]) extends FreesF[A] | |||
case class TProduct[A](name: String, fields: List[Field[A]]) extends FreesF[A] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
even more final
:)
532fdf6
to
1ffa734
Compare
ec0c249
to
ef9316f
Compare
This PR:
frees/service.scala
tofrees/protocol.scala
to be homogeneous with avro & protobufdroste
contrib module that has been already contributed to droste.AvroF
&FreesF
schemataAvroF[A]
tocirce.Json
and creates the renderer fromavro.Protocol[A]
tocirce.Json
.