Skip to content

Commit d72370d

Browse files
hchokshimeta-codesync[bot]
authored andcommitted
Flatten C++ namespaces in py3 generator
Summary: - Simplify condition in `cpp_pass_field` helper - Flatten C++ namespaces property to return a namespace string, rename to `cpp_namespace` Reviewed By: vitaut Differential Revision: D95888004 fbshipit-source-id: 4c4cbc22aa0c0457d0962d8744037156fb1dc0f3
1 parent a9215cf commit d72370d

File tree

391 files changed

+798
-2518
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

391 files changed

+798
-2518
lines changed

thrift/compiler/generate/t_mstch_py3_generator.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -873,8 +873,9 @@ class t_mstch_py3_generator : public t_whisker_generator {
873873
}
874874
return whisker::make::array();
875875
});
876-
def.property("cppNamespaces", [](const t_program& self) {
877-
return to_whisker_string_array(cpp2::get_gen_namespace_components(self));
876+
def.property("cpp_namespace", [](const t_program& self) {
877+
return fmt::format(
878+
"{}", fmt::join(cpp2::get_gen_namespace_components(self), "::"));
878879
});
879880
def.property("py3Namespaces", [](const t_program& self) {
880881
return to_whisker_string_array(get_py3_namespace(&self));

thrift/compiler/generate/templates/py3/clients_wrapper.cpp.mustache

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,7 @@ thread that will pass data to the cython callback.
2828

2929
#include <{{program.include_prefix}}gen-py3/{{program.name}}/clients_wrapper.h>
3030

31-
{{#each program.cppNamespaces as |ns|}}namespace {{ns}} {
32-
{{/each}}
31+
namespace {{program.cpp_namespace}} {
3332
3433
3534
{{#each program.services as |service|}}
@@ -38,7 +37,7 @@ folly::Future<{{#partial clients.cpp_return_type function=function}}>
3837
{{service.name}}ClientWrapper::{{function.cpp_name}}(
3938
apache::thrift::RpcOptions& rpcOptions{{#each function.params.fields as |field|}},
4039
{{#partial types.cpp_value_type type=field.type}} arg_{{field.py_name}}{{/each}}) {
41-
auto* client = static_cast<::{{#each service.program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.cpp_name}}AsyncClient*>(async_client_.get());
40+
auto* client = static_cast<::{{service.program.cpp_namespace}}::{{service.cpp_name}}AsyncClient*>(async_client_.get());
4241
{{> clients/clients_wrapper.cpp_func_body}}
4342

4443
}
@@ -64,14 +63,13 @@ folly::Future<{{#partial clients.cpp_return_type function=function}}>
6463
{{service.name}}ClientWrapper::{{interaction.name}}InteractionWrapper::{{function.cpp_name}}(
6564
apache::thrift::RpcOptions& rpcOptions{{#each function.params.fields as |field|}},
6665
{{#partial types.cpp_value_type type=field.type}} arg_{{field.py_name}}{{/each}}) {
67-
auto* client = static_cast<::{{#each interaction.program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.cpp_name}}AsyncClient::{{interaction.name}}*>(async_client_.get());
66+
auto* client = static_cast<::{{service.program.cpp_namespace}}::{{service.cpp_name}}AsyncClient::{{interaction.name}}*>(async_client_.get());
6867
{{> clients/clients_wrapper.cpp_func_body}}
6968

7069
}
7170

7271
{{/each}}
7372
{{/each}}
74-
{{/each}}{{!
75-
}}{{#each program.cppNamespaces as |ns|}}} // namespace {{ns}}
7673
{{/each}}
74+
} // namespace {{program.cpp_namespace}}
7775
{{/if (not program.auto_migrate?)}}

thrift/compiler/generate/templates/py3/clients_wrapper.h.mustache

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,14 @@ Header file for ClientsWrapper.cpp for cython to import in CythonClients.pxd.
5151
#include <set>
5252
#include <vector>
5353

54-
{{#each program.cppNamespaces as |ns|}}namespace {{ns}} {
55-
{{/each}}
54+
namespace {{program.cpp_namespace}} {
5655
5756
{{#each program.services as |service|}}
5857
class {{service.name}}ClientWrapper : {{!
59-
}}{{#if (object.notnull? service.extends)}}public ::{{#each service.extends.program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.extends.name}}ClientWrapper{{#else}}public ::thrift::py3::ClientWrapper{{/if}} {
58+
}}{{#if (object.notnull? service.extends)}}public ::{{service.extends.program.cpp_namespace}}::{{service.extends.name}}ClientWrapper{{#else}}public ::thrift::py3::ClientWrapper{{/if}} {
6059
public:
6160
using {{#if (object.notnull? service.extends)}}{{!
62-
}}::{{#each service.extends.program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.extends.name}}ClientWrapper::{{service.extends.name}}ClientWrapper{{!
61+
}}::{{service.extends.program.cpp_namespace}}::{{service.extends.name}}ClientWrapper::{{service.extends.name}}ClientWrapper{{!
6362
}}{{#else}}::thrift::py3::ClientWrapper::ClientWrapper{{/if (object.notnull? service.extends)}};
6463

6564
{{#each service.supportedFunctions as |function|}}
@@ -89,8 +88,6 @@ class {{service.name}}ClientWrapper : {{!
8988
};
9089

9190

92-
{{/each}}{{!
93-
}}{{#each program.cppNamespaces as |ns|}}
94-
} // namespace {{ns}}
9591
{{/each}}
92+
} // namespace {{program.cpp_namespace}}
9693
{{/if (not program.auto_migrate?)}}

thrift/compiler/generate/templates/py3/clients_wrapper.pxd.mustache

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,13 @@ cimport {{include.modulePathPeriodSeparated}}.clients_wrapper as _{{include.modu
7171
{{/each}}
7272

7373
{{#each program.services as |service|}}
74-
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_clients.h"{{!
75-
}} namespace "{{#each program.cppNamespaces as |ns|}}::{{ns}}{{/each}}":
76-
cdef cppclass c{{service.name}}AsyncClient {{!
77-
}}"::{{#each program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{!
78-
}}{{service.cpp_name}}AsyncClient":
74+
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_clients.h" namespace "::{{program.cpp_namespace}}":
75+
cdef cppclass c{{service.name}}AsyncClient "::{{program.cpp_namespace}}::{{service.cpp_name}}AsyncClient":
7976
pass
8077

8178
{{#each service.supportedInteractions as |interaction|}}
82-
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_clients.h"{{!
83-
}} namespace "{{#each program.cppNamespaces as |ns|}}::{{ns}}{{/each}}":
84-
cdef cppclass c{{service.name}}AsyncClient_{{interaction.name}} {{!
85-
}}"::{{#each program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{!
86-
}}{{service.name}}AsyncClient::{{interaction.name}}":
79+
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_clients.h" namespace "::{{program.cpp_namespace}}":
80+
cdef cppclass c{{service.name}}AsyncClient_{{interaction.name}} "::{{program.cpp_namespace}}::{{service.name}}AsyncClient::{{interaction.name}}":
8781
pass
8882

8983
{{/each}}
@@ -95,12 +89,10 @@ cdef extern from "thrift/lib/cpp/TProcessorEventHandler.h" namespace "::apache::
9589
cdef cppclass cTProcessorEventHandler "apache::thrift::TProcessorEventHandler":
9690
pass
9791

98-
cdef extern from "{{program.include_prefix}}gen-py3/{{program.name}}/clients_wrapper.h"{{!
99-
}} namespace "{{#each program.cppNamespaces as |ns|}}::{{ns}}{{/each}}":{{!
92+
cdef extern from "{{program.include_prefix}}gen-py3/{{program.name}}/clients_wrapper.h" namespace "::{{program.cpp_namespace}}":{{!
10093
}}{{#if (array.empty? program.services)}}
10194
pass{{/if (array.empty? program.services)}}{{#each program.services as |service|}}
102-
cdef cppclass c{{service.name}}ClientWrapper "{{!
103-
}}::{{#each program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.name}}ClientWrapper"{{#partial client_wrapper_parent service=service}}:
95+
cdef cppclass c{{service.name}}ClientWrapper "::{{program.cpp_namespace}}::{{service.name}}ClientWrapper"{{#partial client_wrapper_parent service=service}}:
10496
{{#if (object.eq? null service.extends)}}
10597
void setPersistentHeader(const string& key, const string& value)
10698
void addEventHandler(const shared_ptr[cTProcessorEventHandler]& handler)
@@ -124,7 +116,7 @@ cdef extern from "{{program.include_prefix}}gen-py3/{{program.name}}/clients_wra
124116

125117
{{#each service.supportedInteractions as |interaction|}}
126118
cdef cppclass c{{service.name}}ClientWrapper_{{interaction.name}}InteractionWrapper "{{!
127-
}}::{{#each program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{service.name}}ClientWrapper{{!
119+
}}::{{program.cpp_namespace}}::{{service.name}}ClientWrapper{{!
128120
}}::{{interaction.name}}InteractionWrapper"(cClientWrapper):
129121
void setPersistentHeader(const string& key, const string& value)
130122
void addEventHandler(const shared_ptr[cTProcessorEventHandler]& handler)

thrift/compiler/generate/templates/py3/common/types.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ element types.
444444
{{#else if type.string_or_binary?}}std::string
445445
{{#else if (or type.enum? type.structured?)}}
446446
{{#let ttype=type.true_type}}
447-
::{{#each ttype.program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{ttype.cpp_name}}
447+
::{{ttype.program.cpp_namespace}}::{{ttype.cpp_name}}
448448
{{#else if (or type.list? type.set?)}}
449449
{{#let ttype=type.true_type}}
450450
{{type.cppTemplate}}<{{#partial cpp_value_type type=ttype.elem_type}}>

thrift/compiler/generate/templates/py3/metadata.cpp.mustache

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,14 @@
1515
limitations under the License.
1616
1717
}}
18-
{{#let partial service_qualified_name |service|}}
19-
{{#pragma ignore-newlines}}
20-
{{#each service.program.cppNamespaces as |ns|}}::{{ns}}{{/each}}
21-
::{{service.cpp_name}}
22-
{{/let partial}}
2318
{{> common/auto_generated_c}}
2419
{{#if (not program.auto_migrate?)}}
2520

2621
#include "{{program.include_prefix}}gen-py3/{{program.name}}/metadata.h"
2722

2823
#include "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_handlers.h"
2924

30-
{{#each program.cppNamespaces as |ns|}}
31-
namespace {{ns}} {
32-
{{/each}}
25+
namespace {{program.cpp_namespace}} {
3326
::apache::thrift::metadata::ThriftMetadata {{program.name}}_getThriftModuleMetadata() {
3427
::apache::thrift::metadata::ThriftServiceMetadataResponse response;
3528
::apache::thrift::metadata::ThriftMetadata& metadata = *response.metadata();
@@ -45,11 +38,9 @@ namespace {{ns}} {
4538
{{/each}}
4639
{{#each program.services as |service|}}
4740
::apache::thrift::detail::md::ServiceMetadata<::apache::thrift::ServiceHandler<{{!
48-
}}{{#partial service_qualified_name service=service}}>>::gen(response);
41+
}}::{{service.program.cpp_namespace}}::{{service.cpp_name}}>>::gen(response);
4942
{{/each}}
5043
return metadata;
5144
}
52-
{{#each program.cppNamespaces as |ns|}}
53-
} // namespace {{ns}}
54-
{{/each}}
45+
} // namespace {{program.cpp_namespace}}
5546
{{/if (not program.auto_migrate?)}}

thrift/compiler/generate/templates/py3/metadata.h.mustache

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,7 @@
2525

2626
#include "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_types.h"
2727

28-
{{#each program.cppNamespaces as |ns|}}
29-
namespace {{ns}} {
30-
{{/each}}
28+
namespace {{program.cpp_namespace}} {
3129
::apache::thrift::metadata::ThriftMetadata {{program.name}}_getThriftModuleMetadata();
32-
{{#each program.cppNamespaces as |ns|}}
33-
} // namespace {{ns}}
34-
{{/each}}
30+
} // namespace {{program.cpp_namespace}}
3531
{{/if (not program.auto_migrate?)}}

thrift/compiler/generate/templates/py3/metadata.pxd.mustache

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ from thrift.python.common cimport (
2525

2626
cdef extern from "{{program.include_prefix}}gen-py3/{{program.name}}/metadata.h"{{!
2727
}} :
28-
cdef cThriftMetadata cGetThriftModuleMetadata "{{#each program.cppNamespaces as |ns|}}::{{ns}}{{/each}}::{{program.name}}_getThriftModuleMetadata"()
28+
cdef cThriftMetadata cGetThriftModuleMetadata "::{{program.cpp_namespace}}::{{program.name}}_getThriftModuleMetadata"()
2929
{{/if (not program.auto_migrate?)}}

thrift/compiler/generate/templates/py3/services/cpp_pass_field.mustache

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,12 @@
1515
limitations under the License.
1616
1717
}}{{!
18-
1918
defines how a variable in a field should be passed into another c++ function
2019
from the current function. ie, when calling some_func(a, b, c), the variables
2120
a, b, and c would be written using this file. Most variables can be passed
2221
as themselves by value, but the complex types are unique_ptr values and
2322
need to be moved to the called function.
24-
2523
}}
2624
{{#pragma ignore-newlines}}
27-
{{#if (or field.type.bool? field.type.byte? field.type.i16? field.type.i32? field.type.i64? field.type.double? field.type.float? field.type.enum?)}}
28-
{{field.cpp_name}}
29-
{{#else if (or field.type.string? field.type.binary? field.type.structured? field.type.container?)}}
30-
std::move({{field.cpp_name}})
31-
{{/if (or field.type.bool? field.type.byte? field.type.i16? field.type.i32? field.type.i64? field.type.double? field.type.float? field.type.enum?)}}
25+
{{#let should_move=(or field.type.string? field.type.binary? field.type.structured? field.type.container?)}}
26+
{{#if should_move}}std::move({{field.cpp_name}}){{#else}}{{field.cpp_name}}{{/if}}

thrift/compiler/generate/templates/py3/services_interface.pxd.mustache

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ Reflection functions for clients and services
2424

2525
{{#each program.services as |service|}}
2626

27-
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_handlers.h"{{!
28-
}} namespace "{{#each program.cppNamespaces as |ns|}}::{{ns}}{{/each}}":
29-
cdef cppclass c{{service.cpp_name}}SvIf {{!
30-
}}"::{{#each program.cppNamespaces as |ns|}}{{ns}}::{{/each}}{{!
31-
}}{{service.cpp_name}}SvIf":
27+
cdef extern from "{{program.include_prefix}}{{program.cpp_gen_path}}/{{program.name}}_handlers.h" namespace "::{{program.cpp_namespace}}":
28+
cdef cppclass c{{service.cpp_name}}SvIf "::{{program.cpp_namespace}}::{{service.cpp_name}}SvIf":
3229
pass
3330
{{/each}}
3431
{{/if (not program.auto_migrate?)}}

0 commit comments

Comments
 (0)