forked from jbsmith7741/uri
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.go
63 lines (55 loc) · 1.48 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
package uri
import (
"encoding"
"fmt"
"reflect"
"strings"
)
func isAlias(v reflect.Value) bool {
if v.Kind() == reflect.Struct || v.Kind() == reflect.Ptr {
return false
}
return strings.Contains(v.Type().String(), ".")
}
func implementsUnmarshaler(v reflect.Value) bool {
return v.Type().Implements(reflect.TypeOf((*encoding.TextUnmarshaler)(nil)).Elem())
}
func implementsMarshaler(v reflect.Value) bool {
return v.Type().Implements(reflect.TypeOf((*encoding.TextMarshaler)(nil)).Elem())
}
func tryMarshal(v reflect.Value) (string, error) {
// does it implement TextMarshaler?
if implementsMarshaler(v) {
b, err := v.Interface().(encoding.TextMarshaler).MarshalText()
return string(b), err
} else if v.Type().Implements(reflect.TypeOf((*fmt.Stringer)(nil)).Elem()) {
return v.Interface().(fmt.Stringer).String(), nil
}
return "", nil
}
func isZero(v reflect.Value) bool {
if !v.CanInterface() {
return false
}
switch v.Kind() {
case reflect.Func, reflect.Map, reflect.Slice:
return v.IsNil()
case reflect.Array:
z := true
for i := 0; i < v.Len(); i++ {
z = z && isZero(v.Index(i))
}
return z
}
// Compare other types directly:
z := reflect.Zero(v.Type())
return v.Interface() == z.Interface()
}
// parseURITag splits the passed tag value by comma and returns the first value. Comma separated values
// is only checked when using the "json" tag as the name.
func parseURITag(tv string) string {
if !usingJSONTag {
return tv
}
return strings.Split(tv, ",")[0]
}