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

Function SafeStringValue

pkg/api/utils.go:299–316  ·  view source on GitHub ↗

SafeStringValue safely converts various types to string representation. This function provides a safe way to convert interface{} values to strings, handling multiple common types that can appear in JSON or API responses. It never panics and always returns a string value. Supported conversions: - s

(value interface{})

Source from the content-addressed store, hash-verified

297// str := SafeStringValue(true) // "true"
298// str := SafeStringValue(nil) // ""
299func SafeStringValue(value interface{}) string {
300 if value == nil {
301 return ""
302 }
303
304 switch v := value.(type) {
305 case string:
306 return v
307 case int:
308 return strconv.Itoa(v)
309 case float64:
310 return strconv.FormatFloat(v, 'f', -1, 64)
311 case bool:
312 return strconv.FormatBool(v)
313 default:
314 return fmt.Sprintf("%v", v)
315 }
316}
317
318// SafeFloatValue safely converts various types to float64 representation.
319//

Callers 3

mainFunction · 0.92
TestSafeStringValueFunction · 0.85
GetClusterTasksMethod · 0.85

Calls

no outgoing calls

Tested by 1

TestSafeStringValueFunction · 0.68