-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathutils.go
More file actions
92 lines (75 loc) · 1.71 KB
/
utils.go
File metadata and controls
92 lines (75 loc) · 1.71 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
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
package shared
import (
"fmt"
"reflect"
"strings"
"unicode/utf8"
)
// ToPtr - returns a pointer to the given value.
func ToPtr[T any](v T) *T {
return &v
}
// ToValue - returns the value of the bool pointer passed in
func ToValue[T any](ptr *T) T {
return *ptr
}
// ToValueDefault - returns the value of the pointer passed in, or the default type value if the pointer is nil
func ToValueDefault[T any](ptr *T) T {
var defaultVal T
if ptr == nil {
return defaultVal
}
return *ptr
}
func SliceToValueDefault[T any](ptrSlice *[]T) []T {
return append([]T{}, *ptrSlice...)
}
type Nullable[T any] struct {
value *T
isSet bool
}
func (v Nullable[T]) Get() *T {
return v.value
}
func (v *Nullable[T]) Set(val *T) {
v.value = val
v.isSet = true
}
func (v Nullable[T]) IsSet() bool {
return v.isSet
}
func (v *Nullable[T]) Unset() {
v.value = nil
v.isSet = false
}
func Strlen(s string) int {
return utf8.RuneCountInString(s)
}
// IsNil checks if an input is nil
func IsNil(i interface{}) bool {
if i == nil {
return true
}
switch reflect.TypeOf(i).Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.UnsafePointer, reflect.Interface, reflect.Slice:
return reflect.ValueOf(i).IsNil()
case reflect.Array:
return reflect.ValueOf(i).IsZero()
}
return false
}
// EnsureURLFormat checks that the URL has the correct format (no trailing slash,
// has http/https scheme prefix) and updates it if necessary
func EnsureURLFormat(url string) string {
length := len(url)
if length <= 1 {
return url
}
if url[length-1] == '/' {
url = url[:length-1]
}
if !strings.HasPrefix(url, "https://") && !strings.HasPrefix(url, "http://") {
url = fmt.Sprintf("https://%s", url)
}
return url
}