-
Notifications
You must be signed in to change notification settings - Fork 8
/
string.go
83 lines (75 loc) · 1.76 KB
/
string.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
package cvt
import (
"encoding/json"
"fmt"
"strconv"
)
// String convert an interface to a string type, with default value
func String(v interface{}, def ...string) string {
if v, err := StringE(v); err == nil {
return v
}
if len(def) > 0 {
return def[0]
}
return ""
}
// StringE convert an interface to a string type
func StringE(val interface{}) (string, error) {
// interface implements
switch vv := val.(type) {
case fmt.Stringer:
return vv.String(), nil
case error:
return vv.Error(), nil
case json.Marshaler:
vvv, e := vv.MarshalJSON()
if e == nil {
return string(vvv), nil
}
}
// direct type
switch vv := val.(type) {
case nil:
return "", nil
case bool:
return strconv.FormatBool(vv), nil
case string:
return vv, nil
case []byte:
return string(vv), nil
case []rune:
return string(vv), nil
case uint, uint8, uint16, uint32, uint64, uintptr:
return strconv.FormatUint(Uint64(vv), 10), nil
case int, int8, int16, int32, int64:
return strconv.FormatInt(Int64(vv), 10), nil
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(vv), 'f', -1, 32), nil
}
// indirect type
v, _, rv := indirect(val)
switch vv := v.(type) {
case nil:
return "", nil
case bool:
return strconv.FormatBool(vv), nil
case string:
return vv, nil
case []byte:
return string(vv), nil
case []rune:
return string(vv), nil
case uint, uint8, uint16, uint32, uint64, uintptr:
return strconv.FormatUint(rv.Uint(), 10), nil
case int, int8, int16, int32, int64:
return strconv.FormatInt(rv.Int(), 10), nil
case float64:
return strconv.FormatFloat(vv, 'f', -1, 64), nil
case float32:
return strconv.FormatFloat(float64(vv), 'f', -1, 32), nil
}
return "", newErr(val, "string")
}