Skip to content
This repository has been archived by the owner on Feb 27, 2024. It is now read-only.

Make serde of google protobufs idempotent #57

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 5 additions & 5 deletions MODULE.bazel.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions src/proto_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,9 @@ def construct(
@staticmethod
def parse_syntax_and_preceding_comments(
proto_content: str,
) -> tuple[ProtoSyntax, Sequence[ProtoComment], str]:
) -> tuple[ProtoSyntax, Sequence[ProtoNode], str]:
# First, parse any preceding comments.
parsed_tree = []
parsed_tree: list[ProtoNode] = []
while True:
for node_type in [ProtoSingleLineComment, ProtoMultiLineComment]:
try:
Expand All @@ -161,6 +161,7 @@ def parse_syntax_and_preceding_comments(
raise ValueError(f"Proto doesn't have parseable syntax:\n{proto_content}")
syntax = syntax_match.node
proto_content = syntax_match.remaining_source.strip()
parsed_tree.append(syntax)

return syntax, parsed_tree, proto_content

Expand All @@ -172,7 +173,7 @@ def normalize(self) -> Optional["ProtoNode"]:
)

def serialize(self) -> str:
serialized_parts = [self.syntax.serialize()]
serialized_parts = []
previous_type: type[ProtoNode] = self.syntax.__class__
for node in self.nodes:
# Attempt to group up lines of the same type.
Expand Down
2 changes: 1 addition & 1 deletion src/proto_syntax.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def __init__(self, syntax: ProtoStringLiteral, *args, **kwargs):
self.syntax = syntax

def __eq__(self, other) -> bool:
return self.syntax == other.syntax
return isinstance(other, ProtoSyntax) and self.syntax == other.syntax

def __str__(self) -> str:
return f"<ProtoSyntax syntax={self.syntax.serialize()}>"
Expand Down
13 changes: 9 additions & 4 deletions test/util/parser_binary_test.sh
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
#!/usr/bin/env bash
set -euo pipefail

function compare_proto() {
# Takes one argument, the relative path to the proto to compare.
echo $1
./src/util/parser_binary $1 > "${1}_serialized.proto"
diff --side-by-side --ignore-all-space --ignore-blank-lines $1 "${1}_serialized.proto"
}

echo "Local protos:"
LOCAL_PROTOS=$(find ./test/resources -name "*.proto" | sort)
for f in $LOCAL_PROTOS; do
echo $f
./src/util/parser_binary $f > /dev/null
compare_proto $f
done

echo "Google protos:"
GOOGLE_PROTOS=$(find ./external/com_google_protobuf/src/google/protobuf -name "*.proto" | xargs grep --files-without-match "proto2" | sort)
for f in $GOOGLE_PROTOS; do
echo $f
./src/util/parser_binary $f > /dev/null
compare_proto $f
done
Loading