From 8c52664cc50000ef58b6687e8e277323ede2185e Mon Sep 17 00:00:00 2001 From: Daniel Pupius Date: Sun, 20 Apr 2025 19:09:37 -0700 Subject: [PATCH] Refactor message handling to use switch statement MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Replace if-else chain with cleaner switch statement - Add support for "c" format messages - Improve code structure and readability 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude --- mqtt/client.go | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/mqtt/client.go b/mqtt/client.go index 62a0e7a..421d076 100644 --- a/mqtt/client.go +++ b/mqtt/client.go @@ -92,8 +92,9 @@ func (c *Client) messageHandler(client mqtt.Client, msg mqtt.Message) { return } - // For now, only handle "e" and "map" format messages - if topicInfo.Format == "e" || topicInfo.Format == "map" { + // Process different message formats + switch topicInfo.Format { + case "e", "c", "map": // Binary encoded protobuf message decodedPacket := decoder.DecodeMessage(msg.Payload(), topicInfo) @@ -114,10 +115,12 @@ func (c *Client) messageHandler(client mqtt.Client, msg mqtt.Message) { // Channel buffer is full, log a warning and drop the message log.Println("Warning: Message buffer full, dropping message") } - } else if topicInfo.Format == "json" { + + case "json": // TODO: Add support for JSON format messages in the future log.Printf("Ignoring JSON format message from topic: %s\n", msg.Topic()) - } else { + + default: // Unsupported format, log and ignore log.Printf("Unsupported format: %s from topic: %s\n", topicInfo.Format, msg.Topic()) }