-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
78 lines (73 loc) · 1.31 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
package gqlcost
import (
"reflect"
"strconv"
)
func toNumber(v interface{}) (int, bool) {
rv := reflect.ValueOf(v)
if !rv.IsValid() {
return 0, false
}
switch rv.Kind() {
case reflect.Array, reflect.Map, reflect.Slice:
n := rv.Len()
if n > 0 {
return n, true
}
case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
n := int(rv.Int())
if n != 0 {
return n, true
}
case reflect.Uint, reflect.Uintptr, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64:
n := int(rv.Uint())
if n > 0 {
return n, true
}
case reflect.Float32, reflect.Float64:
n := int(rv.Float())
if n != 0 {
return n, true
}
case reflect.String:
n, err := strconv.ParseInt(rv.String(), 10, 64)
if err != nil {
return 0, false
}
if n != 0 {
return int(n), true
}
}
return 0, false
}
func maxCost(costs []int) int {
n := len(costs)
switch n {
case 0:
return 0
case 1:
return costs[0]
default:
m := n / 2
a, b := maxCost(costs[:m]), maxCost(costs[m:])
if a > b {
return a
}
return b
}
}
func copyInts(src []int) []int {
n := len(src)
if n == 0 {
return nil
}
dst := make([]int, n)
copy(dst, src)
return dst
}
func typName(typDef interface{}) string {
if x, ok := typDef.(interface{ Name() string }); ok {
return x.Name()
}
return ""
}