fix(output): deterministic --format table ordering and show error status code #258
Loading…
Reference in New Issue
No description provided.
Delete Branch "luwanzhou/gitlink-cli:fix/output-table-determinism"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
问题描述
gitlink-cli ... --format table的渲染依赖 Go map 的遍历顺序,而 Go 的 map 遍历是随机的。printMapTable(单对象 KEY/VALUE 表)每次运行的行顺序都不一样;collectKeys在补全非优先列时直接for k := range m,导致printSliceTable(列表表)优先列之后的列顺序也随机。Error: <message>,不显示状态码。diff、截图,也无法在脚本 / 测试中稳定断言;错误时也不易快速区分 404 / 422 / 500。问题复现步骤
对任意返回扁平对象的命令使用
--format table,例如:多次运行,观察 KEY/VALUE 行顺序(及列表表优先列之后的列顺序)在两次运行间发生变化。
触发一个错误(如对不存在资源请求)并用
--format table,输出只有Error: <message>,看不到状态码。根本原因
internal/output/formatter.go:printMapTable直接for k, v := range m输出,行顺序 = map 随机遍历顺序;collectKeys在追加非优先列时for k := range m,列顺序同样随机;envelope.Error.Message,未利用已有的envelope.Error.Code。修复方案
仅改动
internal/output/formatter.go(与其它进行中的 PR 无文件交叉):collectKeys:优先列(id/name/login/title/status/state/created_at/updated_at)之后的剩余列改为sort.Strings排序,列顺序稳定可预期。printMapTable:改为按collectKeys的顺序输出,行顺序确定,且与列表表的列顺序保持一致。Error [<code>]: <message>(无错误码时回退Error: <message>),便于快速识别状态码。不影响 json / yaml 输出,也不改变成功数据内容,仅稳定其呈现顺序并丰富错误提示。
合并请求描述
本 PR 修复
--format table输出随 Go map 遍历顺序变化、不确定的问题,使表格输出在多次运行间稳定一致,并在错误输出中补充状态码,提升可读性与可脚本化程度。相关Issue
无直接关联 Issue(输出格式 / 错误提示一致性优化,对应赛题一「优化现有命令的输出格式、错误提示」)。
变更内容
1. 修复:表格输出确定性排序(
internal/output/formatter.go)collectKeys非优先列sort.Strings排序;printMapTable按collectKeys顺序渲染,行序确定且与列表表一致。2. 增强:错误输出展示状态码
Error [<code>]: <message>(无码时回退Error: <message>)。3. 测试(
internal/output/formatter_test.go)collectKeys非优先列按字典序排序;Error [500]: ...,无码时回退Error: ...。4. 变更说明(
doc/changes/output-table-determinism.md)测试与验证
go build ./...、go vet ./...、go test ./...、gofmt -s全部通过;新增/既有internal/output测试全绿。