Skip to content

Commit 8ac8326

Browse files
committed
refactor proto-structs: prettify IO and bundles
1. rename `forward_declarations.inc.jinja` -\> `types_forward_declarations.inc.jinja` 2. `VanillaType` -\> `VanillaTypeDeclaration` 3. `VanillaClass` -\> `VanillaClassDeclaration` 4. don't use `trim` in jinja templates 5. not append includes that are in bundles commit_hash:471f9699744675f724988f08431957920e9a342b
1 parent 3dc06a1 commit 8ac8326

File tree

12 files changed

+107
-54
lines changed

12 files changed

+107
-54
lines changed

.mapping.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2592,8 +2592,8 @@
25922592
"libraries/proto-structs/codegen-tests/proto/not_recommended_field_names/basic.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/not_recommended_field_names/basic.proto",
25932593
"libraries/proto-structs/codegen-tests/proto/oneof/basic.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/oneof/basic.proto",
25942594
"libraries/proto-structs/codegen-tests/proto/oneof/proto2.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/oneof/proto2.proto",
2595+
"libraries/proto-structs/codegen-tests/proto/simple/base.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/simple/base.proto",
25952596
"libraries/proto-structs/codegen-tests/proto/simple/cycles.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/simple/cycles.proto",
2596-
"libraries/proto-structs/codegen-tests/proto/simple/file1.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/simple/file1.proto",
25972597
"libraries/proto-structs/codegen-tests/proto/simple/subdirectory/separate_enum.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/simple/subdirectory/separate_enum.proto",
25982598
"libraries/proto-structs/codegen-tests/proto/simple/subdirectory/subdirectory.proto":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/proto/simple/subdirectory/subdirectory.proto",
25992599
"libraries/proto-structs/codegen-tests/src/cycles_test.cpp":"taxi/uservices/userver/libraries/proto-structs/codegen-tests/src/cycles_test.cpp",
@@ -4268,6 +4268,7 @@
42684268
"scripts/proto_structs/models/__init__.py":"taxi/uservices/userver/scripts/proto_structs/models/__init__.py",
42694269
"scripts/proto_structs/models/gen_node.py":"taxi/uservices/userver/scripts/proto_structs/models/gen_node.py",
42704270
"scripts/proto_structs/models/includes.py":"taxi/uservices/userver/scripts/proto_structs/models/includes.py",
4271+
"scripts/proto_structs/models/includes_bundles.py":"taxi/uservices/userver/scripts/proto_structs/models/includes_bundles.py",
42714272
"scripts/proto_structs/models/io.py":"taxi/uservices/userver/scripts/proto_structs/models/io.py",
42724273
"scripts/proto_structs/models/names.py":"taxi/uservices/userver/scripts/proto_structs/models/names.py",
42734274
"scripts/proto_structs/models/options.py":"taxi/uservices/userver/scripts/proto_structs/models/options.py",
@@ -4276,12 +4277,12 @@
42764277
"scripts/proto_structs/models/type_ref.py":"taxi/uservices/userver/scripts/proto_structs/models/type_ref.py",
42774278
"scripts/proto_structs/models/type_ref_consts.py":"taxi/uservices/userver/scripts/proto_structs/models/type_ref_consts.py",
42784279
"scripts/proto_structs/models/vanilla.py":"taxi/uservices/userver/scripts/proto_structs/models/vanilla.py",
4279-
"scripts/proto_structs/templates/forward_declarations.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/forward_declarations.inc.jinja",
42804280
"scripts/proto_structs/templates/io.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/io.inc.jinja",
42814281
"scripts/proto_structs/templates/io_forward_declarations.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/io_forward_declarations.inc.jinja",
42824282
"scripts/proto_structs/templates/structs.usrv.cpp.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/structs.usrv.cpp.jinja",
42834283
"scripts/proto_structs/templates/structs.usrv.hpp.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/structs.usrv.hpp.jinja",
42844284
"scripts/proto_structs/templates/types.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/types.inc.jinja",
4285+
"scripts/proto_structs/templates/types_forward_declarations.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/types_forward_declarations.inc.jinja",
42854286
"scripts/proto_structs/templates/utils.inc.jinja":"taxi/uservices/userver/scripts/proto_structs/templates/utils.inc.jinja",
42864287
"scripts/rabbitmq/ubuntu_install_rabbitmq_dev.sh":"taxi/uservices/userver/scripts/rabbitmq/ubuntu_install_rabbitmq_dev.sh",
42874288
"scripts/rabbitmq/ubuntu_install_rabbitmq_server.sh":"taxi/uservices/userver/scripts/rabbitmq/ubuntu_install_rabbitmq_server.sh",

