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