wiro icon indicating copy to clipboard operation
wiro copied to clipboard

Add annotation for applying custom functions to routes

Open calippo opened this issue 7 years ago • 4 comments

requirements

  1. Wiro user should be allowed to modify all the routes of a given server applying a custom function on the whole router.
  2. Wiro user should be allowed to selectively modify wiro routes applying custom functions.

specs

Requirement 1 The function should be passed to HttpRPCServer. Passing the function to deriveRouter would be messy (see discussion).

Requirement 2

class Cat {
  val f: Route => Route

  @command
  @apply(f)
  def meow ...
}

Implementation should be pretty straightforward using MethodMetaData.

Note: Route => Route signature is possibly correct, though I'm not sure about it. Consider changing the signature to match the requirement. Even the annotation name apply might be misleading in scala, feel free to propose something different.

misc

{optional: other useful info}

calippo avatar Jan 16 '18 09:01 calippo

why putting the f function inside the Class?

I would like to define this at the router level, e.g. in the deriveRouter method, or - even better - at the Server level.

gabro avatar Jan 16 '18 09:01 gabro

Applying f on the specific method has finer granularity. The use case I had in mind was to selectively applying this to a few routes. We can actually have both the annotation on the method and an additional argument in deriveRoute.

Implementation-wise adding an optional argument to deriveRoute macro is a little trickier than adding an annotation. We are currently handling optional arguments in macros using method overloading... we would end up with something like:

def deriveRouter[A](a: A): Router
def deriveRouter[A](a: A, printer: Printer): Router
def deriveRouter[A](a: A, printer: Printer, f: Route => Route): Router
def deriveRouter[A](a: A, f: Route => Route): Router

If we want to add other arguments to deriveRouter I think we need to find a smarter way.

calippo avatar Jan 16 '18 10:01 calippo

My concern is that if I want to transform any request across my router definition, I now have to do it in N places.

We definitely need a way to do this for the whole Server.

gabro avatar Jan 16 '18 10:01 gabro

We can do that on HttpRPCServer... should be pretty easy.

I updated the requirements... Do requirement 2 (@apply(f) def meow = ???) make sense at all?

calippo avatar Jan 16 '18 11:01 calippo