forked from signalfx/golib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pointer.go
115 lines (97 loc) · 2.55 KB
/
pointer.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
package pointer
import (
"fmt"
"reflect"
"time"
)
// Duration returns a pointer to a time.Duration
func Duration(d time.Duration) *time.Duration {
return &d
}
// Int32 returns a pointer to an int32
func Int32(i int32) *int32 {
return &i
}
// Uint returns a pointer to a uint
func Uint(i uint) *uint {
return &i
}
// Uint16 returns a pointer to a uint16
func Uint16(i uint16) *uint16 {
return &i
}
// Uint32 returns a pointer to a uint32
func Uint32(i uint32) *uint32 {
return &i
}
// Uint64 returns a pointer to a uint64
func Uint64(i uint64) *uint64 {
return &i
}
// String returns a pointer to a string
func String(i string) *string {
return &i
}
// Int returns a pointer to an int
func Int(i int) *int {
return &i
}
// Int64 returns a pointer to an int64
func Int64(i int64) *int64 {
return &i
}
// Bool returns a pointer to a bool
func Bool(b bool) *bool {
return &b
}
// Float32 returns a pointer to a float32
func Float32(f float32) *float32 {
return &f
}
// Float64 returns a pointer to a float64
func Float64(b float64) *float64 {
return &b
}
func canNil(k reflect.Kind) bool {
return k == reflect.Chan || k == reflect.Func || k == reflect.Map || k == reflect.Ptr || k == reflect.Interface || k == reflect.Slice
}
// FillDefaultFrom fills default values replacing nil values with the first non nil. The replacement goes into existing
func FillDefaultFrom(defaultsList ...interface{}) interface{} {
if len(defaultsList) == 0 {
return nil
}
rootType := reflect.TypeOf(defaultsList[0])
if rootType.Kind() != reflect.Ptr {
panic(fmt.Sprintf("FillDefaultsFrom only takes pointer types, not %s", rootType))
}
typeToMake := rootType.Elem()
existing := reflect.New(typeToMake).Interface()
existingVal := reflect.ValueOf(existing).Elem()
existingType := reflect.TypeOf(existing)
for _, defaults := range defaultsList {
if defaults == nil {
continue
}
defaultType := reflect.TypeOf(defaults)
if defaultType != existingType {
panic(fmt.Sprintf("Uncompatible types %s vs %s", existingType, defaultType))
}
defaultsVal := reflect.ValueOf(defaults)
if defaultsVal.IsNil() {
continue
}
defaultsVal = defaultsVal.Elem()
singleItemCopy(existingVal, defaultsVal)
}
return existing
}
func singleItemCopy(existingVal reflect.Value, defaultsVal reflect.Value) {
for i := 0; i < existingVal.NumField(); i++ {
if canNil(existingVal.Field(i).Kind()) && existingVal.Field(i).IsNil() {
defaultValue := defaultsVal.Field(i).Interface()
if defaultValue != reflect.ValueOf(nil) {
existingVal.Field(i).Set(defaultsVal.Field(i))
}
}
}
}