- .golangci.yml:standard 集 - errcheck(存量 38 处裸忽略待专项),staticcheck 关纯风格/quickfix - 清理 3 个无引用死函数;SanitizedJSON 不可达分支收敛为接口分支 + nil 指针防护(原死分支中的 nil 检查从未执行) - CI backend job 增加 golangci-lint-action@v6
73 lines
1.8 KiB
Go
73 lines
1.8 KiB
Go
package models
|
|
|
|
// Sanitizer interface for models that can sanitize themselves
|
|
// 用于从API响应中移除敏感字段
|
|
type Sanitizer interface {
|
|
Sanitized() map[string]interface{}
|
|
}
|
|
|
|
// SanitizedJSON sanitizes a single model or a slice of models.
|
|
// 使用接口替代类型断言,更优雅且可扩展。
|
|
func SanitizedJSON(v any) any {
|
|
switch val := v.(type) {
|
|
// Single objects: 实现 Sanitizer 接口的类型统一走这里(nil 指针安全返回 nil)
|
|
case Sanitizer:
|
|
if val == nil {
|
|
return nil
|
|
}
|
|
return val.Sanitized()
|
|
// Slices
|
|
case []User:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []Cluster:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []Host:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []StorageConfig:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []Collector:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []LokiDeployment:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
case []LogPolicyGroup:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i := range val {
|
|
result[i] = val[i].Sanitized()
|
|
}
|
|
return result
|
|
case []Sanitizer:
|
|
result := make([]map[string]interface{}, len(val))
|
|
for i, item := range val {
|
|
result[i] = item.Sanitized()
|
|
}
|
|
return result
|
|
default:
|
|
// 尝试通过反射处理指针类型
|
|
return v
|
|
}
|
|
}
|