-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathutils.go
66 lines (56 loc) · 1.54 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
package patcher
import (
"reflect"
"strings"
)
// ptr returns a pointer to the value passed in.
func ptr[T any](v T) *T {
return &v
}
func isPointerToStruct[T any](t T) bool {
rv := reflect.ValueOf(t)
if rv.Kind() != reflect.Ptr || rv.IsNil() {
return false
}
return rv.Elem().Kind() == reflect.Struct
}
func dereferenceIfPointer(resource any) any {
if reflect.TypeOf(resource).Kind() == reflect.Ptr {
return reflect.ValueOf(resource).Elem().Interface()
}
return resource
}
func ensureStruct(resource any) {
if reflect.TypeOf(resource).Kind() != reflect.Struct {
// Intentionally panic here as this should never happen. This is a programming error.
panic("resource is not a struct")
}
}
func getTag(fType *reflect.StructField, tagName string) string {
tag := fType.Tag.Get(tagName)
if tag == "" {
tag = strings.ToLower(fType.Name)
}
tags := strings.Split(tag, TagOptSeparator)
if len(tags) > 1 {
return tags[0]
}
return tag
}
func getValue(fVal reflect.Value) any {
if fVal.Kind() == reflect.Ptr {
return fVal.Elem().Interface()
}
return fVal.Interface()
}
// IsValidType checks if the given value is of a type that can be stored as a database field.
func IsValidType(val reflect.Value) bool {
switch val.Kind() {
case reflect.Bool, reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64,
reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr,
reflect.Float32, reflect.Float64, reflect.String, reflect.Struct, reflect.Ptr:
return true
default:
return false
}
}