Files
nofx/trader/helpers.go
shinchan-zhai 60b97399e4 fix: consistent safe type helpers in auto_trader, log emergency exit errors
- Replace all raw pos["key"].(type) assertions with posFloat64/posString/posInt64 helpers
  across auto_trader_decision, auto_trader_grid, auto_trader_grid_orders,
  auto_trader_grid_regime, auto_trader_loop, auto_trader_orders, auto_trader_risk
- Add posInt64 helper for int64 extraction (createdTime etc)
- Fix emergencyExit: log CloseLong/CloseShort errors instead of silently dropping
- Fix emergencyExit: log GetPositions error on failure
- Upgrade closeAllPositions log level from Infof to Warnf for close failures
- Zero raw type assertions remaining in auto_trader_* files
2026-03-23 12:29:21 +08:00

114 lines
2.5 KiB
Go

package trader
import (
"fmt"
"strconv"
)
// posFloat64 extracts a float64 from a position map, returning 0 on failure.
// Use in loops where a malformed position should be skipped, not crash.
func posFloat64(pos map[string]interface{}, key string) float64 {
v, _ := SafeFloat64(pos, key)
return v
}
// posString extracts a string from a position map, returning "" on failure.
func posString(pos map[string]interface{}, key string) string {
v, _ := SafeString(pos, key)
return v
}
// posInt64 extracts an int64 from a position map, returning 0 on failure.
func posInt64(pos map[string]interface{}, key string) int64 {
value, ok := pos[key]
if !ok {
return 0
}
switch v := value.(type) {
case int64:
return v
case int:
return int64(v)
case float64:
return int64(v)
case string:
parsed, err := strconv.ParseInt(v, 10, 64)
if err != nil {
return 0
}
return parsed
default:
return 0
}
}
// SafeFloat64 Safely extract float64 value from map
func SafeFloat64(data map[string]interface{}, key string) (float64, error) {
value, ok := data[key]
if !ok {
return 0, fmt.Errorf("key '%s' not found", key)
}
switch v := value.(type) {
case float64:
return v, nil
case float32:
return float64(v), nil
case int:
return float64(v), nil
case int64:
return float64(v), nil
case string:
// Try to parse string as float64
parsed, err := strconv.ParseFloat(v, 64)
if err != nil {
return 0, fmt.Errorf("cannot parse string '%s' as float64: %w", v, err)
}
return parsed, nil
default:
return 0, fmt.Errorf("value for key '%s' is not a number (type: %T)", key, v)
}
}
// SafeString Safely extract string value from map
func SafeString(data map[string]interface{}, key string) (string, error) {
value, ok := data[key]
if !ok {
return "", fmt.Errorf("key '%s' not found", key)
}
switch v := value.(type) {
case string:
return v, nil
case fmt.Stringer:
return v.String(), nil
default:
return fmt.Sprintf("%v", v), nil
}
}
// SafeInt Safely extract int value from map
func SafeInt(data map[string]interface{}, key string) (int, error) {
value, ok := data[key]
if !ok {
return 0, fmt.Errorf("key '%s' not found", key)
}
switch v := value.(type) {
case int:
return v, nil
case int64:
return int(v), nil
case float64:
return int(v), nil
case string:
parsed, err := strconv.Atoi(v)
if err != nil {
return 0, fmt.Errorf("cannot parse string '%s' as int: %w", v, err)
}
return parsed, nil
default:
return 0, fmt.Errorf("value for key '%s' is not an integer (type: %T)", key, v)
}
}