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

Andymck/ts master/combined opts and template changes #2

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions priv/grpcbox_service_bhvr.erl
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,10 @@
-module({{module_name}}_bhvr).

{{#methods}}
-callback {{method}}({{^input_stream}}{{#output_stream}}{{pb_module}}:{{input}}(), grpcbox_stream:t(){{/output_stream}}{{/input_stream}}{{#input_stream}}{{^output_stream}}reference(), grpcbox_stream:t(){{/output_stream}}{{#output_stream}}reference(), grpcbox_stream:t(){{/output_stream}}{{/input_stream}}{{^input_stream}}{{^output_stream}}ctx:ctx(), {{pb_module}}:{{input}}(){{/output_stream}}{{/input_stream}}) ->
{{#output_stream}}ok{{/output_stream}}{{^output_stream}}{ok, {{pb_module}}:{{output}}(), ctx:ctx()}{{/output_stream}}{{#output_stream}} | {continue, grpcbox_stream:t()}{{/output_stream}} | grpcbox_stream:grpc_error_response().

-callback {{method}}({{^input_stream}}{{#output_stream}}{{pb_module}}:{{input}}(), grpcbox_stream:t(){{/output_stream}}{{/input_stream}}{{#input_stream}}{{^output_stream}}{{pb_module}}:{{input}}(), grpcbox_stream:t(){{/output_stream}}{{#output_stream}}{{pb_module}}:{{input}}(), grpcbox_stream:t(){{/output_stream}}{{/input_stream}}{{^input_stream}}{{^output_stream}}ctx:ctx(), {{pb_module}}:{{input}}(){{/output_stream}}{{/input_stream}}) ->
{{#output_stream}}ok{{/output_stream}}{{^output_stream}}{ok, {{pb_module}}:{{output}}(), ctx:ctx()}{{/output_stream}}{{#output_stream}} | {ok, grpcbox_stream:t()}{{/output_stream}}{{#output_stream}} | {ok, {{pb_module}}:{{output}}(), grpcbox_stream:t()}{{/output_stream}}{{#output_stream}} | {stop, grpcbox_stream:t()}{{/output_stream}}{{#output_stream}} | {stop, {{pb_module}}:{{output}}(), grpcbox_stream:t()}{{/output_stream}} | grpcbox_stream:grpc_error_response().
{{/methods}}

-callback init(grpcbox_stream:t()) -> grpcbox_stream:t().
-callback init(atom(), grpcbox_stream:t()) -> grpcbox_stream:t().
-callback handle_info(any(), grpcbox_stream:t()) -> grpcbox_stream:t().
-optional_callbacks([init/1, handle_info/2]).
-optional_callbacks([init/2, handle_info/2]).
2 changes: 1 addition & 1 deletion rebar.config
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{erl_opts, [debug_info]}.
{deps, [{gpb, "~> 4.13.0"}
{deps, [{gpb, "~> 4.19.2"}
]}.
8 changes: 5 additions & 3 deletions rebar.lock
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
{"1.1.0",
[{<<"gpb">>,{pkg,<<"gpb">>,<<"4.13.0">>},0}]}.
{"1.2.0",
[{<<"gpb">>,{pkg,<<"gpb">>,<<"4.19.5">>},0}]}.
[
{pkg_hash,[
{<<"gpb">>, <<"EA3A81F83524D5685395594B57BB515F4631A54D27514219D67024BD251C9E82">>}]}
{<<"gpb">>, <<"FF9D434308FD205C07B662A86C37E9579A8C9A04AD511D2C4DD63430A59F4729">>}]},
{pkg_hash_ext,[
{<<"gpb">>, <<"21B76750D66B459CE40CB14298FB38EF8E0161D9B672A5284585C02563806F8E">>}]}
].
37 changes: 21 additions & 16 deletions src/grpcbox_plugin_prv.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ handle_app(AppInfo, Options, State) ->
GrpcOpts = rebar_opts:get(Opts, grpc, []),
GpbOpts = proplists:get_value(gpb_opts, GrpcOpts, []),
BaseDir = rebar_app_info:dir(AppInfo),
OverrideGpbDDefaults = proplists:get_value(override_gpb_defaults, GrpcOpts, true),
GrpcCreateServices = proplists:get_value(create_services, GrpcOpts, true),
GrpcOptOutDir = proplists:get_value(out_dir, GrpcOpts, filename:join(BaseDir, "src")),
BeamOutDir = proplists:get_value(beam_out_dir, GrpcOpts, rebar_app_info:ebin_dir(AppInfo)),
GrpcKeepBeams = proplists:get_value(keep_beams, GrpcOpts, false),
GrpcOutDir = filename:join(BaseDir, GrpcOptOutDir),
GpbOutDir = filename:join(BaseDir, proplists:get_value(o, GpbOpts, GrpcOptOutDir)),

ProtoFiles = case proplists:get_all_values(proto_files, Options) of
ProtoFiles = case proplists:get_all_values(proto_files, Options) of
[] ->
case proplists:get_value(proto_files, GrpcOpts) of
undefined ->
Expand Down Expand Up @@ -98,7 +99,7 @@ handle_app(AppInfo, Options, State) ->
T
end,
Templates = templates(Type),
ProtoModules = [compile_pb(Filename, GpbOutDir, BeamOutDir, GpbOpts) || Filename <- ProtoFilesAbs],
ProtoModules = [compile_pb(Filename, GpbOutDir, BeamOutDir, GpbOpts, OverrideGpbDDefaults) || Filename <- ProtoFilesAbs],
case GrpcCreateServices of
true ->
[gen_services(Templates, ProtoModule, ProtoBeam, GrpcOutDir, GrpcOpts, State)
Expand All @@ -112,7 +113,7 @@ handle_app(AppInfo, Options, State) ->
false -> [file:delete(ProtoBeam) || {_ProtoModule, ProtoBeam} <- ProtoModules]
end.

compile_pb(Filename, OutDir, BeamOutDir, GpbOpts) ->
compile_pb(Filename, OutDir, BeamOutDir, GpbOpts, OverrideGpbDDefaults) ->
ModuleName = lists:flatten(
[proplists:get_value(module_name_prefix, GpbOpts, ""),
filename:basename(Filename, ".proto"),
Expand All @@ -123,16 +124,21 @@ compile_pb(Filename, OutDir, BeamOutDir, GpbOpts) ->
case needs_update(Filename, GeneratedPB) of
true ->
rebar_log:log(info, "Writing ~s", [GeneratedPB]),
%% unless the calling app overrides, we will default defs_as_proplists to true
%% resulting in service defs using proplists
%% Overriding the default with a value of false will result in the service defs using maps
GpbDefsAsProplists = proplists:get_value(defs_as_proplists, GpbOpts, true),
case gpb_compile:file(Filename, [{rename,{msg_fqname,base_name}},
use_packages,
{defs_as_proplists, GpbDefsAsProplists},
{i, "."},
{report_errors, false},
{o, OutDir} | GpbOpts]) of
%% if OverrideGpbDDefaults is true, drop all default gpb options
%% and use only those supplied by the calling service
%% otherwise append any supplied gpb options to grpcbox defaults
FinalGpbOpts =
case OverrideGpbDDefaults of
true ->
GpbOpts;
false ->
[{rename,{msg_name,snake_case}},
{rename,{msg_fqname,base_name}},
use_packages, maps,
strings_as_binaries, {i, "."},
{o, OutDir} | GpbOpts]
end,
case gpb_compile:file(Filename, FinalGpbOpts) of
ok ->
ok;
{error, Error} ->
Expand Down Expand Up @@ -184,8 +190,7 @@ gen_service_def(Service, ProtoModule, GrpcConfig, FullOutDir) ->
methods => [resolve_method(M, ProtoModule) || M <- Methods]}.

resolve_method(Method, ProtoModule) ->
%% if the gpb option defs_as_proplists is set to false, then service defs will be maps
%% so just force the methods to maps type here
%% handle methods as props or maps
rebar_log:log(info, "METHOD: ~p", [Method]),
MethodMap = ensure_map(Method),
MessageType = {message_type, ProtoModule:msg_name_to_fqbin(maps:get(input, MethodMap))},
Expand Down Expand Up @@ -247,4 +252,4 @@ log_warnings(Warnings) ->
ensure_map(S) when is_map(S)->
S;
ensure_map(S) when is_list(S)->
maps:from_list(S).
maps:from_list(S).