Go: use unified ToInt (#20772)

Signed-off-by: Jin Hai <[email protected]>
This commit is contained in:
Jin Hai
2026-10-11 20:29:20 +08:00
committed by GitHub
parent 5ccba271d9
commit b45dfd927f
7 changed files with 55 additions and 17 deletions
+1 -1
View File
@@ -83,7 +83,7 @@ type nodeBodyFn = func(ctx context.Context, in map[string]any) (map[string]any,
// (componentFactoryFromContext): the override is threaded through
// canvas.Compile → BuildWorkflow → buildNodeBody without the canvas
// package ever importing the ingestion layer.
const ctxKeyOverrideParams ctxKey = "canvas_override_params"
const ctxKeyOverrideParams = "canvas_override_params"
// withOverrideParams attaches a run-level override map to ctx. It is
// a no-op when m is nil so callers can pass a possibly-nil run parameter
+1 -3
View File
@@ -37,9 +37,7 @@ import (
// (events channel, message/session ids) so the statePre/statePost
// wrappers can emit node_started/node_finished without depending on
// the service package.
type ctxKey string
const ctxKeyRunMeta ctxKey = "canvas_run_meta"
const ctxKeyRunMeta = "canvas_run_meta"
const terminalMergeNodeID = "__canvas_terminal_merge__"
// RunMeta carries the per-run metadata that node lifecycle hooks need.
+2 -3
View File
@@ -227,8 +227,7 @@ func TestWithTimeout_ParentCancellation(t *testing.T) {
// which is the documented contract of context.WithTimeout, not a
// helper bug.
func TestWithTimeout_PassesContextToFn(t *testing.T) {
type ctxKey struct{}
parent := context.WithValue(t.Context(), ctxKey{}, "v")
parent := context.WithValue(t.Context(), struct{}{}, "v")
type captured struct {
ctx context.Context
errInFlight error
@@ -251,7 +250,7 @@ func TestWithTimeout_PassesContextToFn(t *testing.T) {
if cap.ctx == parent {
t.Fatal("fn received the parent ctx directly — expected a derived child ctx")
}
if v, _ := cap.ctx.Value(ctxKey{}).(string); v != "v" {
if v, _ := cap.ctx.Value(struct{}{}).(string); v != "v" {
t.Errorf("child ctx did not carry parent's Value(): got %q, want %q", v, "v")
}
if cap.errInFlight != nil {
+1 -3
View File
@@ -363,9 +363,7 @@ func (stdLogRouter) Write(p []byte) (int, error) {
// log helpers below lets a single `grep session_id=<id>` reconstruct one
// turn's full trail (the q71 postmortem had to reconstruct it from timestamps).
type ctxKey int
const sessionIDCtxKey ctxKey = iota
const sessionIDCtxKey int = iota
// WithSessionID returns a context that tags every Ctx-variant log call with
// the conversation turn's session id. Empty ids are a no-op so callers do not
+47
View File
@@ -0,0 +1,47 @@
//
// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package common
import (
"strconv"
"strings"
)
func ToInt(v any) (int, bool) {
switch n := v.(type) {
case int:
return n, true
case int32:
return int(n), true
case int64:
return int(n), true
case float32:
return int(n), true
case float64:
return int(n), true
case bool:
if v.(bool) {
return 1, true
}
return 0, true
case string:
if i, err := strconv.Atoi(strings.TrimSpace(n)); err == nil {
return i, true
}
}
return 0, false
}
+2 -5
View File
@@ -348,9 +348,6 @@ type agentDetailResponse struct {
LastPublishTime *int64 `json:"last_publish_time,omitempty"`
}
// updateAgentRequest is the wire shape for PUT /api/v1/agents/:canvas_id.
type updateAgentRequest map[string]interface{}
func decodeJSONWithNumber(body io.Reader, target any) error {
decoder := json.NewDecoder(body)
decoder.UseNumber()
@@ -373,13 +370,13 @@ func (h *AgentHandler) UpdateAgent(c *gin.Context) {
return
}
canvasID := c.Param("canvas_id")
var req updateAgentRequest
var req map[string]interface{}
if err := decodeJSONWithNumber(c.Request.Body, &req); err != nil && !errors.Is(err, io.EOF) {
common.ResponseWithCodeData(c, common.CodeArgumentError, nil, "Invalid request: "+err.Error())
return
}
if req == nil {
req = updateAgentRequest{}
req = map[string]interface{}{}
}
if err := h.agentService.UpdateAgent(c.Request.Context(), user.ID, canvasID, req); err != nil {
ec, em := mapAgentError(err)
@@ -991,8 +991,7 @@ func TestPipelineExecutor_Execute_DoesNotLogCanceledRun(t *testing.T) {
// Execute is the ctx received by runPipelineFunc - the task context must flow
// through to the pipeline run.
func TestPipelineExecutor_Execute_PropagatesContext(t *testing.T) {
type ctxKey string
const key ctxKey = "trace"
const key string = "trace"
taskCtx := makeTaskCtx()
ctx := t.Context()
taskCtx.Ctx = context.WithValue(ctx, key, "task-ctx")