forked from kubernetes/kops
-
Notifications
You must be signed in to change notification settings - Fork 0
/
values.go
105 lines (91 loc) · 1.68 KB
/
values.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
package fi
import (
"encoding/json"
"fmt"
"reflect"
)
func StringValue(s *string) string {
if s == nil {
return ""
}
return *s
}
func String(s string) *string {
return &s
}
func Bool(v bool) *bool {
return &v
}
func BoolValue(v *bool) bool {
if v == nil {
return false
}
return *v
}
func Int(v int) *int {
return &v
}
func Int64(v int64) *int64 {
return &v
}
func DebugPrint(o interface{}) string {
if o == nil {
return "<nil>"
}
if rh, ok := o.(*ResourceHolder); ok {
if rh == nil {
// Avoid go nil vs interface problems
return "<nil>"
}
if rh.Resource == nil {
return fmt.Sprintf("unknown resource %q", rh.Name)
}
}
if resource, ok := o.(Resource); ok {
if resource == nil {
// Avoid go nil vs interface problems
return "<nil>"
}
s, err := ResourceAsString(resource)
if err != nil {
return fmt.Sprintf("error converting resource to string: %v", err)
}
if len(s) >= 256 {
s = s[:256] + "... (truncated)"
}
return s
}
v := reflect.ValueOf(o)
if v.Kind() == reflect.Ptr {
if v.IsNil() {
return "<nil>"
}
v = v.Elem()
}
if !v.IsValid() {
return "<?>"
}
o = v.Interface()
if stringer, ok := o.(fmt.Stringer); ok {
if stringer == nil {
// Avoid go nil vs interface problems
return "<nil>"
}
return stringer.String()
}
return fmt.Sprint(o)
}
func DebugAsJsonString(v interface{}) string {
data, err := json.Marshal(v)
if err != nil {
return fmt.Sprintf("error marshalling: %v", err)
}
return string(data)
}
func DebugAsJsonStringIndent(v interface{}) string {
data, err := json.MarshalIndent(v, "", " ")
if err != nil {
return fmt.Sprintf("error marshalling: %v", err)
}
return string(data)
}