-
-
Notifications
You must be signed in to change notification settings - Fork 107
/
reflect.go
58 lines (50 loc) · 1.22 KB
/
reflect.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
/*
* Copyright (c) 2018 LI Zhennan
*
* Use of this work is governed by a MIT License.
* You may find a license copy in project root.
*/
package etherscan
import (
"fmt"
"reflect"
"strconv"
)
// extractValue obtains string-formed slice representation via
// input. It only handles string, int, and their slice form, and
// panics otherwise.
func extractValue(input interface{}) (output []string) {
v := direct(reflect.ValueOf(input))
if v.Kind() == reflect.Slice {
length := v.Len()
output = make([]string, length)
for i := 0; i < length; i++ {
output[i] = valueToStr(v.Index(i))
}
} else {
output = make([]string, 1)
output[0] = valueToStr(v)
}
return
}
// valueToStr convert v into proper string representation
// Only handles int and string, panic otherwise.
func valueToStr(v reflect.Value) (str string) {
switch v.Kind() {
case reflect.String:
str = v.String()
case reflect.Int:
str = strconv.FormatInt(v.Int(), 10)
default:
panic(fmt.Sprintf("valueToStr: %v is of unexpected kind %q", v, v.Kind()))
}
return
}
// direct traverses the pointer chain to fetch
// the solid value
func direct(v reflect.Value) reflect.Value {
for ; v.Kind() == reflect.Ptr; v = v.Elem() {
// relax
}
return v
}