From 08867c1d73c3d8a81c97e45ba95f23d06ba28293 Mon Sep 17 00:00:00 2001 From: Jin Hai Date: Thu, 6 Aug 2026 12:33:09 +0800 Subject: [PATCH] Go: refactor (#17917) Signed-off-by: Jin Hai --- internal/admin/service.go | 6 +++--- internal/dao/chat_session.go | 6 +++--- internal/dao/compilation_template.go | 5 ++--- internal/dao/compilation_template_seed.go | 8 ++++---- internal/dao/document_test.go | 2 +- 5 files changed, 13 insertions(+), 14 deletions(-) diff --git a/internal/admin/service.go b/internal/admin/service.go index 50aa110c77..e534566133 100644 --- a/internal/admin/service.go +++ b/internal/admin/service.go @@ -463,11 +463,11 @@ func (s *Service) getInitTenantLLM(ctx context.Context, userID string) ([]*entit // Remove duplicates based on (tenant_id, llm_factory, llm_name) seen := make(map[string]bool) var uniqueLLMs []*entity.TenantLLM - for _, tllm := range tenantLLMs { - key := fmt.Sprintf("%s|%s|%s", tllm.TenantID, tllm.LLMFactory, *tllm.LLMName) + for _, tenantLLM := range tenantLLMs { + key := fmt.Sprintf("%s|%s|%s", tenantLLM.TenantID, tenantLLM.LLMFactory, *tenantLLM.LLMName) if !seen[key] { seen[key] = true - uniqueLLMs = append(uniqueLLMs, tllm) + uniqueLLMs = append(uniqueLLMs, tenantLLM) } } diff --git a/internal/dao/chat_session.go b/internal/dao/chat_session.go index 46c2d3b670..0917725d7f 100644 --- a/internal/dao/chat_session.go +++ b/internal/dao/chat_session.go @@ -110,7 +110,7 @@ func (dao *ChatSessionDAO) DeleteByID(ctx context.Context, db *gorm.DB, id strin // ListByChatID lists chat sessions by chat ID func (dao *ChatSessionDAO) ListByChatID(ctx context.Context, db *gorm.DB, chatID, sessionID, name, orderby string, desc bool, page, pageSize int) ([]*entity.ChatSession, error) { - var convs []*entity.ChatSession + var chatSessions []*entity.ChatSession query := db.WithContext(ctx).Where("dialog_id = ?", chatID) if sessionID != "" { query = query.Where("id = ?", sessionID) @@ -132,8 +132,8 @@ func (dao *ChatSessionDAO) ListByChatID(ctx context.Context, db *gorm.DB, chatID } query = query.Offset((page - 1) * pageSize).Limit(pageSize) } - err := query.Find(&convs).Error - return convs, err + err := query.Find(&chatSessions).Error + return chatSessions, err } // CheckDialogExists checks if a dialog exists with given tenant_id and dialog_id diff --git a/internal/dao/compilation_template.go b/internal/dao/compilation_template.go index 929d44a5dc..0f5515dc74 100644 --- a/internal/dao/compilation_template.go +++ b/internal/dao/compilation_template.go @@ -189,8 +189,7 @@ func (dao *CompilationTemplateDAO) UpdateFields(ctx context.Context, db *gorm.DB Where("id = ?", id).Updates(m).Error } -// UpdateStatusByGroup flips the status of every valid template in a group, -// mirroring Python group delete's child cascade. +// UpdateStatusByGroup flips the status of every valid template in a group func (dao *CompilationTemplateDAO) UpdateStatusByGroup(ctx context.Context, db *gorm.DB, groupID, status string) error { return db.WithContext(ctx).Model(&entity.CompilationTemplate{}). Where("group_id = ? AND status = ?", groupID, string(entity.StatusValid)). @@ -206,7 +205,7 @@ func (dao *CompilationTemplateDAO) UpdateStatusByID(ctx context.Context, db *gor // HardDeleteOrphansByName physically removes stale, invalid, non-built-in // templates of the given name within the group, mirroring Python -// _purge_stale_invalid_children (which DELETEs orphaned duplicate names after a +// _purge_stale_invalid_children (which Deletes orphaned duplicate names after a // group child is soft-deleted). The deletion is scoped to the group so a // same-named template in another group is never affected. These rows were // soft-deleted in a prior operation but must be permanently purged to keep the diff --git a/internal/dao/compilation_template_seed.go b/internal/dao/compilation_template_seed.go index 055093ee4f..26c4e987b4 100644 --- a/internal/dao/compilation_template_seed.go +++ b/internal/dao/compilation_template_seed.go @@ -49,7 +49,7 @@ var builtinCompilationTemplateKinds = []struct { {Kind: "timeline", Name: "Timeline"}, } -func strptr(s string) *string { return &s } +func strPTR(s string) *string { return &s } // builtinTemplateID derives a deterministic, <=32-byte id for a built-in // compilation template from "-". A plain concatenation @@ -92,7 +92,7 @@ func SeedBuiltinCompilationTemplatesForTenant(ctx context.Context, db *gorm.DB, ID: BuiltinCompilationTemplateGroupID, TenantID: "", // global built-in catalogue Name: "Built-in templates", - Status: strptr(valid), + Status: &valid, } if err := tx.WithContext(ctx).Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, @@ -105,11 +105,11 @@ func SeedBuiltinCompilationTemplatesForTenant(ctx context.Context, db *gorm.DB, tmpl := &entity.CompilationTemplate{ ID: builtinTemplateID(t.Kind), TenantID: nil, // global built-in catalogue - GroupID: strptr(BuiltinCompilationTemplateGroupID), + GroupID: strPTR(BuiltinCompilationTemplateGroupID), Name: t.Name, Kind: t.Kind, Config: entity.JSONMap{"kind": t.Kind}, - Status: strptr(valid), + Status: &valid, } if err := tx.WithContext(ctx).Clauses(clause.OnConflict{ Columns: []clause.Column{{Name: "id"}}, diff --git a/internal/dao/document_test.go b/internal/dao/document_test.go index 88d2059fb2..80dda70d2d 100644 --- a/internal/dao/document_test.go +++ b/internal/dao/document_test.go @@ -130,7 +130,7 @@ func TestDocumentGetByKBIDOrdersByCreateTime(t *testing.T) { dao := NewDocumentDAO() docs, total, err := dao.GetByKBID(ctx, db, "kb1") if err != nil { - t.Fatalf("GetByKBID failed: %v", err) + t.Fatalf("fail to get document by dataset id: %v", err) } if total != 2 { t.Fatalf("expected total=2, got %d", total)