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

Ouverte
#411 0 commentaires 0 réactions 0 personnes assignées Voir sur GitHub

Les mainteneurs répondent en général sous 1 jour

@fx-kurumi y travaille déjà.

Depuis le 13/11/2026.

  • #412 par @fx-kurumi — ouverte

Évaluation

Difficulté
2/5
Temps estimé
1-3 heures
Accessibilité débutants
25/100
Type d'issue
Refactorisation
Clarté
Clairement spécifiée
Activité
À l'abandon
Stack technique
go
Domaine
tooling

Piste de recherche

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.

Rédigé par le modèle d'indexation à partir du texte de l'issue.

Description

现状问题

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/ 全部通过。
Langage dominant
Go
Étoiles
141
Forks
28
Merge moyen
1 j 15 h
PR mergées (30 j)
113

Préparer son environnement

Par où commencer

  1. Lisez l'issue en entier, puis le guide de contribution du projet.
  2. Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
  3. Forkez le dépôt et travaillez sur une branche.
  4. Ouvrez une pull request qui référence le numéro de l'issue.

Autres issues de Tencent/LoopForge

Toutes les issues de Tencent/LoopForge

Issues similaires

Plus d'issues Go

Recevez les nouvelles issues par e-mail

Un résumé court des issues GitHub adaptées aux débutants.