-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalue_format.go
More file actions
57 lines (52 loc) · 1.2 KB
/
value_format.go
File metadata and controls
57 lines (52 loc) · 1.2 KB
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
package rigging
import (
"reflect"
"time"
)
func formatStructuredValue(v reflect.Value, secret bool) any {
if secret {
return "***redacted***"
}
if !v.IsValid() || (v.Kind() == reflect.Ptr && v.IsNil()) {
return nil
}
switch v.Kind() {
case reflect.String:
return v.String()
case reflect.Bool:
return v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
if v.Type() == durationType {
if dur, ok := v.Interface().(time.Duration); ok {
return dur.String()
}
}
return v.Int()
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
return v.Uint()
case reflect.Float32, reflect.Float64:
return v.Float()
case reflect.Slice:
if v.Type().Elem().Kind() == reflect.String {
slice := make([]string, v.Len())
for i := 0; i < v.Len(); i++ {
slice[i] = v.Index(i).String()
}
return slice
}
slice := make([]any, v.Len())
for i := 0; i < v.Len(); i++ {
slice[i] = v.Index(i).Interface()
}
return slice
case reflect.Struct:
if v.Type() == timeType {
if t, ok := v.Interface().(time.Time); ok {
return t.Format(time.RFC3339)
}
}
return v.Interface()
default:
return v.Interface()
}
}