Skip to content
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

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

Merged
merged 14 commits into from
Jun 25, 2024

Conversation

cornerman
Copy link
Owner

@cornerman cornerman commented Jun 24, 2024

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.

Copy link
Collaborator

@fdietze fdietze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@cornerman cornerman changed the title generate String comparison instead of HashMap for Router generate String comparison instead of HashMap for Router and Method data structure Jun 25, 2024
@cornerman cornerman changed the title generate String comparison instead of HashMap for Router and Method data structure generate String comparison instead of HashMap for Router and use Method data structure Jun 25, 2024
@cornerman cornerman merged commit 005e26e into master Jun 25, 2024
4 checks passed
@cornerman cornerman deleted the router-mapping-fast branch June 25, 2024 18:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants