Improve API protobuf decode method readability and reduce code size - #9455
Conversation
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## dev #9455 +/- ##
==========================================
+ Coverage 59.60% 59.63% +0.02%
==========================================
Files 50 50
Lines 10261 10261
Branches 1379 1379
==========================================
+ Hits 6116 6119 +3
+ Misses 3786 3784 -2
+ Partials 359 358 -1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
Hey there @OttoWinter, mind taking a look at this pull request as it has been labeled with an integration ( |
There was a problem hiding this comment.
Pull Request Overview
This PR refactors the Python code generator for API protocol buffer decode methods to emit single-line case statements, remove the dedent dependency, and move default/return logic outside of switch blocks for more compact and readable output.
- Removed
textwrap.dedentand replaced multi-linecasetemplates with one-line templates ending inbreak; - Streamlined
build_message_typeto appenddefaultand finalreturnoutside the switch - Reduced generated C++ file size by ~6 KB (457 lines)
Comments suppressed due to low confidence (1)
script/api_protobuf/api_protobuf.py:155
- [nitpick] These
decode_*_contentmethods all follow the same pattern of emittingcase {number}: ...; break;. Consider extracting a helper function (e.g.def _single_line_case(number, field_name, expr):) to generate this string and avoid duplicating the template logic.
def decode_varint_content(self) -> str:
|
waits for #9461 |
What does this implement/fix?
This PR refactors the code generation for API protocol buffer decode methods to produce more readable and compact code. The switch-case statements are now formatted with single-line case statements instead of multi-line blocks, making the code easier to read while maintaining the same functionality.
The changes reduce the generated
api_pb2.cppfile by approximately 6KB (457 lines), making it both more readable and more compact. (a lot less now after #9461)Types of changes
Related issue or feature (if applicable):
Pull request in esphome-docs with documentation (if applicable):
Test Environment
Example entry for
config.yaml:# No configuration changes required - this is an internal code generation improvementChecklist:
tests/folder).If user exposed functionality or configuration variables are added/changed:
- N/A - This is an internal code generation change with no user-facing impact
8000Additional Notes
Before:
After:
The compiler generates the same efficient jump table in both cases, but the new format is significantly more readable and reduces file size.