Skip to content

Commit

Permalink
Allow non-sequential arguments (#29)
Browse files Browse the repository at this point in the history
* Allow non-sequential argument numbers

* Demonstrate bug with non-sequential args
  • Loading branch information
binaryseed authored and scohen committed Jun 23, 2016
1 parent 4a2bbe1 commit f4a4654
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 15 deletions.
2 changes: 1 addition & 1 deletion lib/riffed/client.ex
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ defmodule Riffed.Client do

reply_meta = Riffed.Enumeration.get_overridden_type(function_name, :return_type, overrides, reply_meta)

arg_list = build_arg_list(length(param_meta))
arg_list = build_arg_list(param_meta)
{:{}, _, list_args} = build_handler_tuple_args(param_meta)
casts = build_casts(function_name, struct_module, param_meta, overrides, :to_erlang)

Expand Down
18 changes: 8 additions & 10 deletions lib/riffed/macro_helpers.ex
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
defmodule Riffed.MacroHelpers do
@moduledoc false
def build_arg_list(size) when is_integer(size) do
case size do
0 -> []
size ->
Enum.map(1..size, fn(param_idx) ->
:"arg_#{param_idx}"
|> Macro.var(nil)
end)
end
def build_arg_list(param_meta) do
param_meta
|> Enum.map(&build_arg(&1))
end

def build_handler_tuple_args(param_meta) do
args = param_meta |> length |> build_arg_list
args = build_arg_list(param_meta)
{:{}, [], args}
end

Expand All @@ -21,6 +15,10 @@ defmodule Riffed.MacroHelpers do
|> Enum.map(&build_arg_cast(function_name, struct_module, &1, overrides, cast_function))
end

defp build_arg({index, _type}=arg) do
Macro.var(:"arg_#{abs(index)}", nil)
end

defp build_arg_cast(function_name, struct_module, param_meta, overrides, cast_function) do
{index, param_type} = param_meta
param_name = :"arg_#{abs(index)}"
Expand Down
6 changes: 3 additions & 3 deletions lib/riffed/server.ex
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ defmodule Riffed.Server do
end
end

defp build_delegate_call(delegate_fn) do
defp build_delegate_call(delegate_fn, params_meta) do
delegate_info = :erlang.fun_info(delegate_fn)

arg_list = build_arg_list(delegate_info[:arity])
arg_list = build_arg_list(params_meta)

{{:., [], [{:__aliases__, [alias: false],
[delegate_info[:module]]}, delegate_info[:name]]}, [], arg_list}
Expand All @@ -116,7 +116,7 @@ defmodule Riffed.Server do
params_meta = function_meta[:params]
reply_meta = function_meta[:reply] |> Riffed.Struct.to_riffed_type_spec
tuple_args = build_handler_tuple_args(params_meta)
delegate_call = build_delegate_call(delegate_fn)
delegate_call = build_delegate_call(delegate_fn, params_meta)
casts = build_casts(thrift_fn_name, struct_module, params_meta, fn_overrides, :to_elixir)
overridden_type = Riffed.Enumeration.get_overridden_type(thrift_fn_name, :return_type, fn_overrides, reply_meta)

Expand Down
2 changes: 1 addition & 1 deletion thrift/server.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ struct UserBoardResponse {

service Server {
ConfigResponse config(1: ConfigRequest request, 2: i32 timestamp);
ActivityState setUserState(1: User user, 2: ActivityState status);
ActivityState setUserState(10: User user, 20: ActivityState status);
map<string, i32> dictFun(1: map<string, i32> dict);
map<string, User> dictUserFun(1: map<string, User> dict);
set<string> setFun(1: set<string> mySet);
Expand Down

0 comments on commit f4a4654

Please sign in to comment.