ParseVMID extracts and validates a VM ID from various input types. This function handles the common scenario where VM IDs can come from different sources (JSON APIs, user input, etc.) in various formats. It safely converts the input to a valid integer VM ID. Supported input types: - int: Direct in
(input interface{})
| 258 | // vmid, err := ParseVMID("invalid") // Returns 0, error |
| 259 | // vmid, err := ParseVMID(-1) // Returns 0, error |
| 260 | func ParseVMID(input interface{}) (int, error) { |
| 261 | switch v := input.(type) { |
| 262 | case int: |
| 263 | return v, nil |
| 264 | case float64: |
| 265 | return int(v), nil |
| 266 | case string: |
| 267 | return strconv.Atoi(v) |
| 268 | case nil: |
| 269 | return 0, fmt.Errorf("VMID cannot be nil") |
| 270 | default: |
| 271 | return 0, fmt.Errorf("invalid VMID type: %T", input) |
| 272 | } |
| 273 | } |
| 274 | |
| 275 | // SafeStringValue safely converts various types to string representation. |
| 276 | // |
no outgoing calls