From 35a893e1d27e5454c66a0cfbda3bfbf39b812ec1 Mon Sep 17 00:00:00 2001 From: Haoxin Li Date: Mon, 28 Jul 2025 17:59:54 +0800 Subject: [PATCH] refactor: update struct field types from interface{} to any for improved type safety --- backend/domain/codesnippet.go | 74 +++++++++---------- backend/domain/workspace.go | 36 ++++----- backend/ent/schema/workspace.go | 2 +- .../codesnippet/usecase/codesnippet.go | 10 +-- backend/internal/socket/handler/socket.go | 16 ++-- .../internal/workspace/usecase/workspace.go | 2 +- 6 files changed, 70 insertions(+), 70 deletions(-) diff --git a/backend/domain/codesnippet.go b/backend/domain/codesnippet.go index e58e1dc..57540e3 100644 --- a/backend/domain/codesnippet.go +++ b/backend/domain/codesnippet.go @@ -24,47 +24,47 @@ type CodeSnippetRepo interface { // 请求结构体 type CreateCodeSnippetReq struct { - WorkspaceFileID string `json:"workspace_file_id" validate:"required"` // 关联的workspace file ID - Name string `json:"name"` // 代码片段名称 - SnippetType string `json:"snippet_type"` // 代码片段类型 (function, class, variable, etc.) - Language string `json:"language"` // 编程语言 - Content string `json:"content"` // 代码片段内容 - Hash string `json:"hash"` // 内容哈希 - StartLine int `json:"start_line"` // 起始行号 - EndLine int `json:"end_line"` // 结束行号 - StartColumn int `json:"start_column"` // 起始列号 - EndColumn int `json:"end_column"` // 结束列号 - Namespace string `json:"namespace"` // 命名空间 - ContainerName string `json:"container_name"` // 容器名称 (类名、模块名等) - Scope []string `json:"scope"` // 作用域信息 - Dependencies []string `json:"dependencies"` // 依赖项 - Parameters []map[string]interface{} `json:"parameters"` // 参数列表 - Signature string `json:"signature"` // 函数签名 - DefinitionText string `json:"definition_text"` // 定义文本 - StructuredInfo map[string]interface{} `json:"structured_info"` // 结构化信息 + WorkspaceFileID string `json:"workspace_file_id" validate:"required"` // 关联的workspace file ID + Name string `json:"name"` // 代码片段名称 + SnippetType string `json:"snippet_type"` // 代码片段类型 (function, class, variable, etc.) + Language string `json:"language"` // 编程语言 + Content string `json:"content"` // 代码片段内容 + Hash string `json:"hash"` // 内容哈希 + StartLine int `json:"start_line"` // 起始行号 + EndLine int `json:"end_line"` // 结束行号 + StartColumn int `json:"start_column"` // 起始列号 + EndColumn int `json:"end_column"` // 结束列号 + Namespace string `json:"namespace"` // 命名空间 + ContainerName string `json:"container_name"` // 容器名称 (类名、模块名等) + Scope []string `json:"scope"` // 作用域信息 + Dependencies []string `json:"dependencies"` // 依赖项 + Parameters []map[string]any `json:"parameters"` // 参数列表 + Signature string `json:"signature"` // 函数签名 + DefinitionText string `json:"definition_text"` // 定义文本 + StructuredInfo map[string]any `json:"structured_info"` // 结构化信息 } // 数据模型 type CodeSnippet struct { - ID string `json:"id"` // 代码片段ID - WorkspaceFileID string `json:"workspace_file_id"` // 关联的workspace file ID - Name string `json:"name"` // 代码片段名称 - SnippetType string `json:"snippet_type"` // 代码片段类型 - Language string `json:"language"` // 编程语言 - Content string `json:"content"` // 代码片段内容 - Hash string `json:"hash"` // 内容哈希 - StartLine int `json:"start_line"` // 起始行号 - EndLine int `json:"end_line"` // 结束行号 - StartColumn int `json:"start_column"` // 起始列号 - EndColumn int `json:"end_column"` // 结束列号 - Namespace string `json:"namespace"` // 命名空间 - ContainerName string `json:"container_name"` // 容器名称 - Scope []string `json:"scope"` // 作用域信息 - Dependencies []string `json:"dependencies"` // 依赖项 - Parameters []map[string]interface{} `json:"parameters"` // 参数列表 - Signature string `json:"signature"` // 函数签名 - DefinitionText string `json:"definition_text"` // 定义文本 - StructuredInfo map[string]interface{} `json:"structured_info"` // 结构化信息 + ID string `json:"id"` // 代码片段ID + WorkspaceFileID string `json:"workspace_file_id"` // 关联的workspace file ID + Name string `json:"name"` // 代码片段名称 + SnippetType string `json:"snippet_type"` // 代码片段类型 + Language string `json:"language"` // 编程语言 + Content string `json:"content"` // 代码片段内容 + Hash string `json:"hash"` // 内容哈希 + StartLine int `json:"start_line"` // 起始行号 + EndLine int `json:"end_line"` // 结束行号 + StartColumn int `json:"start_column"` // 起始列号 + EndColumn int `json:"end_column"` // 结束列号 + Namespace string `json:"namespace"` // 命名空间 + ContainerName string `json:"container_name"` // 容器名称 + Scope []string `json:"scope"` // 作用域信息 + Dependencies []string `json:"dependencies"` // 依赖项 + Parameters []map[string]any `json:"parameters"` // 参数列表 + Signature string `json:"signature"` // 函数签名 + DefinitionText string `json:"definition_text"` // 定义文本 + StructuredInfo map[string]any `json:"structured_info"` // 结构化信息 } func (c *CodeSnippet) From(e *db.CodeSnippet) *CodeSnippet { diff --git a/backend/domain/workspace.go b/backend/domain/workspace.go index d0a3dba..5477b92 100644 --- a/backend/domain/workspace.go +++ b/backend/domain/workspace.go @@ -60,18 +60,18 @@ type WorkspaceFileRepo interface { // 请求结构体 type CreateWorkspaceReq struct { - UserID string `json:"user_id" validate:"required"` // 用户ID - Name string `json:"name" validate:"required"` // 工作区名称 - Description string `json:"description"` // 工作区描述 - RootPath string `json:"root_path" validate:"required"` // 工作区根路径 - Settings map[string]interface{} `json:"settings"` // 工作区设置 + UserID string `json:"user_id" validate:"required"` // 用户ID + Name string `json:"name" validate:"required"` // 工作区名称 + Description string `json:"description"` // 工作区描述 + RootPath string `json:"root_path" validate:"required"` // 工作区根路径 + Settings map[string]any `json:"settings"` // 工作区设置 } type UpdateWorkspaceReq struct { - ID string `json:"id" validate:"required"` // 工作区ID - Name *string `json:"name"` // 工作区名称 - Description *string `json:"description"` // 工作区描述 - Settings map[string]interface{} `json:"settings"` // 工作区设置 + ID string `json:"id" validate:"required"` // 工作区ID + Name *string `json:"name"` // 工作区名称 + Description *string `json:"description"` // 工作区描述 + Settings map[string]any `json:"settings"` // 工作区设置 } type ListWorkspaceReq struct { @@ -151,15 +151,15 @@ type SyncWorkspaceFileResp struct { // 数据模型 type Workspace struct { - ID string `json:"id"` // 工作区ID - UserID string `json:"user_id"` // 用户ID - Name string `json:"name"` // 工作区名称 - Description string `json:"description"` // 工作区描述 - RootPath string `json:"root_path"` // 工作区根路径 - Settings map[string]interface{} `json:"settings"` // 工作区设置 - LastAccessedAt int64 `json:"last_accessed_at"` // 最后访问时间 - CreatedAt int64 `json:"created_at"` // 创建时间 - UpdatedAt int64 `json:"updated_at"` // 更新时间 + ID string `json:"id"` // 工作区ID + UserID string `json:"user_id"` // 用户ID + Name string `json:"name"` // 工作区名称 + Description string `json:"description"` // 工作区描述 + RootPath string `json:"root_path"` // 工作区根路径 + Settings map[string]any `json:"settings"` // 工作区设置 + LastAccessedAt int64 `json:"last_accessed_at"` // 最后访问时间 + CreatedAt int64 `json:"created_at"` // 创建时间 + UpdatedAt int64 `json:"updated_at"` // 更新时间 } type WorkspaceFile struct { diff --git a/backend/ent/schema/workspace.go b/backend/ent/schema/workspace.go index a89a126..553d700 100644 --- a/backend/ent/schema/workspace.go +++ b/backend/ent/schema/workspace.go @@ -33,7 +33,7 @@ func (Workspace) Fields() []ent.Field { field.String("name").Optional().Comment("工作区名称"), field.String("description").Optional().Comment("工作区描述"), field.String("root_path").NotEmpty().Comment("工作区根路径"), - field.JSON("settings", map[string]interface{}{}).Optional().Comment("工作区设置"), + field.JSON("settings", map[string]any{}).Optional().Comment("工作区设置"), field.Time("last_accessed_at").Default(time.Now).Comment("最后访问时间"), field.Time("created_at").Default(time.Now).Immutable().Comment("创建时间"), field.Time("updated_at").Default(time.Now).UpdateDefault(time.Now).Comment("更新时间"), diff --git a/backend/internal/codesnippet/usecase/codesnippet.go b/backend/internal/codesnippet/usecase/codesnippet.go index 9726501..e782e63 100644 --- a/backend/internal/codesnippet/usecase/codesnippet.go +++ b/backend/internal/codesnippet/usecase/codesnippet.go @@ -38,13 +38,13 @@ func (u *CodeSnippetUsecase) CreateFromIndexResult(ctx context.Context, workspac // StartColumn 和 EndColumn 在 IndexResult 中没有直接对应字段,暂时设置为 0 StartColumn: 0, EndColumn: 0, - Namespace: "", // IndexResult 中没有直接对应字段 - ContainerName: "", // IndexResult 中没有直接对应字段 - Dependencies: []string{}, // IndexResult 中没有直接对应字段 - Parameters: []map[string]interface{}{}, // IndexResult 中没有直接对应字段 + Namespace: "", // IndexResult 中没有直接对应字段 + ContainerName: "", // IndexResult 中没有直接对应字段 + Dependencies: []string{}, // IndexResult 中没有直接对应字段 + Parameters: []map[string]any{}, // IndexResult 中没有直接对应字段 Signature: indexResult.Signature, DefinitionText: indexResult.DefinitionText, - StructuredInfo: map[string]interface{}{ + StructuredInfo: map[string]any{ "definition": indexResult.Definition, }, } diff --git a/backend/internal/socket/handler/socket.go b/backend/internal/socket/handler/socket.go index 7dfd412..d6d50a0 100644 --- a/backend/internal/socket/handler/socket.go +++ b/backend/internal/socket/handler/socket.go @@ -350,10 +350,10 @@ func (h *SocketHandler) processFileUpdateAsync(socket *socketio.Socket, updateDa codeFiles := domain.CodeFiles{ Files: []domain.FileMeta{ { - FilePath: updateData.FilePath, - FileExtension: fileExtension, - Language: h.getFileLanguage(fileExtension), - Content: updateData.Content, + FilePath: updateData.FilePath, + // FileExtension: fileExtension, + Language: h.getFileLanguage(fileExtension), + Content: updateData.Content, }, }, } @@ -432,10 +432,10 @@ func (h *SocketHandler) processFileUpdateAsync(socket *socketio.Socket, updateDa codeFiles := domain.CodeFiles{ Files: []domain.FileMeta{ { - FilePath: updateData.FilePath, - FileExtension: fileExtension, - Language: h.getFileLanguage(fileExtension), - Content: updateData.Content, + FilePath: updateData.FilePath, + // FileExtension: fileExtension, + Language: h.getFileLanguage(fileExtension), + Content: updateData.Content, }, }, } diff --git a/backend/internal/workspace/usecase/workspace.go b/backend/internal/workspace/usecase/workspace.go index e383637..b38cafb 100644 --- a/backend/internal/workspace/usecase/workspace.go +++ b/backend/internal/workspace/usecase/workspace.go @@ -163,7 +163,7 @@ func (u *WorkspaceUsecase) EnsureWorkspace(ctx context.Context, userID, rootPath Name: name, Description: fmt.Sprintf("Auto-created workspace for %s", rootPath), RootPath: rootPath, - Settings: map[string]interface{}{}, + Settings: map[string]any{}, } return u.Create(ctx, createReq)