-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathconv.go
137 lines (118 loc) · 2.48 KB
/
conv.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
// Copyright (c) 2019 srfrog - https://srfrog.me
// Use of this source code is governed by the license in the LICENSE file.
package dict
import (
"reflect"
"strconv"
)
// Stringer is just like fmt.Stringer without loading that package.
type Stringer interface {
String() string
}
var stringerType = reflect.TypeOf((*Stringer)(nil)).Elem()
func toFloat64(x interface{}) float64 {
if v, ok := x.(float32); ok {
return float64(v)
}
return x.(float64)
}
func toUint64(x interface{}) uint64 {
switch v := x.(type) {
case uint:
return uint64(v)
case uint8:
return uint64(v)
case uint16:
return uint64(v)
case uint32:
return uint64(v)
case uint64:
return v
}
return x.(uint64)
}
func toInt64(x interface{}) int64 {
switch v := x.(type) {
case int:
return int64(v)
case int8:
return int64(v)
case int16:
return int64(v)
case int32:
return int64(v)
case int64:
return v
}
return x.(int64)
}
func toString(x interface{}) string {
var s string
switch v := x.(type) {
case float32, float64:
s = strconv.FormatFloat(toFloat64(v), 'f', -1, 64)
case int, int8, int16, int32, int64:
s = strconv.FormatInt(toInt64(v), 10)
case uint, uint8, uint16, uint32, uint64:
s = strconv.FormatUint(toUint64(v), 10)
case string:
s = v
case Stringer:
s = v.String()
}
return s
}
// Item is a key-value pair.
// Key is the key name value.
// Value is the stored value in dict.
type Item struct {
Key interface{}
Value interface{}
}
func toIterable(i interface{}) <-chan Item {
ci := make(chan Item)
go func() {
defer close(ci)
if item, ok := i.(Item); ok {
ci <- item
return
}
t := reflect.TypeOf(i)
if t == nil {
return
}
switch v := reflect.ValueOf(i); t.Kind() {
case reflect.Map:
// The map key must be a hashable key type.
if !isKeyType(t.Key()) {
break
}
for iter := v.MapRange(); iter.Next(); {
ci <- Item{Key: iter.Key().Interface(), Value: iter.Value().Interface()}
}
case reflect.Chan:
L:
for {
x, ok := v.Recv()
if !ok {
break L
}
ci <- Item{Value: x.Interface()}
}
case reflect.Array, reflect.Slice:
for j := 0; j < v.Len(); j++ {
ci <- Item{Key: j, Value: v.Index(j).Interface()}
}
default:
ci <- Item{Value: v.Interface()}
}
}()
return ci
}
func isKeyType(t reflect.Type) bool {
kind := t.Kind()
return (kind > reflect.Bool && kind < reflect.Uintptr) ||
kind == reflect.Float32 || kind == reflect.Float64 ||
kind == reflect.String ||
t.Implements(stringerType)
}