Skip to content

Commit

Permalink
Cleanup uses_sso
Browse files Browse the repository at this point in the history
  • Loading branch information
goffrie committed Feb 12, 2024
1 parent 3cb6a26 commit 250a8fe
Showing 1 changed file with 13 additions and 29 deletions.
42 changes: 13 additions & 29 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 @@ -1945,7 +1944,7 @@ def mod_tree_dfs(
yield filename, content

def get_spec_toml_file(
self, derive_serde: bool, include_sso: bool
self, derive_serde: bool
) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
Expand All @@ -1955,13 +1954,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 @@ -1971,7 +1966,7 @@ def get_spec_toml_file(
yield crate, targets

def get_cargo_toml_file(
self, derive_serde: bool, include_sso: bool
self, derive_serde: bool
) -> Iterator[Tuple[Text, Text]]:
for crate, deps in self.deps_map.items():
all_deps = (
Expand All @@ -1981,13 +1976,9 @@ 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" ',
Expand Down Expand Up @@ -2098,7 +2089,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 +2140,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 +2156,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

0 comments on commit 250a8fe

Please sign in to comment.