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

Minor codegen cleanups #162

Merged
merged 3 commits into from
Feb 12, 2024
Merged
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
58 changes: 19 additions & 39 deletions pb-jelly-gen/codegen/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -737,7 +737,6 @@ def __init__(
self.indentation = 0
self.content = StringIO()
self.is_proto3 = proto_file and proto_file.syntax == "proto3"
self.uses_sso = False
if proto_file and proto_file.options.Extensions[extensions_pb2.serde_derive]:
self.derive_serde = True
else:
Expand Down Expand Up @@ -787,13 +786,7 @@ def write_comments(self, sci_loc: Optional[SourceCodeInfo.Location]) -> None:
def rust_type(
self, msg_type: DescriptorProto, field: FieldDescriptorProto
) -> RustType:
rust_type = RustType(self.ctx, self.proto_file, msg_type, field)

# checks if any of our types use a small string optimization
if not self.uses_sso and rust_type.is_small_string_optimization():
self.uses_sso = True

return rust_type
return RustType(self.ctx, self.proto_file, msg_type, field)

def gen_closed_enum(
self,
Expand Down Expand Up @@ -1799,6 +1792,12 @@ def calc_impls(
(impls_eq, impls_copy) = (False, False)
self.extra_crates[crate].add("bytes")
may_use_grpc_slices = True
elif (
typ == FieldDescriptorProto.TYPE_STRING
and field.options.Extensions[extensions_pb2.sso]
):
impls_copy = False
self.extra_crates[crate].add("compact_str")
elif typ in PRIMITIVE_TYPES:
if not PRIMITIVE_TYPES[typ][1]:
impls_eq = False
Expand Down Expand Up @@ -1944,9 +1943,7 @@ def mod_tree_dfs(
content = RS_HEADER + LIB_RS_HEADER + librs.content.getvalue()
yield filename, content

def get_spec_toml_file(
self, derive_serde: bool, include_sso: bool
) -> Iterator[Tuple[Text, Text]]:
def get_spec_toml_file(self, derive_serde: bool) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
{"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate]
Expand All @@ -1955,13 +1952,9 @@ def get_spec_toml_file(
"serde": 'features=["serde_derive"]',
"compact_str": 'features=["bytes"]',
}

if derive_serde:
all_deps.update({"serde"})
if include_sso:
all_deps.update({"compact_str"})
if derive_serde:
features.update({"compact_str": 'features=["bytes", "serde"]'})
features.update({"compact_str": 'features=["bytes", "serde"]'})

deps_str = "\n".join(
"{dep} = {{{feat}}}".format(dep=dep, feat=features.get(dep, ""))
Expand All @@ -1970,9 +1963,7 @@ def get_spec_toml_file(
targets = SPEC_TOML_TEMPLATE.format(crate=crate, deps=deps_str)
yield crate, targets

def get_cargo_toml_file(
self, derive_serde: bool, include_sso: bool
) -> Iterator[Tuple[Text, Text]]:
def get_cargo_toml_file(self, derive_serde: bool) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
{"lazy_static", "pb-jelly"} | deps | self.extra_crates[crate]
Expand All @@ -1981,20 +1972,16 @@ def get_cargo_toml_file(
"serde": 'features=["serde_derive"]',
"compact_str": 'features=["bytes"]',
}

if derive_serde:
all_deps.update({"serde"})
if include_sso:
all_deps.update({"compact_str"})
if derive_serde:
features.update({"compact_str": 'features=["bytes", "serde"]'})
features.update({"compact_str": 'features=["bytes", "serde"]'})

versions = {
"lazy_static": ' version = "1.4.0" ',
"pb-jelly": ' version = "0.0.16" ',
"serde": ' version = "1.0" ',
"bytes": ' version = "1.0" ',
"compact_str": ' version = "0.5" ',
"lazy_static": 'version = "1.4.0"',
"pb-jelly": 'version = "0.0.16"',
"serde": 'version = "1.0"',
"bytes": 'version = "1.0"',
"compact_str": 'version = "0.5"',
}

deps_lines = []
Expand All @@ -2007,7 +1994,7 @@ def get_cargo_toml_file(
else:
fields.append('path = "../{dep}"'.format(dep=dep))
deps_lines.append(
"{dep} = {{{fields}}}".format(dep=dep, fields=",".join(fields))
"{dep} = {{ {fields} }}".format(dep=dep, fields=", ".join(fields))
)

targets = CARGO_TOML_TEMPLATE.format(
Expand Down Expand Up @@ -2098,7 +2085,6 @@ def new_mod_tree() -> ModTree:
# Set iteration order is nondeterministic, but this is ok, because we never iterate through this
processed_files: Set[Text] = set()
derive_serde = False
include_sso = False

for proto_file_name in file_to_generate:
# Detect packages which collide with filenames. The rust codegen does not support those due
Expand Down Expand Up @@ -2150,10 +2136,6 @@ def add_mod(writer: CodeWriter) -> None:

add_mod(writer=writer)

# check if the writer ever used a small string optimization
if writer.uses_sso:
include_sso = True

# Note that output filenames must use "/" even on windows. It is part of the
# protoc plugin protocol. The plugin speaks os-independent in "/". Thus, we
# should not use os.path.sep or os.path.join
Expand All @@ -2170,15 +2152,13 @@ def add_mod(writer: CodeWriter) -> None:
output.name = file_prefix + crate + "/BUILD.in-gen-proto~"
output.content = ""
elif "generate_spec_toml" in request.parameter:
for crate, spec_toml_file in ctx.get_spec_toml_file(derive_serde, include_sso):
for crate, spec_toml_file in ctx.get_spec_toml_file(derive_serde):
output = response.file.add()
output.name = file_prefix + crate + "/Spec.toml"
output.content = spec_toml_file
else:
# Generate good ol Cargo.toml files
for crate, cargo_toml_file in ctx.get_cargo_toml_file(
derive_serde, include_sso
):
for crate, cargo_toml_file in ctx.get_cargo_toml_file(derive_serde):
output = response.file.add()
output.name = file_prefix + crate + "/Cargo.toml"
output.content = cargo_toml_file
Expand Down
1 change: 0 additions & 1 deletion pb-test/gen/pb-jelly/proto_google/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ version = "0.0.1"
edition = "2018"

[dependencies]
compact_str = {features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.16" }
1 change: 0 additions & 1 deletion pb-test/gen/pb-jelly/proto_nopackage/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ version = "0.0.1"
edition = "2018"

[dependencies]
compact_str = {features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.16" }
4 changes: 2 additions & 2 deletions pb-test/gen/pb-jelly/proto_pbtest/Cargo.toml.expected
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ edition = "2018"

[dependencies]
bytes = { version = "1.0" }
compact_str = {features=["bytes"], version = "0.5" }
compact_str = { features=["bytes"], version = "0.5" }
lazy_static = { version = "1.4.0" }
pb-jelly = { version = "0.0.16" }
proto_google = {path = "../proto_google"}
proto_google = { path = "../proto_google" }
Loading