-
Notifications
You must be signed in to change notification settings - Fork 0
/
marshal.go
61 lines (54 loc) · 1.77 KB
/
marshal.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
// Holds function for marshaling go structs into icinga readable strings
package marshaler
import (
"fmt"
"reflect"
"strings"
)
// Pass any struct into Marshal to create an icinga readable string.
func Marshal(v any) []byte {
return []byte(strings.TrimSpace(string(marshal(v, ""))))
}
// Recursively walks through a struct and generates a string from it.
func marshal(v any, parent string) []byte {
result := []byte{}
if v == nil {
return result
}
s := reflect.Indirect(reflect.ValueOf(v))
for i := 0; i < s.NumField(); i++ {
fieldType := s.Type().Field(i)
currentField := s.Field(i)
if fieldIsExported(fieldType) { // Exported-check must be evaluated first to avoid panic.
if currentField.Kind() == reflect.Ptr { // case when it's a pointer or struct pointer
if currentField.IsNil() {
continue
}
result = append(result, marshal(currentField.Elem().Interface(), parent+fieldType.Name+".")...)
} else if currentField.Kind() == reflect.Struct {
result = append(result, marshal(currentField.Interface(), parent+fieldType.Name+".")...)
} else {
uom := fieldType.Tag.Get("uom")
keys := []string{"warn", "crit", "min", "max"}
perfNumbers := ""
for _, key := range keys {
value := fieldType.Tag.Get(key)
perfNumbers = fmt.Sprintf("%v;%v", perfNumbers, value)
}
if perfNumbers == ";;;;" {
perfNumbers = ""
}
fieldName := fmt.Sprintf("%v%v", parent, fieldType.Name)
customName := fieldType.Tag.Get("icinga")
if customName != "" {
fieldName = customName
}
result = append(result, []byte(fmt.Sprintf("'%v'=%v%v%v ", fieldName, currentField, uom, perfNumbers))...)
}
}
}
return result
}
func fieldIsExported(field reflect.StructField) bool {
return field.Name[0] >= 65 && field.Name[0] <= 90
}