【架构优化】合并 preview/config.go 中 intFromEnv/int64FromEnv 重复的环境变量正整数解析

未关闭
#411 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

维护者通常 1 天内回复

@fx-kurumi 已经在做这个了。

开始于 2026年11月13日。

  • #412 来自 @fx-kurumi —— 未关闭

评估

难度
2/5
预计耗时
1-3 小时
新手友好度
25/100
Issue 类型
重构
描述清晰度
描述清楚
活跃度
停滞
技术栈
go
领域
tooling

调研方向

Read internal/preview/config.go and compare intFromEnv with int64FromEnv; the issue also specifies the shared helper signature and wrapper behavior. Check the proposed cases in internal/preview/config_env_int_test.go, then run gofmt, go vet ./internal/preview/, and go test -cover ./internal/preview/. Done means the parsing logic is shared while existing behavior is preserved; note that pull request #412 is already open.

由索引模型根据 Issue 内容生成。

描述

现状问题

internal/preview/config.go 中 intFromEnv 与 int64FromEnv 两个函数逐行重复:都是「读环境变量 → TrimSpace → 为空回退 → 解析十进制整数 → 解析失败或 ≤0 回退」,区别仅在于一个用 strconv.Atoi、一个用 strconv.ParseInt(raw, 10, 64)。后续若调整解析规则(如允许 0、加日志)需要两处同步修改,容易漂移。

重构方案

  • 新增包内私有函数 positiveIntFromEnv(key string, fallback int64, bitSize int) int64,承载唯一一份解析逻辑。
  • intFromEnv 改为 int(positiveIntFromEnv(key, int64(fallback), strconv.IntSize));int64FromEnv 改为 positiveIntFromEnv(key, fallback, 64)。
  • 两个原函数签名不变,LoadConfig 等调用方无需改动。
  • strconv.Atoi 等价于 ParseInt(s, 10, strconv.IntSize)(溢出同样报错),因此行为完全一致。

影响范围

仅 internal/preview/config.go(包内私有函数)及新增测试文件,不涉及对外接口、配置项或默认值。

验证方式

  • 新增 internal/preview/config_env_int_test.go:表驱动覆盖空值、空白、合法值、前后空格、0、负数、非法字符、小数、超 int32 的大数、int64 溢出;另测 bitSize 溢出回退。
  • gofmt、go vet ./internal/preview/、go test -cover ./internal/preview/ 全部通过。
主要语言
Go
星标
141
派生
28
平均合并
1 天 15 小时
30 天内合并 PR
113

环境准备

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

Tencent/LoopForge 的其他 Issue

查看 Tencent/LoopForge 的全部 Issue

相似的 Issue

更多 Go Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。