diff --git a/esphome/components/api/api.proto b/esphome/components/api/api.proto index 1e03675999..9b62f8e758 100644 --- a/esphome/components/api/api.proto +++ b/esphome/components/api/api.proto @@ -315,9 +315,9 @@ message ListEntitiesBinarySensorResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_BINARY_SENSOR"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string device_class = 5; @@ -349,9 +349,9 @@ message ListEntitiesCoverResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_COVER"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id bool assumed_state = 5; @@ -433,9 +433,9 @@ message ListEntitiesFanResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_FAN"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id bool supports_oscillation = 5; @@ -521,9 +521,9 @@ message ListEntitiesLightResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_LIGHT"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id repeated ColorMode supported_color_modes = 12 [(container_pointer_no_template) = "light::ColorModeMask"]; @@ -626,9 +626,9 @@ message ListEntitiesSensorResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_SENSOR"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -666,9 +666,9 @@ message ListEntitiesSwitchResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_SWITCH"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -708,9 +708,9 @@ message ListEntitiesTextSensorResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_TEXT_SENSOR"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -971,9 +971,9 @@ message ListEntitiesCameraResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_CAMERA"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id bool disabled_by_default = 5; string icon = 6 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1056,9 +1056,9 @@ message ListEntitiesClimateResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_CLIMATE"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id bool supports_current_temperature = 5; // Deprecated: use feature_flags @@ -1167,9 +1167,9 @@ message ListEntitiesWaterHeaterResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_WATER_HEATER"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; string icon = 4 [(field_ifdef) = "USE_ENTITY_ICON"]; bool disabled_by_default = 5; EntityCategory entity_category = 6; @@ -1243,9 +1243,9 @@ message ListEntitiesNumberResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_NUMBER"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1292,9 +1292,9 @@ message ListEntitiesSelectResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_SELECT"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1336,9 +1336,9 @@ message ListEntitiesSirenResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_SIREN"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1399,9 +1399,9 @@ message ListEntitiesLockResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_LOCK"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1448,9 +1448,9 @@ message ListEntitiesButtonResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_BUTTON"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -1515,9 +1515,9 @@ message ListEntitiesMediaPlayerResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_MEDIA_PLAYER"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2103,9 +2103,9 @@ message ListEntitiesAlarmControlPanelResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_ALARM_CONTROL_PANEL"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; bool disabled_by_default = 6; @@ -2150,9 +2150,9 @@ message ListEntitiesTextResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_TEXT"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; bool disabled_by_default = 6; @@ -2198,9 +2198,9 @@ message ListEntitiesDateResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_DATETIME_DATE"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2245,9 +2245,9 @@ message ListEntitiesTimeResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_DATETIME_TIME"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2292,9 +2292,9 @@ message ListEntitiesEventResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_EVENT"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2323,9 +2323,9 @@ message ListEntitiesValveResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_VALVE"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2378,9 +2378,9 @@ message ListEntitiesDateTimeResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_DATETIME_DATETIME"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2421,9 +2421,9 @@ message ListEntitiesUpdateResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_UPDATE"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; reserved 4; // Deprecated: was string unique_id string icon = 5 [(field_ifdef) = "USE_ENTITY_ICON"]; @@ -2504,9 +2504,9 @@ message ListEntitiesInfraredResponse { option (source) = SOURCE_SERVER; option (ifdef) = "USE_INFRARED"; - string object_id = 1; + string object_id = 1 [(max_data_length) = 120, (force) = true]; fixed32 key = 2 [(force) = true]; - string name = 3; + string name = 3 [(max_data_length) = 120, (force) = true]; string icon = 4 [(field_ifdef) = "USE_ENTITY_ICON"]; bool disabled_by_default = 5; EntityCategory entity_category = 6; diff --git a/esphome/components/api/api_options.proto b/esphome/components/api/api_options.proto index 0aa9e814cf..0f71268d70 100644 --- a/esphome/components/api/api_options.proto +++ b/esphome/components/api/api_options.proto @@ -102,4 +102,10 @@ extend google.protobuf.FieldOptions { // and direct byte writes instead of varint branching, since the encoded varint // is guaranteed to be 1 byte. optional uint32 max_value = 50017; + + // max_data_length: Maximum length of a string or bytes field. + // When max_data_length < 128, the code generator emits constant-size + // length varint calculations and direct byte writes, since the length + // varint is guaranteed to be 1 byte. + optional uint32 max_data_length = 50018; } diff --git a/esphome/components/api/api_pb2.cpp b/esphome/components/api/api_pb2.cpp index f25d269e8f..00d700116f 100644 --- a/esphome/components/api/api_pb2.cpp +++ b/esphome/components/api/api_pb2.cpp @@ -207,9 +207,13 @@ uint32_t DeviceInfoResponse::calculate_size() const { } #ifdef USE_BINARY_SENSOR void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); buffer.encode_string(5, this->device_class); buffer.encode_bool(6, this->is_status_binary_sensor); buffer.encode_bool(7, this->disabled_by_default); @@ -223,9 +227,9 @@ void ListEntitiesBinarySensorResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesBinarySensorResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); size += ProtoSize::calc_length(1, this->device_class.size()); size += ProtoSize::calc_bool(1, this->is_status_binary_sensor); size += ProtoSize::calc_bool(1, this->disabled_by_default); @@ -259,9 +263,13 @@ uint32_t BinarySensorStateResponse::calculate_size() const { #endif #ifdef USE_COVER void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); buffer.encode_bool(5, this->assumed_state); buffer.encode_bool(6, this->supports_position); buffer.encode_bool(7, this->supports_tilt); @@ -278,9 +286,9 @@ void ListEntitiesCoverResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesCoverResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); size += ProtoSize::calc_bool(1, this->assumed_state); size += ProtoSize::calc_bool(1, this->supports_position); size += ProtoSize::calc_bool(1, this->supports_tilt); @@ -356,9 +364,13 @@ bool CoverCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_FAN void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); buffer.encode_bool(5, this->supports_oscillation); buffer.encode_bool(6, this->supports_speed); buffer.encode_bool(7, this->supports_direction); @@ -377,9 +389,9 @@ void ListEntitiesFanResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesFanResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); size += ProtoSize::calc_bool(1, this->supports_oscillation); size += ProtoSize::calc_bool(1, this->supports_speed); size += ProtoSize::calc_bool(1, this->supports_direction); @@ -486,9 +498,13 @@ bool FanCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_LIGHT void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); for (const auto &it : *this->supported_color_modes) { buffer.encode_uint32(12, static_cast(it), true); } @@ -508,9 +524,9 @@ void ListEntitiesLightResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesLightResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); if (!this->supported_color_modes->empty()) { for (const auto &it : *this->supported_color_modes) { size += ProtoSize::calc_uint32_force(1, static_cast(it)); @@ -682,9 +698,13 @@ bool LightCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_SENSOR void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -701,9 +721,9 @@ void ListEntitiesSensorResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesSensorResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -740,9 +760,13 @@ uint32_t SensorStateResponse::calculate_size() const { #endif #ifdef USE_SWITCH void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -756,9 +780,9 @@ void ListEntitiesSwitchResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesSwitchResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -815,9 +839,13 @@ bool SwitchCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_TEXT_SENSOR void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -830,9 +858,9 @@ void ListEntitiesTextSensorResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesTextSensorResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -1268,9 +1296,13 @@ uint32_t ExecuteServiceResponse::calculate_size() const { #endif #ifdef USE_CAMERA void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); buffer.encode_bool(5, this->disabled_by_default); #ifdef USE_ENTITY_ICON buffer.encode_string(6, this->icon); @@ -1282,9 +1314,9 @@ void ListEntitiesCameraResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesCameraResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); size += ProtoSize::calc_bool(1, this->disabled_by_default); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); @@ -1329,9 +1361,13 @@ bool CameraImageRequest::decode_varint(uint32_t field_id, proto_varint_value_t v #endif #ifdef USE_CLIMATE void ListEntitiesClimateResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); buffer.encode_bool(5, this->supports_current_temperature); buffer.encode_bool(6, this->supports_two_point_target_temperature); for (const auto &it : *this->supported_modes) { @@ -1373,9 +1409,9 @@ void ListEntitiesClimateResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesClimateResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); size += ProtoSize::calc_bool(1, this->supports_current_temperature); size += ProtoSize::calc_bool(1, this->supports_two_point_target_temperature); if (!this->supported_modes->empty()) { @@ -1562,9 +1598,13 @@ bool ClimateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_WATER_HEATER void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(4, this->icon); #endif @@ -1583,9 +1623,9 @@ void ListEntitiesWaterHeaterResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesWaterHeaterResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -1674,9 +1714,13 @@ bool WaterHeaterCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value #endif #ifdef USE_NUMBER void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -1694,9 +1738,9 @@ void ListEntitiesNumberResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesNumberResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -1759,9 +1803,13 @@ bool NumberCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_SELECT void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -1776,9 +1824,9 @@ void ListEntitiesSelectResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesSelectResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -1848,9 +1896,13 @@ bool SelectCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_SIREN void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -1867,9 +1919,9 @@ void ListEntitiesSirenResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesSirenResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -1960,9 +2012,13 @@ bool SirenCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_LOCK void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -1978,9 +2034,9 @@ void ListEntitiesLockResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesLockResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -2053,9 +2109,13 @@ bool LockCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_BUTTON void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -2068,9 +2128,9 @@ void ListEntitiesButtonResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesButtonResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -2123,9 +2183,13 @@ uint32_t MediaPlayerSupportedFormat::calculate_size() const { return size; } void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -2142,9 +2206,9 @@ void ListEntitiesMediaPlayerResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesMediaPlayerResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -2945,9 +3009,13 @@ bool VoiceAssistantSetConfiguration::decode_length(uint32_t field_id, ProtoLengt #endif #ifdef USE_ALARM_CONTROL_PANEL void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -2962,9 +3030,9 @@ void ListEntitiesAlarmControlPanelResponse::encode(ProtoWriteBuffer &buffer) con } uint32_t ListEntitiesAlarmControlPanelResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3033,9 +3101,13 @@ bool AlarmControlPanelCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit #endif #ifdef USE_TEXT void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3051,9 +3123,9 @@ void ListEntitiesTextResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesTextResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3122,9 +3194,13 @@ bool TextCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_DATETIME_DATE void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3136,9 +3212,9 @@ void ListEntitiesDateResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesDateResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3205,9 +3281,13 @@ bool DateCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_DATETIME_TIME void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3219,9 +3299,9 @@ void ListEntitiesTimeResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesTimeResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3288,9 +3368,13 @@ bool TimeCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_EVENT void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3306,9 +3390,9 @@ void ListEntitiesEventResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesEventResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3344,9 +3428,13 @@ uint32_t EventResponse::calculate_size() const { #endif #ifdef USE_VALVE void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3362,9 +3450,9 @@ void ListEntitiesValveResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesValveResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3431,9 +3519,13 @@ bool ValveCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_DATETIME_DATETIME void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3445,9 +3537,9 @@ void ListEntitiesDateTimeResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesDateTimeResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3504,9 +3596,13 @@ bool DateTimeCommandRequest::decode_32bit(uint32_t field_id, Proto32Bit value) { #endif #ifdef USE_UPDATE void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(5, this->icon); #endif @@ -3519,9 +3615,9 @@ void ListEntitiesUpdateResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesUpdateResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif @@ -3645,9 +3741,13 @@ uint32_t ZWaveProxyRequest::calculate_size() const { #endif #ifdef USE_INFRARED void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { - buffer.encode_string(1, this->object_id); + buffer.write_raw_byte(10); + buffer.write_raw_byte(static_cast(this->object_id.size())); + buffer.encode_raw(this->object_id.c_str(), this->object_id.size()); buffer.write_tag_and_fixed32(21, this->key); - buffer.encode_string(3, this->name); + buffer.write_raw_byte(26); + buffer.write_raw_byte(static_cast(this->name.size())); + buffer.encode_raw(this->name.c_str(), this->name.size()); #ifdef USE_ENTITY_ICON buffer.encode_string(4, this->icon); #endif @@ -3661,9 +3761,9 @@ void ListEntitiesInfraredResponse::encode(ProtoWriteBuffer &buffer) const { } uint32_t ListEntitiesInfraredResponse::calculate_size() const { uint32_t size = 0; - size += ProtoSize::calc_length(1, this->object_id.size()); + size += 2 + this->object_id.size(); size += 5; - size += ProtoSize::calc_length(1, this->name.size()); + size += 2 + this->name.size(); #ifdef USE_ENTITY_ICON size += ProtoSize::calc_length(1, this->icon.size()); #endif diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index c17f16412c..a0335ff404 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -161,6 +161,11 @@ class TypeInfo(ABC): """Get the max_value option for this field, or None if not set.""" return get_field_opt(self._field, pb.max_value, None) + @property + def max_data_length(self) -> int | None: + """Get the max_data_length option for this field, or None if not set.""" + return get_field_opt(self._field, pb.max_data_length, None) + @property def wire_type(self) -> WireType: """Get the wire type for the field.""" @@ -1064,7 +1069,9 @@ class PointerToStringBufferType(PointerToBufferTypeBase): @property def encode_content(self) -> str: if result := self._encode_bytes_with_precomputed_tag( - f"this->{self.field_name}.c_str()", f"this->{self.field_name}.size()" + f"this->{self.field_name}.c_str()", + f"this->{self.field_name}.size()", + max_len=self.max_data_length, ): return result if self.force: @@ -1089,7 +1096,13 @@ class PointerToStringBufferType(PointerToBufferTypeBase): return f'dump_field(out, ESPHOME_PSTR("{self.name}"), this->{self.field_name});' def get_size_calculation(self, name: str, force: bool = False) -> str: - return f"size += ProtoSize::calc_length({self.calculate_field_id_size()}, this->{self.field_name}.size());" + size_field = f"this->{self.field_name}.size()" + max_len = self.max_data_length + if max_len is not None and max_len < 128: + return self._get_single_byte_varint_size( + size_field, force, extra_expr=size_field + ) + return self._get_simple_size_calculation(size_field, force, "length") def get_estimated_size(self) -> int: return self.calculate_field_id_size() + 8 # field ID + 8 bytes typical string