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

Unify names: stubs, services, clients, behaviours #4

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
61 changes: 22 additions & 39 deletions lib/rpc_proto_gen_helpers/cli.ex
Original file line number Diff line number Diff line change
Expand Up @@ -54,18 +54,6 @@ defmodule RPCProtoGenHelpers.CLI do
files =
request.proto_file
|> Stream.filter(&rpc_service?/1)
Copy link

Choose a reason for hiding this comment

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

Suggest we get rid of this filter, can update build_service_metadata to handle an empty list of service instead!

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, let's do this in separate PR. It may cause issues with external (non-Fresha) services, not that this is this plugin's responsibility, but may need adjustments to workflows/scripts

|> Stream.map(fn file_descriptor_proto ->
%{
file_descriptor_proto: file_descriptor_proto,
legacy_name?: Regex.match?(~r|^rpc\.(\w+)\.v(\d+)$|, file_descriptor_proto.package)
}
end)
|> Stream.filter(fn %{
file_descriptor_proto: file_descriptor_proto,
legacy_name?: legacy_name?
} ->
legacy_name? or Regex.match?(~r|^fresha.*|, file_descriptor_proto.package)
end)
|> Stream.map(&build_service_metadata/1)
|> Enum.flat_map(fn metadata ->
[
Expand All @@ -87,32 +75,25 @@ defmodule RPCProtoGenHelpers.CLI do

# Assume 1 package per service
defp rpc_service?(%Google.Protobuf.FileDescriptorProto{service: [_]}), do: true
defp rpc_service?(%Google.Protobuf.FileDescriptorProto{service: []}), do: false

defp build_service_metadata(%{
file_descriptor_proto: %Google.Protobuf.FileDescriptorProto{
name: name,
package: package,
service: [
%Google.Protobuf.ServiceDescriptorProto{method: methods}
],
source_code_info: %Google.Protobuf.SourceCodeInfo{
location: source_code_locations
}
},
legacy_name?: legacy_name?
defp rpc_service?(%Google.Protobuf.FileDescriptorProto{service: _}), do: false

defp build_service_metadata(%Google.Protobuf.FileDescriptorProto{
name: path,
package: package,
service: [
%Google.Protobuf.ServiceDescriptorProto{name: service_name, method: methods}
],
CGA1123 marked this conversation as resolved.
Show resolved Hide resolved
source_code_info: %Google.Protobuf.SourceCodeInfo{
location: source_code_locations
}
}) do
service_name = name |> Path.basename() |> Path.rootname() |> String.replace("_service", "")
service_name_to_pascal = Recase.to_pascal(service_name)
service_name_from_path =
path |> Path.basename() |> Path.rootname() |> String.replace("_service", "")

service_name_from_path_to_pascal = Recase.to_pascal(service_name_from_path)
package_components = package |> String.split(".")
package_to_pascal = package_components |> Enum.map_join(".", &Recase.to_pascal/1)

stub =
if legacy_name? do
"#{package_to_pascal}.RPCService.Stub"
else
"#{package_to_pascal}.#{service_name_to_pascal}Service.Stub"
end
stub = "#{package_to_pascal}.#{service_name}.Stub"

comment_map = extract_comments(source_code_locations)

Expand All @@ -128,11 +109,13 @@ defmodule RPCProtoGenHelpers.CLI do
}
end)

behaviour_module = EExHelper.rpc_client_behaviour(service_name_to_pascal, methods)
impl_module = EExHelper.rpc_client_impl(service_name_to_pascal, stub, methods)
behaviour_module = EExHelper.rpc_client_behaviour(service_name_from_path_to_pascal, methods)
impl_module = EExHelper.rpc_client_impl(service_name_from_path_to_pascal, stub, methods)

behaviour_path =
Path.join(package_components ++ ["#{service_name_from_path}_client_behaviour.ex"])

behaviour_path = Path.join(package_components ++ ["#{service_name}_client_behaviour.ex"])
impl_path = Path.join(package_components ++ ["#{service_name}_client_impl.ex"])
impl_path = Path.join(package_components ++ ["#{service_name_from_path}_client_impl.ex"])

%{
behaviour_path: behaviour_path,
Expand Down