-
Notifications
You must be signed in to change notification settings - Fork 0
/
extractor.go
146 lines (115 loc) · 2.69 KB
/
extractor.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
package ddtags
import (
"fmt"
"reflect"
"strconv"
"strings"
)
const (
tagKey = "ddtag"
tagFmt = "fmt"
)
// Extracts a list of datadog tags using the "ddtag" struct member tags
func Extract(struc any) []string {
if struc == nil {
return []string{}
}
vStruc := reflect.ValueOf(struc)
if vStruc.Kind() != reflect.Interface && vStruc.Kind() != reflect.Pointer {
return []string{}
}
v := vStruc.Elem()
if v.Kind() != reflect.Struct {
return []string{}
}
sType := v.Type()
tags := make([]string, 0, 10)
for i := 0; i < v.NumField(); i++ {
sTag := sType.Field(i).Tag
ddTagVal, hasTag := sTag.Lookup(tagKey)
if !hasTag || ddTagVal == "" {
continue
}
ddTag, tagExtras, _ := strings.Cut(ddTagVal, ",")
if ddTag == "" || ddTag == "-" {
continue
}
field := v.Field(i)
// ignore the zero value
if field.IsZero() {
continue
}
if field.Kind() == reflect.Pointer {
field = field.Elem()
}
var tagValue string
switch f := field.Interface().(type) {
case string:
tagValue = f
case bool:
tagValue = strconv.FormatBool(f)
case int:
tagValue = fmtInteger(f, tagExtras)
case int8:
tagValue = fmtInteger(f, tagExtras)
case int16:
tagValue = fmtInteger(f, tagExtras)
case int32:
tagValue = fmtInteger(f, tagExtras)
case int64:
tagValue = fmtInteger(f, tagExtras)
case uint:
tagValue = fmtInteger(f, tagExtras)
case uint8:
tagValue = fmtInteger(f, tagExtras)
case uint16:
tagValue = fmtInteger(f, tagExtras)
case uint32:
tagValue = fmtInteger(f, tagExtras)
case uint64:
tagValue = fmtInteger(f, tagExtras)
case float32:
tagValue = fmtFloat(f, tagExtras)
case float64:
tagValue = fmtFloat(f, tagExtras)
default:
continue
}
if tagValue != "" {
tags = append(tags, ddTag+":"+tagValue)
}
}
return tags
}
func fmtFloat[T float32 | float64](value T, tagExtras string) string {
strFmt := defaultConfig.FloatFormat
if tagExtras != "" {
for _, extra := range strings.Split(tagExtras, ",") {
nameValue := strings.SplitN(extra, "=", 2)
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case tagFmt:
strFmt = val
}
}
}
}
return fmt.Sprintf(strFmt, float64(value))
}
func fmtInteger[T int | int8 | int16 | int32 | int64 | uint | uint8 | uint16 | uint32 | uint64](value T, tagExtras string) string {
strFmt := defaultConfig.IntegerFormat
if tagExtras != "" {
for _, extra := range strings.Split(tagExtras, ",") {
nameValue := strings.SplitN(extra, "=", 2)
if len(nameValue) == 2 {
name, val := nameValue[0], nameValue[1]
switch name {
case tagFmt:
strFmt = val
}
}
}
}
return fmt.Sprintf(strFmt, value)
}