-
Notifications
You must be signed in to change notification settings - Fork 71
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
Revises the "union" method in SchemaVisitor ... #302
Conversation
…e optimised dispatched logic * Creates a `Alt.Dispatcher` construct that aims at streamlining the implementation of the `union` methods in SchemaVisitors, which is the hardest due to the fact that the implementor has to think about memoization. * Creates an `EncoderK` typeclass (used by Alt.Dispatcher) to generalise over what is an encoder, and streamline further the implementation of union encoders. * Extract a generalisation of SchemaVisitor, named SchemaCompiler, which could be used to unify Schematic, which @kubukoz really really wants. * Examplifies the use of `Alt.Dispatcher` in DocumentEncoderSchemaVisitor
def absorb[A](f: A => Document): DocumentEncoder[A] = | ||
new DocumentEncoder[A] { | ||
def apply: A => Document = f | ||
override def canBeKey: Boolean = false |
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 is cheating a little but we're planning on removing that canBeKey
stuff in a subsequent PR
import smithy.api.Http | ||
|
||
object SchemaVisitorPathEncoder extends SchemaVisitor[MaybePathEncode] { self => |
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.
good one
This is a bit over my head, but I like it. We can eventually rewrite most of our implementation and get a free improvement with it |
There's not that many that actually encode things in this repo, and it might be a little bit less efficient than the raw management that the jsoniter interpreter does. |
trait Precompiler[F[_], G[_]] { self => | ||
def apply[A](label: String, instance: F[A]): G[A] | ||
def toPolyFunction[U]: PolyFunction[Alt[F, U, *], G] = | ||
new PolyFunction[Alt[F, U, *], G] { | ||
def apply[A](fa: Alt[F, U, A]): G[A] = self.apply(fa.label, fa.instance) | ||
} | ||
} |
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.
suggestion: some helper for converting from F ~> G
(e.g. if you want to simply lift a visitor to Precompiler) would be great (it'd just ignore the label
)
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.
tbh I don't actually have a need for this, so feel free to ignore
* | ||
* Useful in particular when encoding unions | ||
*/ | ||
trait EncoderK[F[_], Result] extends Contravariant[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.
Would this be useful?
implicit def encoderKForFunction[B]: EncoderK[* => B, B] =
new EncoderK[* => B, B] {
def apply[A](fa: A => B, a: A): B = fa(a)
def absorb[A](f: A => B): A => B = 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.
Mmm, possibly, doesn't hurt to add.
... to make it easier to write memoised dispatching logic
The
union
method in SchemaVisitor is an obvious footgun, because it makes it a hard to memoise the compilation of schemas for each alternative.This PR revises the
union
method to expose a new constructAlt.Dispatcher
, which does the heavylifting of memoizing the result of compiling schemas, and dispatching the union instance to the correct alternative.Alt.Dispatcher
construct that aims at streamlining theimplementation of the
union
methods in SchemaVisitors, which is thehardest due to the fact that the implementor has to think about memoization.
EncoderK
typeclass (used by Alt.Dispatcher) to generaliseover what is an encoder, and streamline further the implementation of
union encoders.
be used to unify Schematic, which @kubukoz really really wants.
Alt.Dispatcher
in DocumentEncoderSchemaVisitorThis should remove the need for #260