cornerman / sloth

Type safe RPC in scala
MIT License
96 stars 11 forks source link

generate String comparison instead of HashMap for Router and use Method data structure #246

Closed cornerman closed 4 months ago

cornerman commented 4 months ago

This makes the Router#route macro generate more efficient code.

We now generate a direct string comparison for mapping the request to the right implementation, instead of a HashMap. An example macro expansion looks like this:

val value = ApiImplFunResponse;
val implRouter = sloth.Router.apply[test.PickleType, test.TypeHelper.ApiResultFun](TypeHelper.functor(SlothSpec.this.executionContext));
val impl = new _root_.sloth.internal.RouterImpl[test.PickleType, test.TypeHelper.ApiResultFun](implRouter);
implRouter.orElse(((endpoint) =>
    if (endpoint.traitName.$eq$eq("Api"))
        endpoint.methodName match {
            case "fun" => Some(((payload: test.PickleType) => impl.execute[scala.Tuple2[Int, String], Int](endpoint, payload)(((args) => value.fun(args._1, args._2)))))
            case "multi" => Some(((payload: test.PickleType) => impl.execute[scala.Tuple2[Int, Int], Int](endpoint, payload)(((args) => value.multi(args._1)(args._2)))))
            case _ => None
        }
    else None
))

We are also changing the reoccuring type path: List[String] to Method, which consists of the traitName and the methodName. We have added deprecated aliases for easier migration, it allows to work List[String] (now Method), PathName (now Name), PathNotFound (now MethodNotFound). These will be removed in the upcoming 1.0 release.