-
Notifications
You must be signed in to change notification settings - Fork 285
/
utils.go
151 lines (122 loc) · 3.08 KB
/
utils.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
147
148
149
150
151
package gocloak
import (
"context"
"github.com/opentracing/opentracing-go"
)
type contextKey string
var tracerContextKey = contextKey("tracer")
// StringP returns a pointer of a string variable
func StringP(value string) *string {
return &value
}
// PString returns a string value from a pointer
func PString(value *string) string {
if value == nil {
return ""
}
return *value
}
// BoolP returns a pointer of a boolean variable
func BoolP(value bool) *bool {
return &value
}
// PBool returns a boolean value from a pointer
func PBool(value *bool) bool {
if value == nil {
return false
}
return *value
}
// IntP returns a pointer of an integer variable
func IntP(value int) *int {
return &value
}
// Int32P returns a pointer of an int32 variable
func Int32P(value int32) *int32 {
return &value
}
// Int64P returns a pointer of an int64 variable
func Int64P(value int64) *int64 {
return &value
}
// PInt returns an integer value from a pointer
func PInt(value *int) int {
if value == nil {
return 0
}
return *value
}
// PInt32 returns an int32 value from a pointer
func PInt32(value *int32) int32 {
if value == nil {
return 0
}
return *value
}
// PInt64 returns an int64 value from a pointer
func PInt64(value *int64) int64 {
if value == nil {
return 0
}
return *value
}
// Float32P returns a pointer of a float32 variable
func Float32P(value float32) *float32 {
return &value
}
// Float64P returns a pointer of a float64 variable
func Float64P(value float64) *float64 {
return &value
}
// PFloat32 returns an flaot32 value from a pointer
func PFloat32(value *float32) float32 {
if value == nil {
return 0
}
return *value
}
// PFloat64 returns an flaot64 value from a pointer
func PFloat64(value *float64) float64 {
if value == nil {
return 0
}
return *value
}
// NilOrEmpty returns true if string is empty or has a nil value
func NilOrEmpty(value *string) bool {
return value == nil || len(*value) == 0
}
// NilOrEmptyArray returns true if string is empty or has a nil value
func NilOrEmptyArray(value *[]string) bool {
if value == nil || len(*value) == 0 {
return true
}
return (*value)[0] == ""
}
// DecisionStrategyP returns a pointer for a DecisionStrategy value
func DecisionStrategyP(value DecisionStrategy) *DecisionStrategy {
return &value
}
// LogicP returns a pointer for a Logic value
func LogicP(value Logic) *Logic {
return &value
}
// PolicyEnforcementModeP returns a pointer for a PolicyEnforcementMode value
func PolicyEnforcementModeP(value PolicyEnforcementMode) *PolicyEnforcementMode {
return &value
}
// PStringSlice converts a pointer to []string or returns ampty slice if nill value
func PStringSlice(value *[]string) []string {
if value == nil {
return []string{}
}
return *value
}
// NilOrEmptySlice returns true if list is empty or has a nil value
func NilOrEmptySlice(value *[]string) bool {
return value == nil || len(*value) == 0
}
// WithTracer generates a context that has a tracer attached
func WithTracer(ctx context.Context, tracer opentracing.Tracer) context.Context {
return context.WithValue(ctx, tracerContextKey, tracer)
}