Clean up redundant parentheses in generated server code (#514)

Minor refactor to make the generated code a little more readable.
This commit is contained in:
Preston Guillory 2020-10-19 10:12:49 -07:00 committed by GitHub
parent e77f242358
commit d6bfeea36b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 34 additions and 61 deletions

View File

@ -836,14 +836,11 @@ defmodule(Calculator.Generated.Service) do
case(Elixir.Calculator.Generated.Service.AddArgs.BinaryProtocol.deserialize(binary_data)) do
{%Calculator.Generated.Service.AddArgs{left: left, right: right}, ""} ->
try do
rsp = handler_module.add(left, right)
result = handler_module.add(left, right)
response = %Calculator.Generated.Service.AddResponse{success: result}
(
response = %Calculator.Generated.Service.AddResponse{success: rsp}
{:reply,
Elixir.Calculator.Generated.Service.AddResponse.BinaryProtocol.serialize(response)}
)
{:reply,
Elixir.Calculator.Generated.Service.AddResponse.BinaryProtocol.serialize(response)}
catch
kind, reason ->
formatted_exception = Exception.format(kind, reason, System.stacktrace())
@ -875,16 +872,11 @@ defmodule(Calculator.Generated.Service) do
) do
{%Calculator.Generated.Service.DivideArgs{left: left, right: right}, ""} ->
try do
rsp = handler_module.divide(left, right)
result = handler_module.divide(left, right)
response = %Calculator.Generated.Service.DivideResponse{success: result}
(
response = %Calculator.Generated.Service.DivideResponse{success: rsp}
{:reply,
Elixir.Calculator.Generated.Service.DivideResponse.BinaryProtocol.serialize(
response
)}
)
{:reply,
Elixir.Calculator.Generated.Service.DivideResponse.BinaryProtocol.serialize(response)}
catch
:error, %Calculator.Generated.DivideByZeroError{} = e ->
response = %Calculator.Generated.Service.DivideResponse{e: e}
@ -924,16 +916,13 @@ defmodule(Calculator.Generated.Service) do
) do
{%Calculator.Generated.Service.MultiplyArgs{left: left, right: right}, ""} ->
try do
rsp = handler_module.multiply(left, right)
result = handler_module.multiply(left, right)
response = %Calculator.Generated.Service.MultiplyResponse{success: result}
(
response = %Calculator.Generated.Service.MultiplyResponse{success: rsp}
{:reply,
Elixir.Calculator.Generated.Service.MultiplyResponse.BinaryProtocol.serialize(
response
)}
)
{:reply,
Elixir.Calculator.Generated.Service.MultiplyResponse.BinaryProtocol.serialize(
response
)}
catch
kind, reason ->
formatted_exception = Exception.format(kind, reason, System.stacktrace())
@ -965,16 +954,13 @@ defmodule(Calculator.Generated.Service) do
) do
{%Calculator.Generated.Service.SubtractArgs{left: left, right: right}, ""} ->
try do
rsp = handler_module.subtract(left, right)
result = handler_module.subtract(left, right)
response = %Calculator.Generated.Service.SubtractResponse{success: result}
(
response = %Calculator.Generated.Service.SubtractResponse{success: rsp}
{:reply,
Elixir.Calculator.Generated.Service.SubtractResponse.BinaryProtocol.serialize(
response
)}
)
{:reply,
Elixir.Calculator.Generated.Service.SubtractResponse.BinaryProtocol.serialize(
response
)}
catch
kind, reason ->
formatted_exception = Exception.format(kind, reason, System.stacktrace())
@ -1009,16 +995,13 @@ defmodule(Calculator.Generated.Service) do
{%Calculator.Generated.Service.VectorProductArgs{left: left, right: right, type: type},
""} ->
try do
rsp = handler_module.vector_product(left, right, type)
result = handler_module.vector_product(left, right, type)
response = %Calculator.Generated.Service.VectorProductResponse{success: result}
(
response = %Calculator.Generated.Service.VectorProductResponse{success: rsp}
{:reply,
Elixir.Calculator.Generated.Service.VectorProductResponse.BinaryProtocol.serialize(
response
)}
)
{:reply,
Elixir.Calculator.Generated.Service.VectorProductResponse.BinaryProtocol.serialize(
response
)}
catch
kind, reason ->
formatted_exception = Exception.format(kind, reason, System.stacktrace())

View File

@ -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
@ -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
@ -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