libraries/proto-structs/codegen-tests/src/simple_test.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
#include <userver/proto-structs/convert.hpp>
66

7-
#include <simple/file1.pb.h>
8-
#include <simple/file1.structs.usrv.pb.hpp>
7+
#include <simple/base.pb.h>
8+
#include <simple/base.structs.usrv.pb.hpp>
99

1010
namespace ss = simple::structs;
1111

scripts/proto_structs/models/gen_node.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ def __init__(self, *, proto_relative_path: pathlib.Path, children: Sequence[Code
8080
def collect_includes(self) -> Iterable[includes.Include]:
8181
for child in self.children:
8282
yield from child.collect_includes()
83+
yield includes.Include(path='cstddef', kind=includes.IncludeKind.FOR_HPP)
84+
yield includes.Include(path='utility', kind=includes.IncludeKind.FOR_HPP)
8385

8486
def gen_path(self, *, ext: str) -> pathlib.Path:
8587
"""Path to the generated userver proto structs file."""
@@ -191,6 +193,18 @@ def own_includes(self) -> Iterable[includes.Include]:
191193
)
192194
for field in self.fields:
193195
yield from field.field_type.collect_includes()
196+
yield includes.Include(path='userver/proto-structs/io/fwd.hpp', kind=includes.IncludeKind.FOR_HPP)
197+
198+
# For IO.
199+
yield from [
200+
includes.Include(path=path, kind=includes.IncludeKind.FOR_CPP)
201+
for path in [
202+
'userver/proto-structs/io/impl/field_accessor.hpp',
203+
'userver/proto-structs/io/impl/read.hpp',
204+
'userver/proto-structs/io/impl/write.hpp',
205+
'userver/proto-structs/type_mapping.hpp',
206+
]
207+
]
194208

195209
@override
196210
def type_dependencies(self) -> Iterable[type_ref.TypeDependency]:
@@ -315,6 +329,7 @@ def kind(self) -> str:
315329
def own_includes(self) -> Iterable[includes.Include]:
316330
for field in self.fields:
317331
yield from field.field_type.collect_includes()
332+
yield includes.Include(path='userver/proto-structs/impl/oneof_codegen.hpp', kind=includes.IncludeKind.FOR_HPP)
318333

319334
@override
320335
def type_dependencies(self) -> Iterable[type_ref.TypeDependency]:
@@ -323,7 +338,7 @@ def type_dependencies(self) -> Iterable[type_ref.TypeDependency]:
323338
yield from field.field_type.type_dependencies()
324339

325340

326-
class VanillaType(TypeNode):
341+
class VanillaTypeDeclaration(TypeNode):
327342
"""A C++ vanilla type."""
328343

329344
def __init__(
@@ -336,7 +351,7 @@ def __init__(
336351
self.vanilla_name: Final[names.TypeName] = vanilla_name
337352

338353

339-
class VanillaClass(VanillaType):
354+
class VanillaClassDeclaration(VanillaTypeDeclaration):
340355
"""A C++ vanilla class."""
341356

342357
@property

scripts/proto_structs/models/includes.py

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from typing import List
1010
from typing import Optional
1111

12+
from proto_structs.models import includes_bundles
1213
from proto_structs.models import names
1314

1415

@@ -21,7 +22,7 @@ class IncludeKind(enum.Enum):
2122
FOR_CPP = enum.auto()
2223

2324

24-
@dataclasses.dataclass
25+
@dataclasses.dataclass(order=True, frozen=True)
2526
class Include:
2627
"""A C++ include"""
2728

@@ -30,12 +31,6 @@ class Include:
3031
#: Include type.
3132
kind: IncludeKind
3233

33-
def __hash__(self):
34-
return hash(self.path)
35-
36-
def __lt__(self, other: 'Include') -> bool:
37-
return self.path < other.path
38-
3934

4035
class HasCppIncludes(abc.ABC):
4136
"""A C++ entity or entity group that may require includes to define."""
@@ -53,8 +48,16 @@ def sorted_includes(entity: HasCppIncludes, *, current_hpp: Optional[str] = None
5348
includes_set.discard(Include(path=current_hpp, kind=IncludeKind.FOR_HPP))
5449
sorted_includes = sorted(includes_set)
5550
return {
56-
IncludeKind.FOR_HPP: [include.path for include in sorted_includes if include.kind == IncludeKind.FOR_HPP],
57-
IncludeKind.FOR_CPP: [include.path for include in sorted_includes if include.kind == IncludeKind.FOR_CPP],
51+
IncludeKind.FOR_HPP: [
52+
include.path
53+
for include in sorted_includes
54+
if include.kind == IncludeKind.FOR_HPP and include.path not in includes_bundles.BUNDLE_HPP
55+
],
56+
IncludeKind.FOR_CPP: [
57+
include.path
58+
for include in sorted_includes
59+
if include.kind == IncludeKind.FOR_CPP and include.path not in includes_bundles.BUNDLE_CPP
60+
],
5861
}
5962

6063

@@ -68,6 +71,11 @@ def proto_path_to_vanilla_pb_h(path: pathlib.Path) -> str:
6871
return str(path.with_suffix('.pb.h'))
6972

7073

74+
def structs_path_to_vanilla_pb_h(path: str) -> str:
75+
"""Returns the path of vanilla C++ `.pb.h` file (relative to source dir)."""
76+
return path.removesuffix('.structs.usrv.pb.hpp') + '.pb.h'
77+
78+
7179
def io_includes_by_full_name(name: str, *, prefix: str) -> List[Include]:
7280
"""Returns includes for IO."""
7381

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Sets of includes that are in bundles."""
2+
3+
from typing import Set
4+
5+
BUNDLE_HPP: Set[str] = {
6+
'cstddef',
7+
'cstdint',
8+
'limits',
9+
'optional',
10+
'string',
11+
'utility',
12+
'vector',
13+
'userver/proto-structs/impl/oneof_codegen.hpp',
14+
'userver/proto-structs/io/fwd.hpp',
15+
}
16+
17+
BUNDLE_CPP: Set[str] = {
18+
'userver/proto-structs/io/impl/field_accessor.hpp',
19+
'userver/proto-structs/io/impl/read.hpp',
20+
'userver/proto-structs/io/impl/write.hpp',
21+
'userver/proto-structs/type_mapping.hpp',
22+
'userver/proto-structs/io/std/optional_conv.hpp',
23+
'userver/proto-structs/io/std/string_conv.hpp',
24+
'userver/proto-structs/io/std/vector_conv.hpp',
25+
'userver/proto-structs/io/std/int32_t_conv.hpp',
26+
'userver/proto-structs/io/std/int64_t_conv.hpp',
27+
'userver/proto-structs/io/std/size_t_conv.hpp',
28+
'userver/proto-structs/io/std/uint32_t_conv.hpp',
29+
'userver/proto-structs/io/std/uint64_t_conv.hpp',
30+
'userver/proto-structs/io/std/scalar_conv.hpp',
31+
}

scripts/proto_structs/models/type_ref.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,7 @@ def full_cpp_name_segments(self) -> Sequence[str]:
123123

124124
@override
125125
def collect_includes(self) -> Iterable[includes.Include]:
126-
# For IO.
127-
yield includes.Include(
128-
path='userver/proto-structs/impl/bundles/structs_cpp.hpp', kind=includes.IncludeKind.FOR_CPP
129-
)
126+
yield includes.Include(path='userver/proto-structs/io/std/scalar_conv.hpp', kind=includes.IncludeKind.FOR_CPP)
130127

131128

132129
class BuiltinType(TypeReference, names.HasCppNameImpl):
@@ -194,7 +191,7 @@ def full_cpp_name_segments(self) -> Sequence[str]:
194191
def collect_includes(self) -> Iterable[includes.Include]:
195192
yield includes.Include(path=self._include, kind=includes.IncludeKind.FOR_HPP)
196193
yield includes.Include(
197-
path=self._include.removesuffix('.structs.usrv.pb.hpp') + '.pb.h', kind=includes.IncludeKind.FOR_CPP
194+
path=includes.structs_path_to_vanilla_pb_h(self._include), kind=includes.IncludeKind.FOR_CPP
198195
)
199196

200197

scripts/proto_structs/models/vanilla.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
from proto_structs.models import names
77

88

9-
def collect_vanilla_types(*, types: List[gen_node.TypeNode]) -> List[gen_node.VanillaType]:
10-
vanillas: List[gen_node.VanillaType] = []
9+
def collect_vanilla_types(*, types: List[gen_node.TypeNode]) -> List[gen_node.VanillaTypeDeclaration]:
10+
vanillas: List[gen_node.VanillaTypeDeclaration] = []
1111
for type_node in types:
1212
for child in type_node.iter_all_children():
1313
if isinstance(child, gen_node.StructNode):
1414
vanilla_name: names.TypeName = names.get_vanilla_type_name(name=child.vanilla_name)
15-
vanillas.append(gen_node.VanillaClass(vanilla_name=vanilla_name))
15+
vanillas.append(gen_node.VanillaClassDeclaration(vanilla_name=vanilla_name))
1616
return vanillas

scripts/proto_structs/templates/io.inc.jinja

Lines changed: 28 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -35,40 +35,41 @@ namespace {{ namespace.short_name }} {
3535
{% endmacro -%}
3636

3737
{%- macro do_render_vanilla(struct) %}
38-
::{{ struct.vanilla_full_name }}
39-
{% endmacro -%}
38+
::{{ struct.vanilla_full_name -}}
39+
{%- endmacro -%}
4040

4141
{%- macro do_render_field_number(struct, field) %}
42-
{{ do_render_vanilla(struct) | trim }}::{{ field.field_number_name }}
43-
{% endmacro -%}
42+
{{ do_render_vanilla(struct) -}}::{{ field.field_number_name }}
43+
{%- endmacro -%}
4444

4545
{%- macro do_render_field_set(struct, field) %}
46-
&{{ do_render_vanilla(struct) | trim }}::set_{{ field.short_vanilla_name }}
47-
{% endmacro -%}
46+
&{{ do_render_vanilla(struct) -}}::set_{{ field.short_vanilla_name }}
47+
{%- endmacro -%}
4848

4949
{%- macro do_render_field_clear(struct, field) %}
50-
&{{ do_render_vanilla(struct) | trim }}::clear_{{ field.short_vanilla_name }}
51-
{% endmacro -%}
50+
&{{ do_render_vanilla(struct) -}}::clear_{{ field.short_vanilla_name }}
51+
{%- endmacro -%}
5252

5353
{%- macro do_render_field_mutable(struct, field) %}
54-
&{{ do_render_vanilla(struct) | trim }}::mutable_{{ field.short_vanilla_name }}
55-
{% endmacro -%}
54+
&{{ do_render_vanilla(struct) -}}::mutable_{{ field.short_vanilla_name }}
55+
{%- endmacro -%}
5656

5757
{%- macro do_render_ptr_getter(struct, field) %}
58-
&{{ do_render_vanilla(struct) | trim }}::{{ field.short_vanilla_name }}
59-
{% endmacro -%}
58+
&{{ do_render_vanilla(struct) -}}::{{ field.short_vanilla_name }}
59+
{%- endmacro -%}
6060

6161
{%- macro do_render_ptr_has(struct, field) %}
62-
&{{ do_render_vanilla(struct) | trim }}::has_{{ field.short_vanilla_name }}
63-
{% endmacro -%}
62+
&{{ do_render_vanilla(struct) -}}::has_{{ field.short_vanilla_name }}
63+
{%- endmacro -%}
6464

6565
{%- macro do_render_field_getter(struct, field, pass_has) %}
6666
USERVER_NAMESPACE::proto_structs::io::impl::CreateFieldGetter(msg, ::{{ struct.vanilla_full_name }}::{{ field.field_number_name }},
6767
{% filter indent(width=4, first=True) %}
68-
{{ do_render_ptr_getter(struct, field) }}
69-
{%- if pass_has %},{{ do_render_ptr_has(struct, field) | trim }}{% endif %})
68+
{{ do_render_ptr_getter(struct, field) -}}
69+
{%- if pass_has -%},{{ do_render_ptr_has(struct, field) -}}{% endif %}
70+
)
7071
{% endfilter %}
71-
{% endmacro -%}
72+
{%- endmacro -%}
7273

7374
{%- macro do_render_read_field(struct, context, field) %}
7475
.{{ field.short_name }} = USERVER_NAMESPACE::proto_structs::io::impl::ReadField<decltype({{ struct.contextual_cpp_name(context=context) }}::{{ field.short_name }})>(ctx,
@@ -80,7 +81,7 @@ USERVER_NAMESPACE::proto_structs::io::impl::CreateFieldGetter(msg, ::{{ struct.v
8081
{%- elif field.read_kind == "oneof" %}
8182
{%- for oneof_field in field.oneof_fields %}
8283
{{ do_render_field_getter(struct, oneof_field, True) }}
83-
{%- if not loop.last %},{% endif %}
84+
{%- if not loop.last -%},{%- endif %}
8485
{% endfor %}
8586
{% endif %}
8687
),
@@ -109,9 +110,9 @@ return {
109110
USERVER_NAMESPACE::proto_structs::io::impl::CreateFieldSetter(
110111
{% filter indent(width=4, first=True) %}
111112
msg,
112-
{{ do_render_field_number(struct, field) | trim }},
113-
{{ do_render_field_set(struct, field) | trim }}<const std::string&>,
114-
{{ do_render_field_clear(struct, field) | trim }}
113+
{{ do_render_field_number(struct, field) }},
114+
{{ do_render_field_set(struct, field) }}<const std::string&>,
115+
{{ do_render_field_clear(struct, field) }}
115116
{% endfilter %}
116117
)
117118
{% endmacro -%}
@@ -120,9 +121,9 @@ msg,
120121
USERVER_NAMESPACE::proto_structs::io::impl::CreateFieldSetter(
121122
{% filter indent(width=4, first=True) %}
122123
msg,
123-
{{ do_render_field_number(struct, field) | trim }},
124-
{{ do_render_field_set(struct, field) | trim }},
125-
{{ do_render_field_clear(struct, field) | trim}}
124+
{{ do_render_field_number(struct, field) }},
125+
{{ do_render_field_set(struct, field) }},
126+
{{ do_render_field_clear(struct, field)}}
126127
{% endfilter %}
127128
)
128129
{% endmacro -%}
@@ -131,9 +132,9 @@ msg,
131132
USERVER_NAMESPACE::proto_structs::io::impl::CreateFieldSetter(
132133
{% filter indent(width=4, first=True) %}
133134
msg,
134-
{{ do_render_field_number(struct, field) | trim }},
135-
{{ do_render_field_mutable(struct, field) | trim }},
136-
{{ do_render_field_clear(struct, field) | trim }}
135+
{{ do_render_field_number(struct, field) }},
136+
{{ do_render_field_mutable(struct, field) }},
137+
{{ do_render_field_clear(struct, field) }}
137138
{% endfilter %}
138139
)
139140
{% endmacro -%}

scripts/proto_structs/templates/structs.usrv.hpp.jinja

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
{% import "forward_declarations.inc.jinja" as forward_declarations %}
1+
{% import "types_forward_declarations.inc.jinja" as types_forward_declarations %}
22
{% import "types.inc.jinja" as types with context %}
33
{% import "utils.inc.jinja" as utils %}
44
{{ utils.do_not_edit(file_name_wo_ext) }}
@@ -12,7 +12,7 @@
1212
{% endfor %}
1313

1414
{% for node in gen_nodes %}
15-
{{ forward_declarations.render_gen_node(node) }}
15+
{{ types_forward_declarations.render_gen_node(node) }}
1616
{%- endfor %}
1717

1818
{% for node in gen_nodes %}

0 commit comments

Comments
 (0)