From f72c9f78db5180585c0adcc1bd1b4cb46d6acc7a Mon Sep 17 00:00:00 2001 From: xfy Date: Fri, 8 May 2026 18:06:44 +0800 Subject: [PATCH] refactor(config): genericize non-negative validators Replace three duplicate ValidateNonNegative* functions with a single generic implementation using Go 1.18+ generics. - Add SignedInteger type constraint for generic support - Create ValidateNonNegative[T SignedInteger] as unified function - Depprecate ValidateNonNegativeInt64 and ValidateNonNegativeDuration - Both deprecated functions now call the generic version Co-Authored-By: Claude Opus 4.7 --- internal/config/validate.go | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/internal/config/validate.go b/internal/config/validate.go index 11bef3e..58487f6 100644 --- a/internal/config/validate.go +++ b/internal/config/validate.go @@ -116,8 +116,13 @@ func ValidateEnum(value string, validValues []string, fieldName string) error { return fmt.Errorf("无效的 %s: %s(仅支持 %v)", fieldName, value, validValues) } -// ValidateNonNegative 验证值为非负数 -func ValidateNonNegative(value int, fieldName string) error { +// SignedInteger 约束:有符号整数类型 +type SignedInteger interface { + ~int | ~int8 | ~int16 | ~int32 | ~int64 +} + +// ValidateNonNegative 验证值为非负数(泛型版本) +func ValidateNonNegative[T SignedInteger](value T, fieldName string) error { if value < 0 { return fmt.Errorf("%s 不能为负数", fieldName) } @@ -125,19 +130,15 @@ func ValidateNonNegative(value int, fieldName string) error { } // ValidateNonNegativeInt64 验证 int64 值为非负数 +// Deprecated: 使用 ValidateNonNegative[int64] 代替 func ValidateNonNegativeInt64(value int64, fieldName string) error { - if value < 0 { - return fmt.Errorf("%s 不能为负数", fieldName) - } - return nil + return ValidateNonNegative(value, fieldName) } // ValidateNonNegativeDuration 验证 time.Duration 值为非负数 +// Deprecated: 使用 ValidateNonNegative[int64] 代替(time.Duration 是 int64 别名) func ValidateNonNegativeDuration(value time.Duration, fieldName string) error { - if value < 0 { - return fmt.Errorf("%s 不能为负数", fieldName) - } - return nil + return ValidateNonNegative(int64(value), fieldName) } // ValidateNoNullByte 验证字符串不包含 null byte