【架构优化】合并 botinteraction/socket.go dial 中三处重复的帧 JSON 键收集排序逻辑

オープン
#427 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

@fx-kurumi がすでに取り組んでいます。

2026年11月13日 から。

  • #428 @fx-kurumi による — オープン

評価

難易度
3/5
見積もり時間
半日
初心者へのやさしさ
18/100
issue の種類
リファクタリング
明瞭さ
明確に書かれている
活発さ
停滞
技術スタック
go
領域
backend

調査の方向性

The work is in internal/botinteraction/socket.go, inside the read-loop goroutine of dial, where three near-identical blocks unmarshal the body, event and template_card_event levels, collect keys and sort them. Read that block and the existing frame logging first. Extract the shared logic into a private helper, then add a unit test file for nested keys, missing levels, invalid JSON and empty input. Note that pull request #428 is already open against this issue, so check it before starting. Done means go vet and go test ./internal/botinteraction/... pass with the log format unchanged.

索引モデルが issue の本文から書いたものです。

説明

现状问题

internal/botinteraction/socket.go 的 dial 函数约 75 行,读循环 goroutine 中内联了一段约 25 行的帧诊断日志逻辑:对 body、body.event、body.event.template_card_event 三层 JSON 分别执行「Unmarshal → 收集键 → 排序」,三段代码几乎完全重复,使读循环的主流程(ack 分发 / 回调分发)被淹没,也无法单独测试。

重构方案

  • 新增包内私有辅助函数 sortedJSONKeys(raw json.RawMessage) ([]string, map[string]json.RawMessage),统一完成解析、取键、排序(解析失败或为空时返回空切片 [],与原行为一致)。
  • 新增 logFrameKeys(f Frame) 封装诊断日志,dial 读循环中仅保留一行调用。
  • 日志格式 wecom frame cmd=%s body_keys=%v event_keys=%v card_keys=%v 及触发条件(Cmd 非空且非 aibot_msg_callback)保持不变。

影响范围

仅 internal/botinteraction/socket.go 及新增单测文件;无对外接口、协议或行为变化。

验证方式

  • 新增单测覆盖:三层嵌套键、缺失层级、非法 JSON、空输入。
  • go vet ./internal/botinteraction/...、go test ./internal/botinteraction/... 通过。
主要言語
Go
スター
141
フォーク
28
平均マージ
1日 15時間
マージ済み PR(30日)
113

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

Tencent/LoopForge のほかの issue

Tencent/LoopForge の issue をすべて見る

似ている issue

Go の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。