MCPcopy Create free account
hub / github.com/devnullvoid/pvetui / ParseVMID

Function ParseVMID

pkg/api/utils.go:260–273  ·  view source on GitHub ↗

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{})

Source from the content-addressed store, hash-verified

258// vmid, err := ParseVMID("invalid") // Returns 0, error
259// vmid, err := ParseVMID(-1) // Returns 0, error
260func 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//

Callers 1

TestParseVMIDFunction · 0.85

Calls

no outgoing calls

Tested by 1

TestParseVMIDFunction · 0.68