From c590ffd289abe4cc83effe8fa6d8c4e9a40fad12 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 22 Jul 2025 09:03:01 -1000 Subject: [PATCH] cleans to dump --- esphome/components/api/api_pb2_dump.cpp | 43 ++++++++++++++----------- script/api_protobuf/api_protobuf.py | 43 ++++++++++++++----------- 2 files changed, 48 insertions(+), 38 deletions(-) diff --git a/esphome/components/api/api_pb2_dump.cpp b/esphome/components/api/api_pb2_dump.cpp index 20595cab9f..2aa970d3dc 100644 --- a/esphome/components/api/api_pb2_dump.cpp +++ b/esphome/components/api/api_pb2_dump.cpp @@ -19,67 +19,72 @@ static inline void append_quoted_string(std::string &out, const StringRef &ref) out.append("'"); } +// Common helpers for dump_field functions +static inline void append_field_prefix(std::string &out, const char *field_name, int indent) { + out.append(indent, ' ').append(field_name).append(": "); +} + +static inline void append_with_newline(std::string &out, const char *str) { + out.append(str); + out.append("\n"); +} + // Helper functions to reduce code duplication in dump methods static void dump_field(std::string &out, const char *field_name, int32_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%" PRId32, value); - out.append(buffer); - out.append("\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, uint32_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%" PRIu32, value); - out.append(buffer); - out.append("\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, float value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%g", value); - out.append(buffer); - out.append("\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, double value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%g", value); - out.append(buffer); - out.append("\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, uint64_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%llu", value); - out.append(buffer); - out.append("\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, bool value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append(YESNO(value)); out.append("\n"); } static void dump_field(std::string &out, const char *field_name, const std::string &value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append("'").append(value).append("'"); out.append("\n"); } static void dump_field(std::string &out, const char *field_name, StringRef value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); append_quoted_string(out, value); out.append("\n"); } template static void dump_field(std::string &out, const char *field_name, T value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append(proto_enum_to_string(value)); out.append("\n"); } diff --git a/script/api_protobuf/api_protobuf.py b/script/api_protobuf/api_protobuf.py index 58e3e953c9..dd3294030c 100755 --- a/script/api_protobuf/api_protobuf.py +++ b/script/api_protobuf/api_protobuf.py @@ -2085,68 +2085,73 @@ static inline void append_quoted_string(std::string &out, const StringRef &ref) out.append("'"); } +// Common helpers for dump_field functions +static inline void append_field_prefix(std::string &out, const char *field_name, int indent) { + out.append(indent, ' ').append(field_name).append(": "); +} + +static inline void append_with_newline(std::string &out, const char *str) { + out.append(str); + out.append("\\n"); +} + // Helper functions to reduce code duplication in dump methods static void dump_field(std::string &out, const char *field_name, int32_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%" PRId32, value); - out.append(buffer); - out.append("\\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, uint32_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%" PRIu32, value); - out.append(buffer); - out.append("\\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, float value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%g", value); - out.append(buffer); - out.append("\\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, double value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%g", value); - out.append(buffer); - out.append("\\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, uint64_t value, int indent = 2) { char buffer[64]; - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); snprintf(buffer, 64, "%llu", value); - out.append(buffer); - out.append("\\n"); + append_with_newline(out, buffer); } static void dump_field(std::string &out, const char *field_name, bool value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append(YESNO(value)); out.append("\\n"); } static void dump_field(std::string &out, const char *field_name, const std::string &value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append("'").append(value).append("'"); out.append("\\n"); } static void dump_field(std::string &out, const char *field_name, StringRef value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); append_quoted_string(out, value); out.append("\\n"); } template static void dump_field(std::string &out, const char *field_name, T value, int indent = 2) { - out.append(indent, ' ').append(field_name).append(": "); + append_field_prefix(out, field_name, indent); out.append(proto_enum_to_string(value)); out.append("\\n"); }