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

Abierto
#411 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

@fx-kurumi ya está trabajando en esto.

Desde el 13/11/2026.

  • #412 de @fx-kurumi — abierto

Evaluación

Dificultad
2/5
Tiempo estimado
1-3 horas
Aptitud para principiantes
25/100
Tipo de issue
Refactorización
Claridad
Bien especificado
Estado de actividad
Estancado
Stack tecnológico
go
Área
tooling

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

现状问题

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/ 全部通过。
Lenguaje dominante
Go
Estrellas
141
Forks
28
Merge medio
1 d 15 h
PR fusionados (30 d)
113

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de Tencent/LoopForge

Todos los issues de Tencent/LoopForge

Issues similares

Más issues de Go

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.