Skip to content

Commit ad3fd46

Browse files
authored
Conditional codegen import trigger (#79)
* Update import_cb codegen interface to allow for only triggering import when needed * Codegen: export after each service
1 parent 55bd686 commit ad3fd46

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

src/ProtoBuf.jl

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@ function service_cb(io, t::CodeGenerators.ServiceType, ctx::CodeGenerators.Conte
22
namespace = join(ctx.proto_file.preamble.namespace, ".")
33
service_name = t.name
44

5-
export_names = Vector{String}()
6-
75
for (i, rpc) in enumerate(t.rpcs)
86
rpc_path = "/$namespace.$service_name/$(rpc.name)"
97

@@ -18,7 +16,6 @@ function service_cb(io, t::CodeGenerators.ServiceType, ctx::CodeGenerators.Conte
1816
end
1917

2018
export_name = "$(service_name)_$(rpc.name)_Client"
21-
push!(export_names, export_name)
2219

2320
println(io, "$(export_name)(")
2421
println(io, "\thost, port;")
@@ -39,16 +36,26 @@ function service_cb(io, t::CodeGenerators.ServiceType, ctx::CodeGenerators.Conte
3936
println(io, "\tkeepalive=keepalive,")
4037
println(io, "\tmax_send_message_length=max_send_message_length,")
4138
println(io, "\tmax_recieve_message_length=max_recieve_message_length,")
42-
println(io, ")\n")
43-
end
39+
println(io, ")")
40+
41+
# TODO: define a standard way to check whether we should export that is used in both ProtoBuf.jl and gRPCClient.jl
42+
if CodeGenerators.is_namespaced(ctx.proto_file) || ctx.options.always_use_modules
43+
println(io, "export $(export_name)")
44+
else
45+
println(io, "")
46+
end
4447

45-
# TODO: define a standard way to check whether we should export that is used in both ProtoBuf.jl and gRPCClient.jl
46-
if !isempty(export_names) && (CodeGenerators.is_namespaced(ctx.proto_file) || ctx.options.always_use_modules)
47-
map(x->println(io, "export $x"), export_names)
48+
if i < lastindex(t.rpcs)
49+
println(io, "")
50+
end
4851
end
52+
4953
end
5054

51-
import_cb(io, ctx) = println(io, "import gRPCClient")
55+
import_cb(io, ctx, definitions) =
56+
mapreduce(x->x isa CodeGenerators.ServiceType ? 1 : 0, +, values(definitions)) > 0 &&
57+
println(io, "import gRPCClient")
58+
5259

5360
grpc_register_service_codegen() = CodeGenerators.register_external_codegen_handler(
5461
"gRPCClient.jl";

0 commit comments

Comments
 (0)