MCPcopy
hub / github.com/netdata/netdata / structToMap

Function structToMap

src/go/plugin/ibm.d/framework/state.go:219–279  ·  view source on GitHub ↗
(labels any)

Source from the content-addressed store, hash-verified

217}
218
219func structToMap(labels any) map[string]string {
220 result := make(map[string]string)
221
222 if labels == nil {
223 return result
224 }
225
226 // Use reflection to extract struct fields
227 v := reflect.ValueOf(labels)
228 t := reflect.TypeOf(labels)
229
230 // Handle pointer types
231 if v.Kind() == reflect.Pointer {
232 if v.IsNil() {
233 return result
234 }
235 v = v.Elem()
236 t = t.Elem()
237 }
238
239 // Must be a struct
240 if v.Kind() != reflect.Struct {
241 return result
242 }
243
244 // Extract all exported fields from the struct
245 for i := 0; i < v.NumField(); i++ {
246 field := v.Field(i)
247 fieldType := t.Field(i)
248
249 // Skip unexported fields
250 if !fieldType.IsExported() {
251 continue
252 }
253
254 // Convert field name to lowercase for consistency
255 key := strings.ToLower(fieldType.Name)
256
257 // Convert field value to string
258 var value string
259 switch field.Kind() {
260 case reflect.String:
261 value = field.String()
262 case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
263 value = fmt.Sprintf("%d", field.Int())
264 case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
265 value = fmt.Sprintf("%d", field.Uint())
266 case reflect.Float32, reflect.Float64:
267 value = fmt.Sprintf("%.3f", field.Float())
268 case reflect.Bool:
269 value = fmt.Sprintf("%t", field.Bool())
270 default:
271 // For other types, use fmt.Sprintf as fallback
272 value = fmt.Sprintf("%v", field.Interface())
273 }
274
275 result[key] = value
276 }

Callers 1

GetInstanceMethod · 0.85

Calls 3

BoolMethod · 0.80
KindMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…