mirror of
https://github.com/esphome/esphome.git
synced 2026-09-03 19:46:02 +00:00
Merge remote-tracking branch 'upstream/api-enum-auto-max-value' into integration
This commit is contained in:
@@ -249,7 +249,7 @@ class TypeInfo(ABC):
|
||||
"encode_bool": "buffer.write_raw_byte({value} ? 0x01 : 0x00);",
|
||||
}
|
||||
|
||||
# When max_value < 128, the varint is always 1 byte — use a direct byte write
|
||||
# When max_value < 128 and forced, the varint is always 1 byte — write tag + value directly
|
||||
RAW_ENCODE_SMALL_MAP: dict[str, str] = {
|
||||
"encode_uint32": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
|
||||
"encode_uint64": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
|
||||
@@ -260,7 +260,7 @@ class TypeInfo(ABC):
|
||||
|
||||
Returns the raw encode string if the tag is a single byte and the
|
||||
encode_func has a known raw equivalent, or None otherwise.
|
||||
When max_value < 128, uses direct byte write instead of varint encoding.
|
||||
When max_value < 128, uses encode_small_varint instead of varint encoding.
|
||||
"""
|
||||
if not self.force:
|
||||
return None
|
||||
@@ -1356,13 +1356,19 @@ class EnumType(TypeInfo):
|
||||
|
||||
@property
|
||||
def encode_content(self) -> str:
|
||||
if result := self._encode_with_precomputed_tag(
|
||||
f"static_cast<uint32_t>(this->{self.field_name})"
|
||||
):
|
||||
value_expr = f"static_cast<uint32_t>(this->{self.field_name})"
|
||||
if result := self._encode_with_precomputed_tag(value_expr):
|
||||
return result
|
||||
# For non-forced enum fields with max < 128 and single-byte tag,
|
||||
# emit a zero-check + two raw byte writes instead of encode_uint32
|
||||
max_val = self.max_value
|
||||
if max_val is not None and max_val < 128 and not self.force:
|
||||
tag = self.calculate_tag()
|
||||
if tag < 128:
|
||||
return f"buffer.encode_small_varint({tag}, static_cast<uint8_t>(this->{self.field_name}));"
|
||||
if self.force:
|
||||
return f"buffer.{self.encode_func}({self.number}, static_cast<uint32_t>(this->{self.field_name}), true);"
|
||||
return f"buffer.{self.encode_func}({self.number}, static_cast<uint32_t>(this->{self.field_name}));"
|
||||
return f"buffer.{self.encode_func}({self.number}, {value_expr}, true);"
|
||||
return f"buffer.{self.encode_func}({self.number}, {value_expr});"
|
||||
|
||||
def dump(self, name: str) -> str:
|
||||
return f"out.append_p(proto_enum_to_string<{self.cpp_type}>({name}));"
|
||||
|
||||
Reference in New Issue
Block a user