|
| 1 | +import language.experimental.namedTuples |
| 2 | +import NamedTuple.* |
| 3 | + |
| 4 | +@FailsWith[HttpError] |
| 5 | +trait GreetService derives HttpService: |
| 6 | + @HttpInfo("GET", "/greet/{name}") |
| 7 | + def greet(@HttpPath name: String): String |
| 8 | + @HttpInfo("POST", "/greet/{name}") |
| 9 | + def setGreeting(@HttpPath name: String, @HttpBody greeting: String): Unit |
| 10 | + |
| 11 | +@main def Test = |
| 12 | + |
| 13 | + val e = HttpService.endpoints[GreetService] |
| 14 | + |
| 15 | + println(e.greet.describe) |
| 16 | + println(e.setGreeting.describe) |
| 17 | + |
| 18 | + // Type-safe server logic, driven by the ops-mirror, |
| 19 | + // requires named tuple with same labels in the same order, |
| 20 | + // and function that matches the required signature. |
| 21 | + val logic = e.serverLogic: |
| 22 | + ( |
| 23 | + greet = (name) => Right("Hello, " + name), |
| 24 | + setGreeting = (name, greeting) => Right(()) |
| 25 | + ) |
| 26 | + |
| 27 | + val server = ServerBuilder() |
| 28 | + .handleAll(logic) |
| 29 | + .create(port = 8080) |
| 30 | + |
| 31 | + sys.addShutdownHook(server.close()) |
| 32 | + |
| 33 | +end Test |
| 34 | + |
| 35 | +// IMPLEMENTATION DETAILS FOLLOW |
| 36 | + |
| 37 | +/** Assume existence of macro to generate this */ |
| 38 | +given (OpsMirror.Of[GreetService] { |
| 39 | + type MirroredType = GreetService |
| 40 | + type OperationLabels = ("greet", "setGreeting") |
| 41 | + type Operations = ( |
| 42 | + OpsMirror.Operation { type InputTypes = (String *: EmptyTuple); type OutputType = String; type ErrorType = HttpError }, |
| 43 | + OpsMirror.Operation { type InputTypes = (String *: String *: EmptyTuple); type OutputType = Unit; type ErrorType = HttpError } |
| 44 | + ) |
| 45 | +}) = new OpsMirror: |
| 46 | + type MirroredType = GreetService |
| 47 | + type OperationLabels = ("greet", "setGreeting") |
| 48 | + type Operations = ( |
| 49 | + OpsMirror.Operation { type InputTypes = (String *: EmptyTuple); type OutputType = String; type ErrorType = HttpError }, |
| 50 | + OpsMirror.Operation { type InputTypes = (String *: String *: EmptyTuple); type OutputType = Unit; type ErrorType = HttpError } |
| 51 | + ) |
| 52 | + |
| 53 | +object OpsMirror: |
| 54 | + type Of[T] = OpsMirror { type MirroredType = T } |
| 55 | + |
| 56 | + type Operation_I[I <: Tuple] = Operation { type InputTypes = I } |
| 57 | + type Operation_O[O] = Operation { type OutputType = O } |
| 58 | + type Operation_E[E] = Operation { type ErrorType = E } |
| 59 | + |
| 60 | + trait Operation: |
| 61 | + type InputTypes <: Tuple |
| 62 | + type OutputType |
| 63 | + type ErrorType |
| 64 | + |
| 65 | +trait OpsMirror: |
| 66 | + type MirroredType |
| 67 | + type OperationLabels <: Tuple |
| 68 | + type Operations <: Tuple |
| 69 | + |
| 70 | +trait HttpService[T]: |
| 71 | + def route(str: String): Route |
| 72 | +trait Route |
| 73 | + |
| 74 | +type Func[I <: Tuple, O, E] = I match |
| 75 | + case EmptyTuple => Either[E, O] |
| 76 | + case t *: EmptyTuple => t => Either[E, O] |
| 77 | + case t *: u *: EmptyTuple => (t, u) => Either[E, O] |
| 78 | + |
| 79 | +type ToFunc[T] = T match |
| 80 | + case HttpService.Endpoint[i, o, e] => Func[i, o, e] |
| 81 | + |
| 82 | +final class FailsWith[E] extends scala.annotation.Annotation |
| 83 | +final class HttpInfo(method: String, route: String) extends scala.annotation.Annotation |
| 84 | +final class HttpBody() extends scala.annotation.Annotation |
| 85 | +final class HttpPath() extends scala.annotation.Annotation |
| 86 | + |
| 87 | +sealed trait HttpError |
| 88 | + |
| 89 | +object HttpService: |
| 90 | + opaque type Endpoint[I <: Tuple, O, E] = Route |
| 91 | + |
| 92 | + extension [I <: Tuple, O, E](e: Endpoint[I, O, E]) |
| 93 | + def describe: String = ??? // some thing that looks inside the Route to debug it |
| 94 | + |
| 95 | + type ToEndpoints[Ops <: Tuple] <: Tuple = Ops match |
| 96 | + case EmptyTuple => EmptyTuple |
| 97 | + case op *: ops => (op, op, op) match |
| 98 | + case (OpsMirror.Operation_I[i]) *: (OpsMirror.Operation_O[o]) *: (OpsMirror.Operation_E[e]) *: _ => |
| 99 | + Endpoint[i, o, e] *: ToEndpoints[ops] |
| 100 | + |
| 101 | + trait Handler |
| 102 | + |
| 103 | + class Endpoints[T](val model: HttpService[T]) extends Selectable: |
| 104 | + type Fields <: AnyNamedTuple |
| 105 | + def selectDynamic(name: String): Route = model.route(name) |
| 106 | + |
| 107 | + def serverLogic(funcs: NamedTuple[Names[Fields], Tuple.Map[DropNames[Fields], ToFunc]]): List[Handler] = ??? |
| 108 | + |
| 109 | + def derived[T](using OpsMirror.Of[T]): HttpService[T] = ??? // inline method to create routes |
| 110 | + |
| 111 | + def endpoints[T](using model: HttpService[T], m: OpsMirror.Of[T]): Endpoints[T] { |
| 112 | + type Fields = NamedTuple[m.OperationLabels, ToEndpoints[m.Operations]] |
| 113 | + } = |
| 114 | + new Endpoints(model) { type Fields = NamedTuple[m.OperationLabels, ToEndpoints[m.Operations]] } |
| 115 | + |
| 116 | +class ServerBuilder(): |
| 117 | + def handleAll(hs: List[HttpService.Handler]): this.type = this |
| 118 | + def create(port: Int): Server = Server() |
| 119 | + |
| 120 | +class Server(): |
| 121 | + def close(): Unit = () |
0 commit comments