feat: 将interface{}替换为any以提高代码一致性
This commit is contained in:
@@ -208,7 +208,7 @@ func (sm *SystemSettingsManager) UpdateSettings(settingsMap map[string]any) erro
|
||||
for key, value := range settingsMap {
|
||||
settingsToUpdate = append(settingsToUpdate, models.SystemSetting{
|
||||
SettingKey: key,
|
||||
SettingValue: fmt.Sprintf("%v", value), // Convert interface{} to string
|
||||
SettingValue: fmt.Sprintf("%v", value), // Convert any to string
|
||||
})
|
||||
}
|
||||
|
||||
@@ -403,7 +403,7 @@ func setFieldFromString(fieldValue reflect.Value, value string) error {
|
||||
|
||||
// 工具函数
|
||||
|
||||
func interfaceToInt(val interface{}) (int, error) {
|
||||
func interfaceToInt(val any) (int, error) {
|
||||
switch v := val.(type) {
|
||||
case int:
|
||||
return v, nil
|
||||
@@ -421,13 +421,13 @@ func interfaceToInt(val interface{}) (int, error) {
|
||||
}
|
||||
|
||||
// interfaceToString is kept for GetEffectiveConfig
|
||||
func interfaceToString(val interface{}) (string, bool) {
|
||||
func interfaceToString(val any) (string, bool) {
|
||||
s, ok := val.(string)
|
||||
return s, ok
|
||||
}
|
||||
|
||||
// interfaceToBool is kept for GetEffectiveConfig
|
||||
func interfaceToBool(val interface{}) (bool, bool) {
|
||||
func interfaceToBool(val any) (bool, bool) {
|
||||
switch v := val.(type) {
|
||||
case bool:
|
||||
return v, true
|
||||
|
@@ -12,7 +12,7 @@ import (
|
||||
// @Tags Dashboard
|
||||
// @Accept json
|
||||
// @Produce json
|
||||
// @Success 200 {object} map[string]interface{}
|
||||
// @Success 200 {object} map[string]any
|
||||
// @Router /api/dashboard/stats [get]
|
||||
func (s *Server) Stats(c *gin.Context) {
|
||||
var totalRequests, successRequests int64
|
||||
|
@@ -25,7 +25,7 @@ func isValidGroupName(name string) bool {
|
||||
}
|
||||
|
||||
// validateAndCleanConfig validates the group config against the GroupConfig struct.
|
||||
func validateAndCleanConfig(configMap map[string]interface{}) (map[string]interface{}, error) {
|
||||
func validateAndCleanConfig(configMap map[string]any) (map[string]any, error) {
|
||||
if configMap == nil {
|
||||
return nil, nil
|
||||
}
|
||||
@@ -60,7 +60,7 @@ func validateAndCleanConfig(configMap map[string]interface{}) (map[string]interf
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var cleanedMap map[string]interface{}
|
||||
var cleanedMap map[string]any
|
||||
if err := json.Unmarshal(validatedBytes, &cleanedMap); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -122,15 +122,15 @@ func (s *Server) ListGroups(c *gin.Context) {
|
||||
// GroupUpdateRequest defines the payload for updating a group.
|
||||
// Using a dedicated struct avoids issues with zero values being ignored by GORM's Update.
|
||||
type GroupUpdateRequest struct {
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
Upstreams json.RawMessage `json:"upstreams"`
|
||||
ChannelType string `json:"channel_type"`
|
||||
Sort *int `json:"sort"`
|
||||
TestModel string `json:"test_model"`
|
||||
ParamOverrides map[string]interface{} `json:"param_overrides"`
|
||||
Config map[string]interface{} `json:"config"`
|
||||
Name string `json:"name"`
|
||||
DisplayName string `json:"display_name"`
|
||||
Description string `json:"description"`
|
||||
Upstreams json.RawMessage `json:"upstreams"`
|
||||
ChannelType string `json:"channel_type"`
|
||||
Sort *int `json:"sort"`
|
||||
TestModel string `json:"test_model"`
|
||||
ParamOverrides map[string]any `json:"param_overrides"`
|
||||
Config map[string]any `json:"config"`
|
||||
}
|
||||
|
||||
// UpdateGroup handles updating an existing group.
|
||||
|
@@ -137,7 +137,7 @@ func (ps *ProxyServer) logRequest(c *gin.Context, group *models.Group, key *mode
|
||||
// updateKeyStats atomically updates the request and failure counts for a key
|
||||
func (ps *ProxyServer) updateKeyStats(keyID uint, success bool) {
|
||||
// Always increment the request count
|
||||
updates := map[string]interface{}{
|
||||
updates := map[string]any{
|
||||
"request_count": gorm.Expr("request_count + 1"),
|
||||
}
|
||||
|
||||
@@ -178,7 +178,7 @@ func (ps *ProxyServer) applyParamOverrides(c *gin.Context, group *models.Group)
|
||||
originalContentType := c.GetHeader("Content-Type")
|
||||
|
||||
// Unmarshal the body into a map
|
||||
var requestData map[string]interface{}
|
||||
var requestData map[string]any
|
||||
if err := json.Unmarshal(bodyBytes, &requestData); err != nil {
|
||||
// If not a valid JSON, just pass it through
|
||||
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
|
||||
|
@@ -10,9 +10,9 @@ import (
|
||||
|
||||
// SuccessResponse defines the standard JSON success response structure.
|
||||
type SuccessResponse struct {
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data interface{} `json:"data,omitempty"`
|
||||
Code int `json:"code"`
|
||||
Message string `json:"message"`
|
||||
Data any `json:"data,omitempty"`
|
||||
}
|
||||
|
||||
// ErrorResponse defines the standard JSON error response structure.
|
||||
@@ -22,7 +22,7 @@ type ErrorResponse struct {
|
||||
}
|
||||
|
||||
// Success sends a standardized success response.
|
||||
func Success(c *gin.Context, data interface{}) {
|
||||
func Success(c *gin.Context, data any) {
|
||||
c.JSON(http.StatusOK, SuccessResponse{
|
||||
Code: 0,
|
||||
Message: "Success",
|
||||
|
@@ -21,7 +21,7 @@ type TaskStatus struct {
|
||||
type TaskService struct {
|
||||
mu sync.Mutex
|
||||
status TaskStatus
|
||||
resultsCache map[string]interface{}
|
||||
resultsCache map[string]any
|
||||
cacheOrder []string
|
||||
maxCacheSize int
|
||||
}
|
||||
@@ -29,9 +29,9 @@ type TaskService struct {
|
||||
// NewTaskService creates a new TaskService.
|
||||
func NewTaskService() *TaskService {
|
||||
return &TaskService{
|
||||
resultsCache: make(map[string]interface{}),
|
||||
resultsCache: make(map[string]any),
|
||||
cacheOrder: make([]string, 0),
|
||||
maxCacheSize: 100, // Store results for the last 100 tasks
|
||||
maxCacheSize: 100,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -100,7 +100,7 @@ func (s *TaskService) EndTask() {
|
||||
}
|
||||
|
||||
// StoreResult stores the result of a finished task.
|
||||
func (s *TaskService) StoreResult(taskID string, result interface{}) {
|
||||
func (s *TaskService) StoreResult(taskID string, result any) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
@@ -116,7 +116,7 @@ func (s *TaskService) StoreResult(taskID string, result interface{}) {
|
||||
}
|
||||
|
||||
// GetResult retrieves the result of a finished task.
|
||||
func (s *TaskService) GetResult(taskID string) (interface{}, bool) {
|
||||
func (s *TaskService) GetResult(taskID string) (any, bool) {
|
||||
s.mu.Lock()
|
||||
defer s.mu.Unlock()
|
||||
|
||||
|
Reference in New Issue
Block a user