From 307f4362290e21bf2e356d114c6e8cab8cb23db7 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 5 Apr 2026 18:56:10 -1000 Subject: [PATCH] [api] Move zero-check into encode_small_varint to simplify call sites --- esphome/components/api/api_pb2.cpp | 204 +++++++--------------------- esphome/components/api/proto.h | 5 +- script/api_protobuf/api_protobuf.py | 6 +- 3 files changed, 56 insertions(+), 159 deletions(-) diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index ffe567f8c9..c4a234d243 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -74,9 +74,7 @@ uint32_t DeviceInfo::calculate_size() const { #ifdef USE_SERIAL_PROXY void SerialProxyInfo::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->name); - if (this->port_type) { - buffer.encode_small_varint(16, static_cast(this->port_type)); - } + buffer.encode_small_varint(16, static_cast(this->port_type)); } uint32_t SerialProxyInfo::calculate_size() const { uint32_t size = 0; @@ -218,9 +216,7 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(8, this->icon); #endif - if (this->entity_category) { - buffer.encode_small_varint(72, static_cast(this->entity_category)); - } + buffer.encode_small_varint(72, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(10, this->device_id); #endif @@ -274,9 +270,7 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(10, this->icon); #endif - if (this->entity_category) { - buffer.encode_small_varint(88, static_cast(this->entity_category)); - } + buffer.encode_small_varint(88, static_cast(this->entity_category)); buffer.encode_bool(12, this->supports_stop); #ifdef USE_DEVICES buffer.encode_uint32(13, this->device_id); @@ -306,9 +300,7 @@ void CoverStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(3, this->position); buffer.encode_float(4, this->tilt); - if (this->current_operation) { - buffer.encode_small_varint(40, static_cast(this->current_operation)); - } + buffer.encode_small_varint(40, static_cast(this->current_operation)); #ifdef USE_DEVICES buffer.encode_uint32(6, this->device_id); #endif @@ -375,9 +367,7 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(10, this->icon); #endif - if (this->entity_category) { - buffer.encode_small_varint(88, static_cast(this->entity_category)); - } + buffer.encode_small_varint(88, static_cast(this->entity_category)); for (const char *it : *this->supported_preset_modes) { buffer.encode_string(12, it, strlen(it), true); } @@ -413,9 +403,7 @@ void FanStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_bool(2, this->state); buffer.encode_bool(3, this->oscillating); - if (this->direction) { - buffer.encode_small_varint(40, static_cast(this->direction)); - } + buffer.encode_small_varint(40, static_cast(this->direction)); buffer.encode_int32(6, this->speed_level); buffer.encode_string(7, this->preset_mode); #ifdef USE_DEVICES @@ -513,9 +501,7 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(14, this->icon); #endif - if (this->entity_category) { - buffer.encode_small_varint(120, static_cast(this->entity_category)); - } + buffer.encode_small_varint(120, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(16, this->device_id); #endif @@ -549,9 +535,7 @@ void LightStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_bool(2, this->state); buffer.encode_float(3, this->brightness); - if (this->color_mode) { - buffer.encode_small_varint(88, static_cast(this->color_mode)); - } + buffer.encode_small_varint(88, static_cast(this->color_mode)); buffer.encode_float(10, this->color_brightness); buffer.encode_float(4, this->red); buffer.encode_float(5, this->green); @@ -706,13 +690,9 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_int32(7, this->accuracy_decimals); buffer.encode_bool(8, this->force_update); buffer.encode_string(9, this->device_class); - if (this->state_class) { - buffer.encode_small_varint(80, static_cast(this->state_class)); - } + buffer.encode_small_varint(80, static_cast(this->state_class)); buffer.encode_bool(12, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(104, static_cast(this->entity_category)); - } + buffer.encode_small_varint(104, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(14, this->device_id); #endif @@ -766,9 +746,7 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const { #endif buffer.encode_bool(6, this->assumed_state); buffer.encode_bool(7, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(64, static_cast(this->entity_category)); - } + buffer.encode_small_varint(64, static_cast(this->entity_category)); buffer.encode_string(9, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(10, this->device_id); @@ -842,9 +820,7 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -899,9 +875,7 @@ bool SubscribeLogsRequest::decode_varint(uint32_t field_id, proto_varint_value_t return true; } void SubscribeLogsResponse::encode(ProtoWriteBuffer &buffer) const { - if (this->level) { - buffer.encode_small_varint(8, static_cast(this->level)); - } + buffer.encode_small_varint(8, static_cast(this->level)); buffer.encode_bytes(3, this->message_ptr_, this->message_len_); } uint32_t SubscribeLogsResponse::calculate_size() const { @@ -1138,9 +1112,7 @@ bool GetTimeResponse::decode_32bit(uint32_t field_id, Proto32Bit value) { #ifdef USE_API_USER_DEFINED_ACTIONS void ListEntitiesServicesArgument::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->name); - if (this->type) { - buffer.encode_small_varint(16, static_cast(this->type)); - } + buffer.encode_small_varint(16, static_cast(this->type)); } uint32_t ListEntitiesServicesArgument::calculate_size() const { uint32_t size = 0; @@ -1154,9 +1126,7 @@ void ListEntitiesServicesResponse::encode(ProtoWriteBuffer &buffer) const { for (auto &it : this->args) { buffer.encode_sub_message(3, it); } - if (this->supports_response) { - buffer.encode_small_varint(32, static_cast(this->supports_response)); - } + buffer.encode_small_varint(32, static_cast(this->supports_response)); } uint32_t ListEntitiesServicesResponse::calculate_size() const { uint32_t size = 0; @@ -1303,9 +1273,7 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { #ifdef USE_ENTITY_ICON buffer.encode_string(6, this->icon); #endif - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -1452,26 +1420,16 @@ uint32_t ListEntitiesClimateResponse::calculate_size() const { } void ClimateStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - if (this->mode) { - buffer.encode_small_varint(16, static_cast(this->mode)); - } + buffer.encode_small_varint(16, static_cast(this->mode)); buffer.encode_float(3, this->current_temperature); buffer.encode_float(4, this->target_temperature); buffer.encode_float(5, this->target_temperature_low); buffer.encode_float(6, this->target_temperature_high); - if (this->action) { - buffer.encode_small_varint(64, static_cast(this->action)); - } - if (this->fan_mode) { - buffer.encode_small_varint(72, static_cast(this->fan_mode)); - } - if (this->swing_mode) { - buffer.encode_small_varint(80, static_cast(this->swing_mode)); - } + buffer.encode_small_varint(64, static_cast(this->action)); + buffer.encode_small_varint(72, static_cast(this->fan_mode)); + buffer.encode_small_varint(80, static_cast(this->swing_mode)); buffer.encode_string(11, this->custom_fan_mode); - if (this->preset) { - buffer.encode_small_varint(96, static_cast(this->preset)); - } + buffer.encode_small_varint(96, static_cast(this->preset)); buffer.encode_string(13, this->custom_preset); buffer.encode_float(14, this->current_humidity); buffer.encode_float(15, this->target_humidity); @@ -1601,9 +1559,7 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(48, static_cast(this->entity_category)); - } + buffer.encode_small_varint(48, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -1641,9 +1597,7 @@ void WaterHeaterStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(2, this->current_temperature); buffer.encode_float(3, this->target_temperature); - if (this->mode) { - buffer.encode_small_varint(32, static_cast(this->mode)); - } + buffer.encode_small_varint(32, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(5, this->device_id); #endif @@ -1718,13 +1672,9 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_float(7, this->max_value); buffer.encode_float(8, this->step); buffer.encode_bool(9, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(80, static_cast(this->entity_category)); - } + buffer.encode_small_varint(80, static_cast(this->entity_category)); buffer.encode_string(11, this->unit_of_measurement); - if (this->mode) { - buffer.encode_small_varint(96, static_cast(this->mode)); - } + buffer.encode_small_varint(96, static_cast(this->mode)); buffer.encode_string(13, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(14, this->device_id); @@ -1807,9 +1757,7 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(6, it, strlen(it), true); } buffer.encode_bool(7, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(64, static_cast(this->entity_category)); - } + buffer.encode_small_varint(64, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); #endif @@ -1900,9 +1848,7 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { } buffer.encode_bool(8, this->supports_duration); buffer.encode_bool(9, this->supports_volume); - if (this->entity_category) { - buffer.encode_small_varint(80, static_cast(this->entity_category)); - } + buffer.encode_small_varint(80, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(11, this->device_id); #endif @@ -2009,9 +1955,7 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_bool(8, this->assumed_state); buffer.encode_bool(9, this->supports_open); buffer.encode_bool(10, this->requires_code); @@ -2041,9 +1985,7 @@ uint32_t ListEntitiesLockResponse::calculate_size() const { } void LockStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - if (this->state) { - buffer.encode_small_varint(16, static_cast(this->state)); - } + buffer.encode_small_varint(16, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -2106,9 +2048,7 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -2158,9 +2098,7 @@ void MediaPlayerSupportedFormat::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(1, this->format); buffer.encode_uint32(2, this->sample_rate); buffer.encode_uint32(3, this->num_channels); - if (this->purpose) { - buffer.encode_small_varint(32, static_cast(this->purpose)); - } + buffer.encode_small_varint(32, static_cast(this->purpose)); buffer.encode_uint32(5, this->sample_bytes); } uint32_t MediaPlayerSupportedFormat::calculate_size() const { @@ -2180,9 +2118,7 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_bool(8, this->supports_pause); for (auto &it : this->supported_formats) { buffer.encode_sub_message(9, it); @@ -2216,9 +2152,7 @@ uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const { } void MediaPlayerStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - if (this->state) { - buffer.encode_small_varint(16, static_cast(this->state)); - } + buffer.encode_small_varint(16, static_cast(this->state)); buffer.encode_float(3, this->volume); buffer.encode_bool(4, this->muted); #ifdef USE_DEVICES @@ -2679,15 +2613,9 @@ uint32_t BluetoothDeviceClearCacheResponse::calculate_size() const { return size; } void BluetoothScannerStateResponse::encode(ProtoWriteBuffer &buffer) const { - if (this->state) { - buffer.encode_small_varint(8, static_cast(this->state)); - } - if (this->mode) { - buffer.encode_small_varint(16, static_cast(this->mode)); - } - if (this->configured_mode) { - buffer.encode_small_varint(24, static_cast(this->configured_mode)); - } + buffer.encode_small_varint(8, static_cast(this->state)); + buffer.encode_small_varint(16, static_cast(this->mode)); + buffer.encode_small_varint(24, static_cast(this->configured_mode)); } uint32_t BluetoothScannerStateResponse::calculate_size() const { uint32_t size = 0; @@ -3012,9 +2940,7 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_uint32(8, this->supported_features); buffer.encode_bool(9, this->requires_code); buffer.encode_bool(10, this->requires_code_to_arm); @@ -3042,9 +2968,7 @@ uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const { } void AlarmControlPanelStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); - if (this->state) { - buffer.encode_small_varint(16, static_cast(this->state)); - } + buffer.encode_small_varint(16, static_cast(this->state)); #ifdef USE_DEVICES buffer.encode_uint32(3, this->device_id); #endif @@ -3104,15 +3028,11 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_uint32(8, this->min_length); buffer.encode_uint32(9, this->max_length); buffer.encode_string(10, this->pattern); - if (this->mode) { - buffer.encode_small_varint(88, static_cast(this->mode)); - } + buffer.encode_small_varint(88, static_cast(this->mode)); #ifdef USE_DEVICES buffer.encode_uint32(12, this->device_id); #endif @@ -3197,9 +3117,7 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3282,9 +3200,7 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3367,9 +3283,7 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); for (const char *it : *this->event_types) { buffer.encode_string(9, it, strlen(it), true); @@ -3425,9 +3339,7 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); buffer.encode_bool(9, this->assumed_state); buffer.encode_bool(10, this->supports_position); @@ -3458,9 +3370,7 @@ uint32_t ListEntitiesValveResponse::calculate_size() const { void ValveStateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.write_tag_and_fixed32(13, this->key); buffer.encode_float(2, this->position); - if (this->current_operation) { - buffer.encode_small_varint(24, static_cast(this->current_operation)); - } + buffer.encode_small_varint(24, static_cast(this->current_operation)); #ifdef USE_DEVICES buffer.encode_uint32(4, this->device_id); #endif @@ -3516,9 +3426,7 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(8, this->device_id); #endif @@ -3591,9 +3499,7 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(5, this->icon); #endif buffer.encode_bool(6, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(56, static_cast(this->entity_category)); - } + buffer.encode_small_varint(56, static_cast(this->entity_category)); buffer.encode_string(8, this->device_class); #ifdef USE_DEVICES buffer.encode_uint32(9, this->device_id); @@ -3715,9 +3621,7 @@ bool ZWaveProxyRequest::decode_length(uint32_t field_id, ProtoLengthDelimited va return true; } void ZWaveProxyRequest::encode(ProtoWriteBuffer &buffer) const { - if (this->type) { - buffer.encode_small_varint(8, static_cast(this->type)); - } + buffer.encode_small_varint(8, static_cast(this->type)); buffer.encode_bytes(2, this->data, this->data_len); } uint32_t ZWaveProxyRequest::calculate_size() const { @@ -3736,9 +3640,7 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_string(4, this->icon); #endif buffer.encode_bool(5, this->disabled_by_default); - if (this->entity_category) { - buffer.encode_small_varint(48, static_cast(this->entity_category)); - } + buffer.encode_small_varint(48, static_cast(this->entity_category)); #ifdef USE_DEVICES buffer.encode_uint32(7, this->device_id); #endif @@ -3934,12 +3836,8 @@ bool SerialProxyRequest::decode_varint(uint32_t field_id, proto_varint_value_t v } void SerialProxyRequestResponse::encode(ProtoWriteBuffer &buffer) const { buffer.encode_uint32(1, this->instance); - if (this->type) { - buffer.encode_small_varint(16, static_cast(this->type)); - } - if (this->status) { - buffer.encode_small_varint(24, static_cast(this->status)); - } + buffer.encode_small_varint(16, static_cast(this->type)); + buffer.encode_small_varint(24, static_cast(this->status)); buffer.encode_string(4, this->error_message); } uint32_t SerialProxyRequestResponse::calculate_size() const { diff --git a/esphome/components/api/proto.h b/esphome/components/api/proto.h index e3cecfcde6..a99cba18b2 100644 --- a/esphome/components/api/proto.h +++ b/esphome/components/api/proto.h @@ -277,8 +277,11 @@ class ProtoWriteBuffer { this->encode_string(field_id, reinterpret_cast(data), len, force); } /** Encode a small varint field where both tag and value fit in one byte each. - * The tag is precomputed by the code generator. value must be < 128. */ + * The tag is precomputed by the code generator. value must be < 128. + * Skips encoding when value is zero (default protobuf behavior). */ void encode_small_varint(uint8_t precomputed_tag, uint8_t value) { + if (value == 0) + return; this->debug_check_bounds_(2); auto *p = this->pos_; p[0] = precomputed_tag; diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 1f569baeef..4ea09f0687 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -1345,11 +1345,7 @@ class EnumType(TypeInfo): if max_val is not None and max_val < 128 and not self.force: tag = self.calculate_tag() if tag < 128: - return ( - f"if (this->{self.field_name}) {{\n" - f" buffer.encode_small_varint({tag}, static_cast(this->{self.field_name}));\n" - f"}}" - ) + return f"buffer.encode_small_varint({tag}, static_cast(this->{self.field_name}));" if self.force: return f"buffer.{self.encode_func}({self.number}, {value_expr}, true);" return f"buffer.{self.encode_func}({self.number}, {value_expr});"