-
Notifications
You must be signed in to change notification settings - Fork 15
/
util.go
63 lines (57 loc) · 1.41 KB
/
util.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
package parse
import (
"reflect"
"strings"
"unicode"
)
// Returns the provided string with the first letter upper-cased
func firstToUpper(s string) string {
if len(s) < 1 {
return s
}
return string(unicode.ToUpper(rune(s[0]))) + s[1:]
}
// Returns the provided string with the first letter lower-cased
func firstToLower(s string) string {
if len(s) < 1 {
return s
}
return string(unicode.ToLower(rune(s[0]))) + s[1:]
}
// parses struct tags in the format:
// parse:"name,option"
//
// and returns each component
func parseTag(tag string) (name, options string) {
parts := strings.Split(tag, ",")
if len(parts) > 1 {
return parts[0], parts[1]
} else {
return parts[0], ""
}
}
func isEmptyValue(v reflect.Value) bool {
switch v.Kind() {
case reflect.Array, reflect.Map, reflect.Slice, reflect.String:
return v.Len() == 0
case reflect.Bool:
return !v.Bool()
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
return v.Int() == 0
case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
return v.Uint() == 0
case reflect.Float32, reflect.Float64:
return v.Float() == 0
case reflect.Interface, reflect.Ptr:
return v.IsNil()
}
return false
}
func canBeNil(v reflect.Value) bool {
switch v.Kind() {
case reflect.Chan, reflect.Func, reflect.Map, reflect.Ptr, reflect.Interface, reflect.Slice:
return true
default:
return false
}
}