Go: fix warnings (#17738)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-08-03 21:30:01 +08:00
committed by GitHub
parent d357eea8ef
commit 86021932ae
103 changed files with 437 additions and 439 deletions

View File

@@ -394,13 +394,13 @@ func (h *AgentHandler) UpdateAgent(c *gin.Context) {
common.ResponseWithCodeData(c, ec, nil, em)
return
}
canvas, err := h.agentService.GetAgent(c.Request.Context(), user.ID, canvasID)
if err != nil || canvas == nil {
canvasInstance, err := h.agentService.GetAgent(c.Request.Context(), user.ID, canvasID)
if err != nil || canvasInstance == nil {
common.SuccessWithData(c, map[string]interface{}{}, "success")
return
}
common.SuccessWithData(c, map[string]interface{}{
"update_time": canvas.UpdateTime,
"update_time": canvasInstance.UpdateTime,
}, "success")
}

View File

@@ -59,7 +59,7 @@ func (h *AgentHandler) GetComponentInputForm(c *gin.Context) {
cv, err := h.loader.LoadCanvasByID(c.Request.Context(), user.ID, canvasID)
if err != nil {
if err == dao.ErrUserCanvasNotFound {
if errors.Is(err, dao.ErrUserCanvasNotFound) {
common.ResponseWithCodeData(c, common.CodeOperatingError, nil, canvasNoAccessMessage)
return
}
@@ -144,7 +144,7 @@ func (h *AgentHandler) DebugComponent(c *gin.Context) {
cv, err := h.loader.LoadCanvasByID(c.Request.Context(), user.ID, canvasID)
if err != nil {
if err == dao.ErrUserCanvasNotFound {
if errors.Is(err, dao.ErrUserCanvasNotFound) {
common.ResponseWithCodeData(c, common.CodeOperatingError, nil, canvasNoAccessMessage)
return
}

View File

@@ -34,6 +34,7 @@
package handler
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
@@ -74,7 +75,7 @@ func (h *AgentHandler) UploadAgentFile(c *gin.Context) {
// (103) with the python permission message so existing clients can
// still pattern-match the text.
if _, err := h.loader.LoadCanvasByID(c.Request.Context(), user.ID, canvasID); err != nil {
if err == dao.ErrUserCanvasNotFound {
if errors.Is(err, dao.ErrUserCanvasNotFound) {
common.ResponseWithCodeData(c, common.CodeOperatingError, nil, canvasNoAccessMessage)
return
}

View File

@@ -85,7 +85,7 @@ const (
// fail-closed default. PR review round 5 (#2) — the previous
// form leaked that distinction via two different messages.
var errWebhookFailClosed = errors.New(
"webhook security is required. Set allow_anonymous to true to permit unauthenticated webhooks.",
"webhook security is required. Set allow_anonymous to true to permit unauthenticated webhooks",
)
// validateWebhookSecurity is the orchestrator.
@@ -362,15 +362,15 @@ func isTruthyAllowAnonymous(cfg map[string]any) bool {
func validateTokenAuth(c *gin.Context, cfg map[string]any) error {
rawToken, _ := cfg["token"].(map[string]any)
if rawToken == nil {
return fmt.Errorf("Invalid token authentication")
return fmt.Errorf("invalid token authentication")
}
header, _ := rawToken["token_header"].(string)
want, _ := rawToken["token_value"].(string)
if header == "" || want == "" {
return fmt.Errorf("Invalid token authentication")
return fmt.Errorf("invalid token authentication")
}
if c.GetHeader(header) != want {
return fmt.Errorf("Invalid token authentication")
return fmt.Errorf("invalid token authentication")
}
return nil
}
@@ -382,16 +382,16 @@ func validateTokenAuth(c *gin.Context, cfg map[string]any) error {
func validateBasicAuth(c *gin.Context, cfg map[string]any) error {
rawBasic, _ := cfg["basic_auth"].(map[string]any)
if rawBasic == nil {
return fmt.Errorf("Invalid Basic Auth credentials")
return fmt.Errorf("invalid basic auth credentials")
}
username, _ := rawBasic["username"].(string)
password, _ := rawBasic["password"].(string)
if username == "" || password == "" {
return fmt.Errorf("Invalid Basic Auth credentials")
return fmt.Errorf("invalid basic auth credentials")
}
u, p, ok := c.Request.BasicAuth()
if !ok || u != username || p != password {
return fmt.Errorf("Invalid Basic Auth credentials")
return fmt.Errorf("invalid basic auth credentials")
}
return nil
}

View File

@@ -266,8 +266,8 @@ func TestWebhook_TokenAuthFails(t *testing.T) {
if code != int(common.CodeDataError) {
t.Errorf("code = %d, want %d", code, common.CodeDataError)
}
if msg != "Invalid token authentication" {
t.Errorf("message = %q, want %q", msg, "Invalid token authentication")
if msg != "invalid token authentication" {
t.Errorf("message = %q, want %q", msg, "invalid token authentication")
}
}

View File

@@ -190,7 +190,7 @@ func TestChatbotInfo_HasTavilyKey(t *testing.T) {
func TestChatbotInfo_ForeignTenant(t *testing.T) {
stub := &stubBotService{
chatbotInfoFn: func(ctx context.Context, tenantID, dialogID string) (string, string, string, string, bool, common.ErrorCode, error) {
return "", "", "", "", false, common.CodeDataError, errors.New("Authentication error: no access to this chatbot!")
return "", "", "", "", false, common.CodeDataError, errors.New("authentication error: no access to this chatbot")
},
}
r := botTestEngine(stub)
@@ -484,7 +484,7 @@ func TestAgentbotCompletion_URLBoundAgentID(t *testing.T) {
func TestAgentbotCompletion_NoAccess(t *testing.T) {
stub := &stubBotService{
agentbotCompleteFn: func(ctx context.Context, tenantID, agentID string, req service.AgentbotCompletionRequest) (<-chan canvas.RunEvent, common.ErrorCode, error) {
return nil, common.CodeDataError, errors.New("Can't find agent by ID: a1")
return nil, common.CodeDataError, errors.New("can't find agent by ID: a1")
},
}
r := botTestEngine(stub)
@@ -497,8 +497,8 @@ func TestAgentbotCompletion_NoAccess(t *testing.T) {
if resp.Code != 102 {
t.Errorf("code = %d, want 102", resp.Code)
}
if !strings.Contains(resp.Message, "Can't find agent") {
t.Errorf("message = %q, want contains 'Can't find agent'", resp.Message)
if !strings.Contains(resp.Message, "can't find agent") {
t.Errorf("message = %q, want contains 'can't find agent'", resp.Message)
}
}
@@ -610,7 +610,7 @@ func TestAgentbotInputs_MissingBeginComponent(t *testing.T) {
func TestAgentbotInputs_NotFound(t *testing.T) {
stub := &stubBotService{
agentbotInputsFn: func(ctx context.Context, tenantID, agentID string) (string, string, string, string, map[string]any, common.ErrorCode, error) {
return "", "", "", "", nil, common.CodeDataError, errors.New("Can't find agent by ID: a1")
return "", "", "", "", nil, common.CodeDataError, errors.New("can't find agent by ID: a1")
},
}
r := botTestEngine(stub)
@@ -623,8 +623,8 @@ func TestAgentbotInputs_NotFound(t *testing.T) {
if resp.Code != 102 {
t.Errorf("code = %d, want 102", resp.Code)
}
if !strings.Contains(resp.Message, "Can't find agent") {
t.Errorf("message = %q, want contains 'Can't find agent'", resp.Message)
if !strings.Contains(resp.Message, "can't find agent") {
t.Errorf("message = %q, want contains 'can't find agent'", resp.Message)
}
}
@@ -1049,14 +1049,15 @@ func TestDownloadAttachment_Unauth(t *testing.T) {
c.Abort()
return
}
if u, code, err := stub.GetUserByToken(c.Request.Context(), auth); err != nil || code != common.CodeSuccess {
u, code, err := stub.GetUserByToken(c.Request.Context(), auth)
if err != nil || code != common.CodeSuccess {
common.ResponseWithCodeData(c, common.CodeUnauthorized, nil, "Invalid auth credentials")
c.Abort()
return
} else {
c.Set("user", u)
c.Next()
}
c.Set("user", u)
c.Next()
})
g.GET("/attachments/:attachment_id/download", h.DownloadAttachment)
@@ -1321,7 +1322,7 @@ func TestGetAgentbotLogs_DeniesInaccessibleAgent(t *testing.T) {
h := NewBotHandler(nil)
h.botService = &stubBotService{agentbotLogsFn: func(context.Context, string, string, string) (map[string]any, common.ErrorCode, error) {
return nil, common.CodeDataError, errors.New("Can't find agent by ID: agent-b")
return nil, common.CodeDataError, errors.New("can't find agent by ID: agent-b")
}}
h.GetAgentbotLogs(c)
@@ -1333,7 +1334,7 @@ func TestGetAgentbotLogs_DeniesInaccessibleAgent(t *testing.T) {
if resp.Code != int(common.CodeDataError) {
t.Errorf("code = %d, want %d", resp.Code, common.CodeDataError)
}
if !strings.Contains(resp.Message, "Can't find agent") {
if !strings.Contains(resp.Message, "can't find agent") {
t.Errorf("message = %q, want an access denial", resp.Message)
}
}

View File

@@ -105,9 +105,9 @@ func (h *ChatSessionHandler) ListChatSessions(c *gin.Context) {
ctx := c.Request.Context()
result, err := h.chatSessionService.ListChatSessions(ctx, userID, chatID, c.Query("id"), c.Query("name"), orderby, desc, page, pageSize)
if err != nil {
// Mirror Python: ownership failures return code 109 "No authorization."
if strings.Contains(err.Error(), "No authorization") {
common.ResponseWithCodeData(c, common.CodeAuthenticationError, false, "No authorization.")
// Mirror Python: ownership failures return code 109 "no authorization"
if strings.Contains(err.Error(), "no authorization") {
common.ResponseWithCodeData(c, common.CodeAuthenticationError, false, "no authorization")
return
}
common.ResponseWithHttpCodeData(c, http.StatusInternalServerError, 500, nil, err.Error())

View File

@@ -76,7 +76,7 @@ func (h *DatasetArtifactHandler) datasetOwner(c *gin.Context, datasetID string)
return user, kb.TenantID, msg
}
// HEAD /artifacts — any wiki artifact present?
// AnyArtifact handles HEAD /artifacts — any wiki artifact present?
func (h *DatasetArtifactHandler) AnyArtifact(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -94,7 +94,7 @@ func (h *DatasetArtifactHandler) AnyArtifact(c *gin.Context) {
}
}
// GET /artifacts — list wiki pages.
// ListArtifacts handles GET /artifacts — list wiki pages.
func (h *DatasetArtifactHandler) ListArtifacts(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -122,7 +122,7 @@ func (h *DatasetArtifactHandler) ListArtifacts(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "pages": items}, "success")
}
// PUT /artifacts/<page_type>/<slug> — edit a wiki page.
// UpdateArtifact handles PUT /artifacts/<page_type>/<slug> — edit a wiki page.
func (h *DatasetArtifactHandler) UpdateArtifact(c *gin.Context) {
user, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -192,7 +192,7 @@ func (h *DatasetArtifactHandler) UpdateArtifact(c *gin.Context) {
common.SuccessWithData(c, detail, "success")
}
// GET /artifacts/<page_type>/<slug> — single wiki page.
// GetArtifact handles GET /artifacts/<page_type>/<slug> — single wiki page.
func (h *DatasetArtifactHandler) GetArtifact(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -213,7 +213,7 @@ func (h *DatasetArtifactHandler) GetArtifact(c *gin.Context) {
common.SuccessWithData(c, detail, "success")
}
// DELETE /artifacts — clear all wiki artifacts.
// DeleteArtifacts handles DELETE /artifacts — clear all wiki artifacts.
func (h *DatasetArtifactHandler) DeleteArtifacts(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -227,7 +227,7 @@ func (h *DatasetArtifactHandler) DeleteArtifacts(c *gin.Context) {
common.SuccessWithData(c, deleted, "success")
}
// GET /artifacts/topics — list wiki topics.
// ListArtifactTopics handles GET /artifacts/topics — list wiki topics.
func (h *DatasetArtifactHandler) ListArtifactTopics(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -242,7 +242,7 @@ func (h *DatasetArtifactHandler) ListArtifactTopics(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "topics": items}, "success")
}
// GET /artifacts/alteration — wiki alteration summary.
// GetArtifactAlteration handles GET /artifacts/alteration — wiki alteration summary.
func (h *DatasetArtifactHandler) GetArtifactAlteration(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -257,7 +257,7 @@ func (h *DatasetArtifactHandler) GetArtifactAlteration(c *gin.Context) {
common.SuccessWithData(c, alt, "success")
}
// GET /artifacts/graph — wiki entity/relation graph.
// GetArtifactGraph handles GET /artifacts/graph — wiki entity/relation graph.
func (h *DatasetArtifactHandler) GetArtifactGraph(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -272,7 +272,7 @@ func (h *DatasetArtifactHandler) GetArtifactGraph(c *gin.Context) {
common.SuccessWithData(c, graph, "success")
}
// GET /artifacts/structure — list compiled structures of a dataset.
// ListStructures handles GET /artifacts/structure — list compiled structures of a dataset.
func (h *DatasetArtifactHandler) ListStructures(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -289,7 +289,7 @@ func (h *DatasetArtifactHandler) ListStructures(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "structures": items}, "success")
}
// DELETE /artifacts/structure — delete compiled structures of a dataset.
// DeleteStructures handles DELETE /artifacts/structure — delete compiled structures of a dataset.
func (h *DatasetArtifactHandler) DeleteStructures(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -306,7 +306,7 @@ func (h *DatasetArtifactHandler) DeleteStructures(c *gin.Context) {
common.SuccessWithData(c, gin.H{"deleted": n}, "success")
}
// HEAD /skills — any skill artifact present?
// AnySkill handles HEAD /skills — any skill artifact present?
func (h *DatasetArtifactHandler) AnySkill(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -324,7 +324,7 @@ func (h *DatasetArtifactHandler) AnySkill(c *gin.Context) {
}
}
// GET /navigation — list navigation clusters.
// ListNavigation handles GET /navigation — list navigation clusters.
func (h *DatasetArtifactHandler) ListNavigation(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -338,7 +338,7 @@ func (h *DatasetArtifactHandler) ListNavigation(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "nav": items}, "success")
}
// DELETE /navigation — delete all navigation clusters.
// DeleteNavigation handles DELETE /navigation — delete all navigation clusters.
func (h *DatasetArtifactHandler) DeleteNavigation(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -352,7 +352,7 @@ func (h *DatasetArtifactHandler) DeleteNavigation(c *gin.Context) {
common.SuccessWithData(c, gin.H{"deleted": n}, "success")
}
// DELETE /navigation/<name> — delete a single navigation cluster.
// DeleteNavigationNode handles DELETE /navigation/<name> — delete a single navigation cluster.
func (h *DatasetArtifactHandler) DeleteNavigationNode(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -366,7 +366,7 @@ func (h *DatasetArtifactHandler) DeleteNavigationNode(c *gin.Context) {
common.SuccessWithData(c, gin.H{"deleted": n}, "success")
}
// GET /navigation/<name>/children — list children of a navigation cluster.
// ListNavigationChildren handles GET /navigation/<name>/children — list children of a navigation cluster.
func (h *DatasetArtifactHandler) ListNavigationChildren(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -380,7 +380,7 @@ func (h *DatasetArtifactHandler) ListNavigationChildren(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "children": items}, "success")
}
// GET /skills — skill tree.
// GetSkillTree handles GET /skills — skill tree.
func (h *DatasetArtifactHandler) GetSkillTree(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -395,7 +395,7 @@ func (h *DatasetArtifactHandler) GetSkillTree(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "tree": items}, "success")
}
// DELETE /skills — delete all skills.
// DeleteSkills handles DELETE /skills — delete all skills.
func (h *DatasetArtifactHandler) DeleteSkills(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -409,7 +409,7 @@ func (h *DatasetArtifactHandler) DeleteSkills(c *gin.Context) {
common.SuccessWithData(c, gin.H{"deleted": n}, "success")
}
// GET /skills/<skill_kwd> — single skill page.
// GetSkillPage handles GET /skills/<skill_kwd> — single skill page.
func (h *DatasetArtifactHandler) GetSkillPage(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -427,7 +427,7 @@ func (h *DatasetArtifactHandler) GetSkillPage(c *gin.Context) {
common.SuccessWithData(c, detail, "success")
}
// DELETE /skills/<skill_kwd> — delete a single skill.
// DeleteSkill handles DELETE /skills/<skill_kwd> — delete a single skill.
func (h *DatasetArtifactHandler) DeleteSkill(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -441,7 +441,7 @@ func (h *DatasetArtifactHandler) DeleteSkill(c *gin.Context) {
common.SuccessWithData(c, gin.H{"deleted": n}, "success")
}
// GET /documents/<document_id>/structure/graph — document structure graph.
// GetDocumentGraph handles GET /documents/<document_id>/structure/graph — document structure graph.
func (h *DatasetArtifactHandler) GetDocumentGraph(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {
@@ -458,7 +458,7 @@ func (h *DatasetArtifactHandler) GetDocumentGraph(c *gin.Context) {
common.SuccessWithData(c, gin.H{"total": total, "graph": items}, "success")
}
// DELETE /documents/<document_id>/structure/graph — delete document structure graph.
// DeleteDocumentGraph handles DELETE /documents/<document_id>/structure/graph — delete document structure graph.
func (h *DatasetArtifactHandler) DeleteDocumentGraph(c *gin.Context) {
_, tenantID, _ := h.datasetOwner(c, c.Param("dataset_id"))
if tenantID == "" {

View File

@@ -263,7 +263,7 @@ func (h *DocumentHandler) GetDocumentPreview(c *gin.Context) {
ctx := c.Request.Context()
preview, err := h.documentService.GetDocumentPreview(ctx, docID)
if err != nil {
common.ErrorWithCode(c, common.CodeDataError, "Document not found!")
common.ErrorWithCode(c, common.CodeDataError, "document not found")
return
}
@@ -636,7 +636,7 @@ func parseDocumentListOptions(c *gin.Context, datasetID string) (dao.DocumentLis
docID := c.Query("id")
docIDs := queryValues(c, "ids")
if docID != "" && len(docIDs) > 0 {
return opts, fmt.Sprintf("Should not provide both 'id':%s and 'ids'%v", docID, docIDs)
return opts, fmt.Sprintf("should not provide both 'id':%s and 'ids'%v", docID, docIDs)
}
if docID != "" {
opts.DocIDs = []string{docID}

View File

@@ -92,7 +92,7 @@ func (h *OpenAIChatHandler) OpenAIChatCompletions(c *gin.Context) {
return
} else {
for _, item := range rawArr {
if _, ok := item.(string); !ok {
if _, ok = item.(string); !ok {
common.ResponseWithCodeData(c, common.CodeArgumentError, nil,
"reference_metadata.fields must be an array.")
return

View File

@@ -334,7 +334,7 @@ func (h *SearchHandler) UpdateSearch(c *gin.Context) {
errMsg := err.Error()
switch errMsg {
case "no authorization":
common.ResponseWithCodeData(c, common.CodeAuthenticationError, false, "No authorization.")
common.ResponseWithCodeData(c, common.CodeAuthenticationError, false, "no authorization")
case "duplicated search name":
common.ResponseWithCodeData(c, common.CodeDataError, nil, "Duplicated search name.")
default: