[api] Don't use encode_small_varint for forced fields (must encode zero)

This commit is contained in:
J. Nick Koston
2026-04-05 18:57:04 -10:00
parent 307f436229
commit d5b5a6a4a7
+3 -9
View File
@@ -244,10 +244,10 @@ class TypeInfo(ABC):
"encode_bool": "buffer.write_raw_byte({value} ? 0x01 : 0x00);",
}
# When max_value < 128, the varint is always 1 byte — use encode_small_varint
# 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.encode_small_varint({tag}, static_cast<uint8_t>({value}));",
"encode_uint64": "buffer.encode_small_varint({tag}, static_cast<uint8_t>({value}));",
"encode_uint32": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
"encode_uint64": "buffer.write_raw_byte(static_cast<uint8_t>({value}));",
}
def _encode_with_precomputed_tag(self, value_expr: str) -> str | None:
@@ -270,12 +270,6 @@ class TypeInfo(ABC):
raw_expr = self.RAW_ENCODE_MAP.get(self.encode_func)
if raw_expr is None:
return None
if (
max_val is not None
and max_val < 128
and self.encode_func in self.RAW_ENCODE_SMALL_MAP
):
return raw_expr.format(tag=tag, value=value_expr)
return f"buffer.write_raw_byte({tag});\n{raw_expr.format(value=value_expr)}"
def _encode_bytes_with_precomputed_tag(