Skip to content

Commit

Permalink
Clean up redundant parentheses in generated server code (#514)
Browse files Browse the repository at this point in the history
Minor refactor to make the generated code a little more readable.
  • Loading branch information
pguillory committed Oct 19, 2020
1 parent e77f242 commit d6bfeea
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 61 deletions.
69 changes: 26 additions & 43 deletions example/lib/calculator/generated/service.ex

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 8 additions & 18 deletions lib/thrift/generator/binary/framed/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,8 @@ defmodule Thrift.Generator.Binary.Framed.Server do
fn_name = Atom.to_string(function.name)
handler_fn_name = Utils.underscore(function.name)
response_module = Module.concat(service_module, Service.module_name(function, :response))

body =
quote do
rsp = handler_module.unquote(handler_fn_name)()
unquote(build_responder(function.return_type, response_module))
end

handler_args = []
body = build_responder(function.return_type, handler_fn_name, handler_args, response_module)
handler = wrap_with_try_catch(body, function, file_group, response_module)

quote do
Expand Down Expand Up @@ -80,13 +75,7 @@ defmodule Thrift.Generator.Binary.Framed.Server do
defp build_handler_call(file_group, function, response_module) do
handler_fn_name = Utils.underscore(function.name)
handler_args = Enum.map(function.params, &Macro.var(&1.name, nil))

body =
quote do
rsp = handler_module.unquote(handler_fn_name)(unquote_splicing(handler_args))
unquote(build_responder(function.return_type, response_module))
end

body = build_responder(function.return_type, handler_fn_name, handler_args, response_module)
wrap_with_try_catch(body, function, file_group, response_module)
end

Expand Down Expand Up @@ -132,16 +121,17 @@ defmodule Thrift.Generator.Binary.Framed.Server do
end
end

defp build_responder(:void, _) do
defp build_responder(:void, handler_fn_name, handler_args, _response_module) do
quote do
_ = rsp
_result = handler_module.unquote(handler_fn_name)(unquote_splicing(handler_args))
:noreply
end
end

defp build_responder(_, response_module) do
defp build_responder(_, handler_fn_name, handler_args, response_module) do
quote do
response = %unquote(response_module){success: rsp}
result = handler_module.unquote(handler_fn_name)(unquote_splicing(handler_args))
response = %unquote(response_module){success: result}
{:reply, unquote(response_module).BinaryProtocol.serialize(response)}
end
end
Expand Down

0 comments on commit d6bfeea

Please sign in to comment.